none
send message to outlook RRS feed

  • Question

  • Hi,

    I get the outlook's window hwnd and then I post a wm_close message to it, but it does no response.

    I am using PostMessage(hWnd, WM_CLOSE, 0, 0);

    And if I use SendMessage(hWnd, WM_CLOSE, 0, 0), the function will not excute successfully and the value of GetLastError() is 122.

    Saturday, March 16, 2013 9:45 AM

Answers

  • Try to cast the main Outlook Explorer (Application.ActiveExplorer) to IOleWindow and call IOleWindow::GetWindow() to retrieve the handle.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by SaraZC Wednesday, July 31, 2013 2:33 AM
    Wednesday, March 20, 2013 1:09 PM

All replies

  • Error 122:The data area passed to a system call is too small.

    So try to figure out where is the inadequate buffer. And post your code out.


    Reply, then visible.

    Monday, March 18, 2013 8:39 AM
  • what are you trying to achieve in broader sense?
    Monday, March 18, 2013 9:27 AM
  • I want to send keyboard shortcuts to outlook, so it can excute the propery fuction.
    Monday, March 18, 2013 10:25 AM
  • How did you retrieve hWnd?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, March 18, 2013 2:52 PM
  • ok, show us how you get hold of hwnd and when you invoke your Post message
    Monday, March 18, 2013 2:56 PM
  • // DemoForGetProcessWindowHwnd.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    
    DWORD FindProcess(TCHAR *strProcessName)
    {
    	DWORD aProcesses[1024], cbNeeded, cbMNeeded;
    	HMODULE hMods[1024];
    	HANDLE hProcess;
    	TCHAR szProcessName[MAX_PATH];
    
    	if (!EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )  return 0;
    	for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
    	{
    		hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
    		EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);
    		GetModuleFileNameEx( hProcess, hMods[0], szProcessName, sizeof(szProcessName));
    
    		CString szName=szProcessName; 
    		CString strName=strProcessName;
    		szName=szName.Right(strName.GetLength()+1);
    		szName.MakeLower();
    		strName.MakeLower();
    		if(szName=="\\"+strName)
    		{
    			return(aProcesses[i]);//0x0109ed98
    		}  
    	}
    	return 0;
    }
    
    // get hwnd with the given process id 
    HWND GetWindowHandleByPID(DWORD dwProcessID)
    {
    	HWND h = ::GetTopWindow(0);
    	while (h)
    	{
    		DWORD pid = 0;
    		DWORD dwTheardId = GetWindowThreadProcessId( h,&pid);
    		if (dwTheardId != 0)
    		{
    			if ( pid == dwProcessID/*your process id*/ )
    			{
    				// here h is the handle to the window
    				while (::GetParent(h)!=NULL)
    					h=::GetParent(h);
    				return h;
    			}
    		}
    		h = ::GetNextWindow(h, GW_HWNDNEXT);
    	}
    	return NULL;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	HWND hWnd = NULL;
    	hWnd = GetWindowHandleByPID(FindProcess(L"outlook.exe"));
    	BOOL bIsDone = FALSE;
    	if (hWnd)
    	{
    		wcout << L"find ok" << endl;
    		bIsDone = SendMessage(hWnd, WM_CLOSE, 0, 0);
    		//LPARAM lParam = MAKELONG(MOD_CONTROL,'N');
    	 //   bIsDone = PostMessage(hWnd, WM_HOTKEY, 0, lParam);
    		if (bIsDone)
    		{
    			wcout << L"send ok" << endl;
    		}
    		else
    		{
    			wcout << GetLastError() << endl;
    		}
    	} 
    	else
    	{
    		wcout << L"no" << endl;
    	}
    	return 0;
    }
    


    Wednesday, March 20, 2013 6:29 AM
  • Try to cast the main Outlook Explorer (Application.ActiveExplorer) to IOleWindow and call IOleWindow::GetWindow() to retrieve the handle.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by SaraZC Wednesday, July 31, 2013 2:33 AM
    Wednesday, March 20, 2013 1:09 PM