• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Sending windows messages to a process

I need to send a windows messages to a process. I have the process' handle. How could I resolve this problem?.
0
svillalba
Asked:
svillalba
  • 3
  • 2
  • 2
  • +1
1 Solution
 
migelCommented:
Hi!
is your process windowed?
if so you can enumerate all toplevel windows in the system and call for each
DWORD dwProcessID;
GetWindowThreadProcessId(hWnd, &dwProcessID);

to get process ID you can use ToolhelpSnapshot and enumerate all process in the system (you get ID). Next you call OpenProcess (dwID) and compare returned handle with your needed.

after all you have window handle and can send any message to it;
for example PostMessage(hWnd...)
0
 
jhanceCommented:
You don't send messages to a process, you send them to a window.  Processes don't process messages, WINPROCs do.

Have your process open a window, even a hidden one.  Then send messages to the window.

If you need to somehow communicate with a process that has no windows, you need to use an alternate method.  Mailslots, network sockets, events, etc.
0
 
danny_pavCommented:
RTFM about PostThreadMessage.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
svillalbaAuthor Commented:
I use the next code to get a handle to a process.

DWORD pids[1024], size ;

// Get pids
// ---------
if( !EnumProcesses( pids, 1024, &size ) )
   params[PARAM_ERROR] = ERR_KILL_PROC ;
else
{
//Get number of processes
// -----------------------
   DWORD num_procesos = size / sizeof( size ) ;
     
   TCHAR szProcessName[MAX_PATH] = _T("");
   HANDLE hProcess ;
   HMODULE hMod ;
   DWORD cbNeeded ;

   //Check names of processes
   // ------------------------
   for( int i = 0; i < num_procesos; i++ )
   {
      //Open process to query and terminating
      // -------------------------------------
      hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, FALSE, pids[i] ) ;

      if( hProcess )
      {
         //Get the module asociated to the process
         // ---------------------------------------
         if( EnumProcessModules( hProcess, &hMod, sizeof( hMod ), &cbNeeded ) )
         {
            //Get the name of the process
            // -----------------------------
            if( GetModuleBaseName( hProcess, hMod, szProcessName, sizeof( szProcessName ) ) )
            {
               //Kill the process
               // -----------------
               if( !_tcscmp( proceso, szProcessName ) )
                  TerminateProcess( hProcess, 1 )

               break ;
            }
         }  

         CloseHandle( hProcess ) ;

...

I would like to send a WM_CLOSE message to the process instead of using TerminateProcess, but I need to get the window's handle of the process in order to do it.
0
 
jhanceCommented:
>>RTFM about PostThreadMessage.

Perhaps YOU should RTFM!

1) A thread is NOT a process, it's a THREAD!
2) Not all threads have a message loop.  Only UI threads.
0
 
svillalbaAuthor Commented:
I forgot to comment that the process to be terminated has a window, so I need to get the hWnd to send a message to it.
0
 
