无法使用pinvoke将WM_CLOSE发送到Windows资源管理器窗口

我有一个C#应用程序,它使用SendMessage pinvoke方法向应用程序外的各个窗口发送“关闭窗口”消息(WM_CLOSE / 16)。 这很有用,除非有问题的窗口是Windows资源管理器窗口。 我没有exception,但窗口没有关闭。

这是签名:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] internal static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam); 

我需要将不同的消息发送到Windows资源管理器窗口吗? 或另一种方法来实现这一目标?

另一种解决方案是使用PostMessage win API调用而不是SendMessage,下面是一个适合我的例子(我正在使用winXP sp3):

 [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.Dll")] public static extern int PostMessage(IntPtr hWnd, UInt32 msg, int wParam, int lParam); private const UInt32 WM_CLOSE = 0x0010; ... IntPtr hWnd = FindWindow("ExploreWClass", null); if (hWnd.ToInt32()!=0) PostMessage(hWnd, WM_CLOSE, 0, 0); 

PostMessage和SendMessage api调用之间的差异在这里描述: http : //msdn.microsoft.com/en-us/magazine/cc301431.aspx