如何使用C#应用程序监听通过USB连接的设备消息? [英] How do I listen a device's messages conected via USB with a C# application?
问题描述
问候!这又是我了!
这个问题很难,因为我会尽力解释它:正如我在上一个问题中所提到的,我正在努力C#上的扫描程序管理,使用提供程序发送的C ++ DLL。根据API的手册,在某些条件下会发送某些消息。例如:启动扫描程序后,它应发送消息DEVICE_CONNECTED(值为0),然后更改状态。
这些消息值已定义在.dll
我的问题是:如何用我的C#项目捕获这些消息?
我尝试了什么:
我一直在寻找有关邮件传输的信息,我发现有一个处理Windows邮件的WndProc。然后我尝试这样做:
Greetings! it's me again!
This question is quite hard, for I'll do my best explaining it: As I mentioned in a previous question, I'm working in a scanner management on C#, using a C++ dll sent by provider. According to the API's manual, there are certain messages sent under certain conditions. In example: After Starting up the scanner, it should send the message DEVICE_CONNECTED (with a value of 0), and then change it state.
Those messages values are defined in the .dll
My question is: How can I catch those messages with my C# project?
What I have tried:
I been looking for information about messages transfering, and I found out there's a WndProc that processes Windows messages. Then I tried doing this:
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);
}
当然失败了。我想我需要以某种方式定义消息源。进一步阅读还有一个叫WMI的东西,但是在查看了MicrosoftDocs的信息之后...我觉得他们用中文或者其他东西说话......
所以任何好处guindance?建议?
提前致谢
///// //////////////////////////////
编辑:
好了,经过长时间的研究,我发现了哪里可以收到消息。
我进口了以前的另一个函数形式:dll:
Of course it fails. I suppose I need to define, somehow, the messages source. Reading further there's another thing called WMI, but after checking the info at MicrosoftDocs... I feel they're talking in chinese or something...
So any good guindance? Suggestions?
Thanks in advance
///////////////////////////////////
Edited:
OK, after a long research, I found out where can I possibly get the message.
I imported previously another function form that dll:
[DllImport(path, EntryPoint = "?StartUp@@YGKPAUHWND__@@I@Z")]
public static extern int StartUp(IntPtr HWMD, uint StMsg);
那个允许我打开扫描仪。
.dll里面的
定义如下:
That one allows me to turn on scanner.
inside of the .dll is defined like this:
DWORD StartUp( HWND Handle, UINT SorterMessage )
在手册中说:HWND Handle - 应用程序消息目标窗口的句柄。
所以,如果我认真对待(英语不是我的主要语言)我有一个指针,我应该从哪里得到这些消息。
现在又来了另一个问题:怎么能我用那个指针?
试过这个例子我在另一个论坛找到了:
And in the manual says: "HWND Handle – the handle to the application’s messages destination window."
So, if I undertstood correrctly (english is not my main language) I got a pointer from where I should get those messages.
Now here comes another question: How can I use that pointer?
Tried following this example I found in another forum:
[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);
试图仿效它:
Tried to emulate it like this:
// 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");
但是现在我得到一个空洞的回复。打印打开设备后,光标只是闪烁,程序没有前进。这就像在等待什么。
我缺少什么?
But now I'm getting an empty response. After printing "Turning on device", cursor just blinks and program doesnt advance. It's like is waiting for something.
What am I missing?
推荐答案
解决了它(最后)
我就这样做了:
1)二手windows表单,因为它有消息类
2)导入.dll我正在努力使事情变得更容易,将所有方法放在ScanMgr类中。
Solved it (finally)
This is how I did it:
1) Used windows forms, since it has the class "Message"
2) Imported the .dll I was working on to make stuff easier, placed all methods in a "ScanMgr" class.
using ...
using APIcsharp;
class ScanMgr
{
int ID = 0;
public string startUp(IntPtr hwmd, uint wmApp)
{
int state;
ID = NativeAPI.StartUp(hwmd, wmApp);
if(ID != 0)
{
do { NativeAPI.GetState(ID, out state); }
while(state == (int)(DevStates.StartingUp)); // DevStates is a enum, ok? :P
return "Device on";
}
return "Error turning on";
}
/* Other stuff to do */
}
3)然后,为消息定义覆盖方法
3) Then, defined an override method for the messages
public partial class Form1 : Form
{
const uint wm_channel = 0x8000 + 1;
ScanMgr scanner = new ScanMgr();
public Form1()
{ InitializeComponent(); }
private void StartBtn_Click(object sender, EventArgs e)
{ log.Items.Add(scanner.startUp(this.Handle, wm_channel)); }
/* Other stuff yadda yadda */
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if(m.Msg == wm_channel)
{ /* To do stuff with m.WParam and m.LParam */ }
}
}
这篇关于如何使用C#应用程序监听通过USB连接的设备消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!