Page 1 of 1

need some help from pro coder

PostPosted: Thu Dec 30, 2010 4:08 pm
by Petani
while(ProcessId==0)
{
Sleep10;
ProcessId=FindProcessId("someapplication");
if(inkey()[0]!=0)
{
fflush(stdout);
ExitProcess(0);
}
}
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,ProcessId);
TerminateProcess(hProcess,0);
CloseHandle(hProcess);
ProcessId=0;
while(ProcessId==0)
{
Sleep10;
ProcessId=FindProcess("someapplication");
if(inkey()[0]!=0)
{
fflush(stdout);
ExitProcess(0);
}
}


is there any possibility for me to rechange Findprocess to FindWindow by editing this line ~>ProcessId=FindProcess("someapplication"); to ProcessId=FindWindow(NULL, La La La");

Re: need some help from pro coder

PostPosted: Thu Dec 30, 2010 6:10 pm
by L. Spiro
Code: Select all
DWORD ProcessIdByWindowTitle( LPCTSTR _lpWindowName ) {
   if ( !_lpWindowName || _lpWindowName[0] == _T( '\0' ) ) { return 0UL; }
   // Walk over every thread.
   HANDLE hThreadSnap = ::CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
   if ( hThreadSnap == INVALID_HANDLE_VALUE ) {
      return 0UL;
   }

   // Copy the window name to a writeable buffer.
   _TCHAR * ptcCopy = ::StrDup( _lpWindowName );

   THREADENTRY32 te32Thread;
   te32Thread.dwSize = sizeof( THREADENTRY32 );

   if ( !::Thread32First( hThreadSnap, &te32 ) ) {
      ::CloseHandle( hThreadSnap );
      ::LocalFree( ptcCopy );
      return 0UL;
   }

   do {
      // Enumerate its windows.
      ::EnumThreadWindows( te32.th32ThreadID, EnumThreadWndowProc, ptcCopy );
      if ( ptcCopy[0] == _T( '\0' ) ) {
         // Found it.
         ::CloseHandle( hThreadSnap );
         ::LocalFree( ptcCopy );
         return te32Thread.th32OwnerProcessID;
      }
   } while ( ::Thread32Next( hThreadSnap, &te32Thread ) );

   ::CloseHandle( hThreadSnap );
   ::LocalFree( ptcCopy );

   return 0UL;
}



BOOL CALLBACK EnumThreadWndowProc( HWND _hWnd, LPARAM _lParam ) {
   int iLen = ::GetWindowTextLength( _hWnd ) + 1;
   _TCHAR * ptcText = new _TCHAR[iLen];
   ::GetWindowText( _hWnd, ptcText, iLen );

   _TCHAR * ptcCheckMe = reinterpret_cast<_TCHAR *>(_lParam);
   int iRet = ::StrCmp( ptcText, ptcCheckMe );   // This is NOT ::strcmp().
   // Tell the outside world we found the target process by modifying the input string buffer.
   //   Buffer must be at least 1 character in length, always, and not read-only.
   //   This is why ProcessIdByWindowTitle() creates a copy of the input name and passes that
   //   to this function.
   if ( iRet == 0 ) {
      ptcCheckMe[0] = _T( '\0' );   // NULL the first character.
   }

   delete [] ptcText;
   return iRet != 0;
}



L. Spiro

Re: need some help from pro coder

PostPosted: Thu Dec 30, 2010 11:45 pm
by Petani
thx alot