Trapping events in C# from hardware or DLL

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hello all,

Is there a way to trap incoming messages (like for example a swipe card
read) other then to do threadings in C#
Ofcourse I can build a native DLL in C++ to trap the event but how to parse
the event on to C# and trap it there?
Must I check every second or so if there is a swipe card read or is there a
better method to do this (like in C++ to handle it only when the event
occurs).
Any input on this will be very welcome.

Thanks Peter.
 
What device/sdk are you using? I would guess that the SDK for the
device in question most likely has an 'on-read' type event.

Chris
 
Hi Peter,
Ofcourse I can build a native DLL in C++ to trap the event but how to parse
the event on to C# and trap it there?

If everything else fails, create an instance of MessageWindow in managed
code and override the WndProc method, pass the handle to the native C++
DLL, and when the event occurs, send a message to that window. For example:

C#:
public class CMyMessageWnd : Microsoft.WindowsCE.Forms.MessageWindow
{
private const int WM_USER = 0x400;
private const int WM_MYEVENT = WM_USER + 0x001;
protected override void WndProc (ref Microsoft.WindowsCE.Forms.Message
msg)
{
switch (msg.Msg)
{
case WM_MYEVENT:
// event triggered
break;
}
base.WndProc(ref msg);
}
}

public static CMyMessageWnd MyMessageWnd;
public static void HookUp()
{
MyMessageWnd = new CMyMessageWnd();
HookUp(MyMessageWnd.Hwnd);
}
[DllImport("MyDLL.dll")]
internal static extern void HookUp(IntPtr hWnd);

C++:
__declspec(dllexport) void HookUp(HWND wnd)
{
// store the handle somewhere, say
myGlobalHWND = wnd;
}

// and wherever your event occurs:
if (myGlobalHWND != NULL)
SendMessage(myGlobalHWND, 0x401, 0, 0);

As far as I know, it's the most elegant way to have unmanaged code
notifying CF code.

Let me know if this helps.

Kind Regards,

Roland
 
Back
Top