I want to get the outlook Active window handle (hwnd) in C#. I want to use this for SendMessage() method which takes hwnd as first parameter. Outlook is open and not minimized. Tried to do like this.. dynamic winHwnd= Globals.ThisAddIn.Application.ActiveWindow(); Not working as type mismatches . Even if i convert it doesn't work. Could some one suggest me to get this handler..
Asked
Active
Viewed 3,297 times
2
-
Are you doing this from within your plugin which has been loaded by Outlook or by some other external application? – CathalMF Apr 22 '16 at 08:08
-
Yes . I am doing within my outlook add-in which is been loaded by outlook.I have a button the the ribbon . this button is a drop down button and i am able to get the control of the button but not able to execute. So i thought i can perform a mouse click i.e WM_LBUTTONDOWN in sendmessage() method. For that i want the outlook handler. – Soumya Chiniwar Apr 22 '16 at 08:11
3 Answers
2
You probably want to use the GetActiveWindow api function.
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
IntPtr handle = GetActiveWindow();
Try and minimize the window to see if you are getting the correct handle.
private const int SW_SHOWMINIMIZED = 2;
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow)
ShowWindowAsync(hWnd, SW_SHOWMINIMIZED);

CathalMF
- 9,705
- 6
- 70
- 106
-
I did this. When i debug handle has some value but when i send this handle in SendMessage() method to click the button it is not happening. Do i have to take to button control also which is in ribbon. – Soumya Chiniwar Apr 22 '16 at 08:52
-
@SoumyaChiniwar Updated answer on how to minimise a window based on the handle received. You can use this to test if you have received the correct handle. – CathalMF Apr 22 '16 at 09:02
-
Yes i tried with what you said. It is having the correct handler. It is minimizing the window. I want to click on button that is on ribbon. Can you suggest me how to i perform that. – Soumya Chiniwar Apr 22 '16 at 11:19
1
Maybe you could try to use generic approach by using system Api FindWindow to get the window you interested by it's name:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr hWnd = (IntPtr)FindWindow(windowName, null);
From ActiveWindow you could try:
dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow();
IntPtr outlookHwnd = new OfficeWin32Window(activeWindow).Handle;

VitaliyK
- 279
- 1
- 8
-
I don't have to find window because we already have handler. Is there a way to do with ActiveWindow() which actually has the outlook handler? – Soumya Chiniwar Apr 22 '16 at 08:28
-
I tired this i don't find OfficeWin32Window . Instead i used ' IntPtr windowHandle = new IWin32Window(win).Handle;' which is throwing me error --- "Cannot create an instances of abstract class". – Soumya Chiniwar Apr 22 '16 at 08:42
-
try implementation from here: https://github.com/JakeGinnivan/VSTOContrib/blob/master/src/VSTOContrib.Core/OfficeWin32Window.cs – VitaliyK Apr 22 '16 at 08:43
-
I already have a add-in . Inside which i am getting the control of a button using redemption. Thing is it is a drop down item and i cannot perform execute() on it. I want to do mouse click using WM_LBUTTONDOWN. – Soumya Chiniwar Apr 22 '16 at 11:05
-1
Cast the Inspector object to the IOleWindow interface and call IOleWindow::GetWindow. The same will work for the Explorer object.

Dmitry Streblechenko
- 62,942
- 4
- 53
- 78
-
I am getting error while doing `dynamic activeWindow = Globals.ThisAddIn.Application.ActiveWindow(); IOleWindow win = activeWindow as IOleWindow; window = win.GetWindow();` getting error at IOleWindow. Do i have to import anything? – Soumya Chiniwar Apr 22 '16 at 15:09
-
Compile error or run-time error? What is it? You can either get the IOleWindow interface definition from http://www.getcodesamples.com/src/3A684D44/2233A502 or add Microsoft.VisualStudio.OLE.Interop to the uses clause. – Dmitry Streblechenko Apr 22 '16 at 15:17
-
-
-
Yeah i have added the interface its not working. the variable has null value – Soumya Chiniwar Apr 25 '16 at 09:59
-
` dynamic winHandle = Globals.ThisAddIn.Application.ActiveWindow(); IOleWindow handle = winHandle as IOleWindow; SendMessage(handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero); ` handle has null value when i debug and check. – Soumya Chiniwar Apr 25 '16 at 15:07
-
I have taken control of my drop down button. It is pooping up in window but not executing as it is not visible to API. Do i have to take handle of that button also? – Soumya Chiniwar Apr 25 '16 at 15:10
-
-
What is not working? It returns an error? You get back 0? SendMessage does not behave as expected? – Dmitry Streblechenko Apr 26 '16 at 13:55