如何收听设备';C#项目中的s消息
又是我 这个问题很难,因为我会尽力解释它:正如我在前一个问题中提到的,我在C++的一个扫描器管理中工作,使用了一个由提供者发送的C++ DLL。根据API手册,在某些条件下会发送某些消息。例如:启动扫描仪后,应发送消息“设备已连接”(值为0),然后更改其状态 这些消息值在.dll中定义 我的问题是试图在我的C#项目中获得这些信息 我一直在寻找有关邮件传输的信息,发现有一个WndProc处理Windows邮件,因为我尝试了以下示例:如何收听设备';C#项目中的s消息,c#,c++,windows,message,handle,C#,C++,Windows,Message,Handle,又是我 这个问题很难,因为我会尽力解释它:正如我在前一个问题中提到的,我在C++的一个扫描器管理中工作,使用了一个由提供者发送的C++ DLL。根据API手册,在某些条件下会发送某些消息。例如:启动扫描仪后,应发送消息“设备已连接”(值为0),然后更改其状态 这些消息值在.dll中定义 我的问题是试图在我的C#项目中获得这些信息 我一直在寻找有关邮件传输的信息,发现有一个WndProc处理Windows邮件,因为我尝试了以下示例: private const int DEVICE_CONNECT
private const int DEVICE_CONNECTED = 0;
/*Some code*/
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
if (m.Msg == DEVICE_CONNECTED)
listBox1.Items.Add("Connected");
base.WndProc(ref m);
}
当然,那一次失败了
后来,我一直在查看API手册,我想我找到了一个线索,可以从哪里获得消息:
// This is how is defined at .dll (C++)
DWORD StartUp( HWND Handle, UINT SorterMessage )
其中“Handle”是应用程序消息目标窗口的句柄
因此,我的C#导入如下:
[DllImport(path, EntryPoint = "?StartUp@@YGKPAUHWND__@@I@Z")]
public static extern int StartUp(IntPtr HWMD, uint SorterMessage);
// Added a constructor inside of the struct:
public MSG(IntPtr hwndPtr)
{
hwnd = hwndPtr;
message = -1;
wParam = new IntPtr();
lParam = new IntPtr();
time = 0;
pt_x = 0;
pt_y = 0;
}
// Left the dll imports like in their example (although I fixed the path)
// Calling the method in my main
int ID, st;
ID = Class1.StartUp(hwnd, 10); // Just used 10 like in the API's manual
Console.WriteLine("Turning on device");
MSG msg = new MSG(hwnd);
while(Class1.GetMessage(ref msg, IntPtr.Zero, 0, 0))
Class1.DispatchMessage(ref msg);
Console.WriteLine(msg.message);
do { Class1.GetState(ID, out st); }
while (st != (int) DevStates.chgParams);
Console.WriteLine("Device on");
现在我得到了一个指针,可以从中提取消息。我的问题是:怎么做
我在另一个论坛上发现了这个例子:
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct MSG
{
public IntPtr hwnd;
public int message;
public IntPtr wParam;
public IntPtr lParam;
public int time;
public int pt_x;
public int pt_y;
};
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool GetMessage([In, Out] ref MSG msg, IntPtr hWnd, int uMsgFilterMin, int uMsgFilterMax);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr DispatchMessage([In] ref MSG msg);
MSG msg = new MSG();
while (GetMessage(ref msg, IntPtr.Zero, 0, 0))
DispatchMessage(ref msg);
我尝试使用它,如下所示:
[DllImport(path, EntryPoint = "?StartUp@@YGKPAUHWND__@@I@Z")]
public static extern int StartUp(IntPtr HWMD, uint SorterMessage);
// Added a constructor inside of the struct:
public MSG(IntPtr hwndPtr)
{
hwnd = hwndPtr;
message = -1;
wParam = new IntPtr();
lParam = new IntPtr();
time = 0;
pt_x = 0;
pt_y = 0;
}
// Left the dll imports like in their example (although I fixed the path)
// Calling the method in my main
int ID, st;
ID = Class1.StartUp(hwnd, 10); // Just used 10 like in the API's manual
Console.WriteLine("Turning on device");
MSG msg = new MSG(hwnd);
while(Class1.GetMessage(ref msg, IntPtr.Zero, 0, 0))
Class1.DispatchMessage(ref msg);
Console.WriteLine(msg.message);
do { Class1.GetState(ID, out st); }
while (st != (int) DevStates.chgParams);
Console.WriteLine("Device on");
我期待什么?打印“打开设备”后,我应该得到消息(因为在启动期间,根据手册,它会在更改状态之前发送消息),然后是“设备打开”字符串
我能得到什么?打印“打开设备”后,程序只会闪烁光标(当然,“设备打开”字符串永远不会显示)。看起来它正在等待任何消息。尝试将消息呼叫放置在不同的位置,行为相同
有什么建议吗?提前谢谢。解决了(最后)
我就是这样做的:
您希望人们能帮助您解决未知API的问题吗?至少我想知道它的名字。虽然我需要帮助,但它的名字与INTPTR(C++中的HWMD改写成Windows库)是不相关的。该Api适用于VisionX扫描仪