migelCommented:
Hi!
So you want just close application :-) isn`t it?
here is completely exmaple how to achieve this:

   //******************
   //Header
   //******************

   #include <windows.h>

   #define TA_FAILED 0
   #define TA_SUCCESS_CLEAN 1
   #define TA_SUCCESS_KILL 2
   #define TA_SUCCESS_16 3

   DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout ) ;
   DWORD WINAPI Terminate16App( DWORD dwPID, DWORD dwThread,
                        WORD w16Task, DWORD dwTimeout );

   //******************
   //Source
   //******************

   #include "TermApp.h"
   #include <vdmdbg.h>

   typedef struct
   {
      DWORD   dwID ;
      DWORD   dwThread ;
   } TERMINFO ;

   // Declare Callback Enum Functions.
   BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ) ;

   BOOL CALLBACK Terminate16AppEnum( HWND hwnd, LPARAM lParam ) ;

   /*----------------------------------------------------------------
   DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )

   Purpose:
      Shut down a 32-Bit Process (or 16-bit process under Windows 95)

   Parameters:
      dwPID
         Process ID of the process to shut down.

      dwTimeout
         Wait time in milliseconds before shutting down the process.

   Return Value:
      TA_FAILED - If the shutdown failed.
      TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE.
      TA_SUCCESS_KILL - if the process was shut down with
         TerminateProcess().
      NOTE:  See header for these defines.
   ----------------------------------------------------------------*/
   DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
   {
      HANDLE   hProc ;
      DWORD   dwRet ;

      // If we can't open the process with PROCESS_TERMINATE rights,
      // then we give up immediately.
      hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE,
         dwPID);

      if(hProc == NULL)
      {
         return TA_FAILED ;
      }

      // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
      // matches your process's.
      EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;

      // Wait on the handle. If it signals, great. If it times out,
      // then you kill it.
      if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
         dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
      else
         dwRet = TA_SUCCESS_CLEAN ;

      CloseHandle(hProc) ;

      return dwRet ;
   }

   /*----------------------------------------------------------------
   DWORD WINAPI Terminate16App( DWORD dwPID, DWORD dwThread,
                        WORD w16Task, DWORD dwTimeout )

   Purpose:
      Shut down a Win16 APP.

   Parameters:
      dwPID
         Process ID of the NTVDM in which the 16-bit application is
         running.

      dwThread
         Thread ID of the thread of execution for the 16-bit
         application.

      w16Task
         16-bit task handle for the application.

      dwTimeout
         Wait time in milliseconds before shutting down the task.

   Return Value:
      If successful, returns TA_SUCCESS_16
      If unsuccessful, returns TA_FAILED.
      NOTE:  These values are defined in the header for this
      function.

   NOTE:
      You can get the Win16 task and thread ID through the
      VDMEnumTaskWOW() or the VDMEnumTaskWOWEx() functions.
   ----------------------------------------------------------------*/
   DWORD WINAPI Terminate16App( DWORD dwPID, DWORD dwThread,
                        WORD w16Task, DWORD dwTimeout )
   {
      HINSTANCE      hInstLib ;
      TERMINFO      info ;

      // You will be calling the functions through explicit linking
      // so that this code will be binary compatible across
      // Win32 platforms.
      BOOL (WINAPI *lpfVDMTerminateTaskWOW)(DWORD dwProcessId,
         WORD htask) ;

      hInstLib = LoadLibraryA( "VDMDBG.DLL" ) ;
      if( hInstLib == NULL )
         return TA_FAILED ;

      // Get procedure addresses.
      lpfVDMTerminateTaskWOW = (BOOL (WINAPI *)(DWORD, WORD ))
         GetProcAddress( hInstLib, "VDMTerminateTaskWOW" ) ;

      if( lpfVDMTerminateTaskWOW == NULL )
      {
         FreeLibrary( hInstLib ) ;
         return TA_FAILED ;
      }

      // Post a WM_CLOSE to all windows that match the ID and the
      // thread.
      info.dwID = dwPID ;
      info.dwThread = dwThread ;
      EnumWindows((WNDENUMPROC)Terminate16AppEnum, (LPARAM) &info) ;

      // Wait.
      Sleep( dwTimeout ) ;

      // Then terminate.
      lpfVDMTerminateTaskWOW(dwPID, w16Task) ;

      FreeLibrary( hInstLib ) ;
      return TA_SUCCESS_16 ;
   }

   BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
   {
      DWORD dwID ;

      GetWindowThreadProcessId(hwnd, &dwID) ;

      if(dwID == (DWORD)lParam)
      {
         PostMessage(hwnd, WM_CLOSE, 0, 0) ;
      }

      return TRUE ;
   }

   BOOL CALLBACK Terminate16AppEnum( HWND hwnd, LPARAM lParam )
   {
      DWORD      dwID ;
      DWORD      dwThread ;
      TERMINFO   *termInfo ;

      termInfo = (TERMINFO *)lParam ;

      dwThread = GetWindowThreadProcessId(hwnd, &dwID) ;

      if(dwID == termInfo->dwID && termInfo->dwThread == dwThread )
      {
         PostMessage(hwnd, WM_CLOSE, 0, 0) ;
      }

      return TRUE ;
   }
0
 
svillalbaAuthor Commented:
I have used TerminateAppEnum function and it works fine.
Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now