在C#中用WIN32API调用WINDOWS消息失败
下面是我写的一段程序,其过程是:利用WIN32API调用WINDOWS消息,给一个已经运行的应用程序(MHS—UA),MHS—UA收到消息后,将读取指定的参数,作出相应的动作,如参数有误,也给出相应的错误号。但不成功,
如用POSTMESSAGE则不出错,如用SENDMESSAGE则出错(发送不成功),如用SENDMESSAGETIMEOUT,则返回消息是对的,但应用系统(MHS—UA)不作任何动作,请各位指点。
public class Win32API
{
[DllImport("User32.dll",CharSet=CharSet.Auto)]
public static extern uint SendMessage( uint hWnd, uint Msg, uint wParam,uint lParam );
[DllImport("User32.dll",CharSet=CharSet.Auto)]
public static extern bool IsWindowEnabled(long hwnd);
[DllImport("User32.dll",CharSet=CharSet.Auto)]
public static extern uint RegisterWindowMessage(ref string String);
[DllImport("User32.dll",CharSet=CharSet.Auto)]
public static extern bool PostMessage(uint hWnd,uint Msg,uint wParam,uint lParam);
[DllImport("User32.dll",CharSet=CharSet.Auto)]
public static extern uint SendMessageTimeout(uint hwnd,uint Msg,uint wParam,uint lParam,uint Flags,uint Timeout,ref uint lpdwResultult);
}
//取得应用程序的句柄
RegistryKey SubKeya;
RegistryKey LKM= RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine,"");
string subkey1 ="SOFTWARE\\MHS_UA";
SubKeya = LKM.OpenSubKey(subkey1);
uint WdHandle;
object rev= SubKeya.GetValue("HANDLE");
//WdHandle = long.Parse(rev.ToString());
WdHandle = System.Convert.ToUInt32(rev);
//测试应用程序是否启动
bool TF=Win32API.IsWindowEnabled(WdHandle);
if (TF==false)
MessageBox.Show("系统未启动!");
//注册消息号
uint ifreg;
string m1="1424";
ifreg=Win32API.RegisterWindowMessage(ref m1);
MessageBox.Show(ifreg.ToString());
bool ret;
ret=Win32API.InSendMessage();
MessageBox.Show(ret.ToString());
uint Retvalue1=0;
//1424是约定的消息号
Retvalue1= Win32API.SendMessage(WdHandle,1424,384,0);
MessageBox.Show(Retvalue1.ToString());
uint Retvalue1,resu=10;
Retvalue1=Win32API.SendMessageTimeout(WdHandle,1424,384,0,0,6000, ref resu);
MessageBox.Show(resu.ToString());
MessageBox.Show(Retvalue1.ToString());