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;
[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);
}
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);
That one allows me to turn on scanner.
inside of the .dll is defined like this:
DWORD StartUp( HWND Handle, UINT SorterMessage )
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:
public MSG(IntPtr hwndPtr)
{
hwnd = hwndPtr;
message = -1;
wParam = new IntPtr();
lParam = new IntPtr();
time = 0;
pt_x = 0;
pt_y = 0;
}
int ID, st;
ID = Class1.StartUp(hwnd, 10);
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?