Synchronization with DLL
Hi!
I'm having some problems with synchronization between a .NET cf application and a native DLL. This is the idea:
// The .NET application
//
public class MyClass
{
public static void Main(String[] args)
{
ManualResetEvent evt = new ManualResetEvent(false);
evt.Handle = CreateDLLSynchronizationEvent();
while(true)
{
evt.WaitOne(); // This returns false immediatelly
evt.Reset();
}
}
[DllImport("MyDll.dll", EntryPoint="CreateDLLSynchronizationEvent")]
private static extern IntPtr CreateDLLSynchronizationEvent();
}
// The DLL
//
extern "C" __declspec(dllexport) HANDLE CreateDLLSynchronizationEvent()
{
return ::CreateEvent(NULL, true, false, NULL);
}
This code works in desktop framework, but not in compact framework. In cf, ManualResetEvent.WaitOne returns false immediatelly, which means that I can't synchronize with the DLL. What am I doing wrong?
Thanks in advance,
Nille
The reason your code does not work on NETCF is because managed event handles are different than native event handles because of an abstraction layer in between. Desktop does not need this abstraction layer since they run only on Win32 platforms while we may not be running on a single platform.
If what you are trying to achieve is notifying managed code from native code, then read on below ... else fill me in with what your scenario is.
You have 2 options here:
1) Use delegate callback.
2) Use PInvokes to [Create/Set/Reset]Event and WaitForSingleObject from managed code.
Option 1 (Recommended):
Create a delegate with a callback in managed code and pass it down to native code as a function pointer. When it is time to "fire the event" in native code, just invoke the delegate ... which will in-turn execute the managed callback.
Option 2:
PInvoke to WaitForSingleObject and use the handle you are getting from native code.
If you need code snippets or samples for either of these, ping me.
Thanks.