Soumya Chiniwar
Soumya Chiniwar

Reputation: 55

Get outlook Active window handle hwnd in C#

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..

Upvotes: 2

Views: 3822

Answers (3)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66306

Cast the Inspector object to the IOleWindow interface and call IOleWindow::GetWindow. The same will work for the Explorer object.

Upvotes: -1

CathalMF
CathalMF

Reputation: 10055

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);

Upvotes: 2

VitaliyK
VitaliyK

Reputation: 299

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;

Upvotes: 1

Related Questions