Tuesday, October 19, 2004

Post a message to a non-.Net window

For anyone who is interested, this is how you Post a message to a window from .net (C#). You can always pass in null for either the calssName or windowName if you don't wish to use them to find the window.

using System;
using System.Runtime.InteropServices;

namespace WinUtilities
{
class WinUtil
{
public static int PostMsg(string className, string windowName, int wMsg, int wParam, int lParam)
{
int hWnd = FindWindow(className, windowName);
int nRet = 0;

if (hWnd != 0)
{
nRet = PostMessageA(hWnd, wMsg, wParam, lParam);
}

return nRet;
}

[DllImport("user32", EntryPoint="PostMessage")]
public static extern int PostMessageA(int hwnd, int wMsg, int wParam, int lParam);

[DllImport("user32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string _ClassName, string _WindowName);

}
}


A useful interop resource is PInvoke

No comments:

Post a Comment