Re: Would C++/CLI solve PInvoke problems that C# has in this situation? What is the advantages using C++/CLI then C# when PInvoking umanaged C++ Code. Major problems here.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Mohamed Mansour" <m0@xxxxxxxxxxxxxxxx> wrote in message news:027BC83A-D030-4A15-B6EE-746CD430E506@xxxxxxxxxxxxxxxx
Hey there, this will be somewhat a long post, but any response is appreciated!

I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI.
Can someone explain more why C++/CLI would be better to PInvoke than doing the PInvoke in
C#?

Because, usually in C# as you already know we use DLLImport and extern functions. Last time
when I was PInvoking a robotics library in my Lab, I had issues PInvoking an "Asynchronous"
method call. Before I go into that, I will explain two methods that I was PInvoking in the past.

I already finished the .NET Wrapping and already did many helper classes to make a .NET
framework in that library but I used a small hack which is not comfortable but all other functions
work great except the one I am having problems.

The two methods that are similar:

deviceScheduleSynchronous = Which polls synchronously ONCE to the device and executes a call
back to a method that allows me to get any data I want. THIS WORKS

deviceScheduleAsynchronous = Which polls asynchronously ALWAYS IN A LOOP to the device
and executes the callback within everyloop. This keeps on running since it spawns a new thread
whenever I execute it. This doesn't work, It crashes, Unexpected crash, with no valid exception.

Both of the methods have the exact same parameters but different return types:
long deviceScheduleSynchronous (SchedulerCallback pCallback, void *pData, unsigned short nPriority)
void deviceScheduleAsynchronous (SchedulerCallback pCallback, void *pData, unsigned short nPriority)

To make that work, I created my own Thread and loop, and used the deviceScheduleSynchronous to poll
the device, and it worked flawlessly.

BUT I really want to learn why deviceScheduleAsynchronous didn't work. I am wondering what
caused the not usefull, not informative exception to occur. Both deviceScheduleSynchronous, and
deviceScheduleAsynchronous has the same parameter types, but only one of them worked.

I was going to wrap another device, and it only had a deviceScheduleAsynchronous method, but an
exception occurs right after the first loop, once again an exception which doesn't make sense. So I
cannot do the same hack I did before cause there is no single polling.

// C/C++ Interops Thats how it is defined
//
// typedef DeviceCallbackCode (__stdcall *SchedulerCallback)(void *pData);
// __declspec(dllimport) unsigned long __stdcall deviceScheduleAsynchronous(SchedulerCallback pCallback, void *pData, unsigned short nPriority);
//
// C# PInvoke
// public delegate uint SchedulerCallback(object pData);
// [DllImport("robod", EntryPoint = "#22", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
// public static extern void deviceScheduleAsynchronous(SchedulerCallback pCallback, IntPtr pData, ushort nPriority);

Once again, it works for One Method, but doesn't work with the other. Difference between the too is:
deviceScheduleSynchronous : access Device -> GrabData -> Done (does this using the current thread since its synchronous)
deviceScheduleAsynchronous : start infinite loop -> GrabData -> GoBack (does this in the thread since it is asynchronous)


So is it my coding problem or is it the manufacturers dll problem? I have done this with two different manufacturers. It works
great with JAVA using JNI without any problem. Would C++/CLI solve this issue? Should I go back in the lab and attempt it again?

Thanks!



--
Regards,
Mohamed Mansour
Microsoft Student Partner

--
Regards,
Mohamed Mansour
Microsoft Student Partner


Are you sure you keep a *live* reference for your delegate instance? Note that the JIT has no idea that a *foreign* thread might call the delegate target asynchronously and prematurely signal to the GC that the delegate is free to be collected.

Willy.


Willy.

.



Relevant Pages