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.
- From: "Mohamed Mansour" <m0@xxxxxxxxxxxxxxxx>
- Date: Sun, 9 Dec 2007 13:38:52 -0500
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.
Wiily
Hi,
I don't know what you mean live reference ? I declare the delegate in the same static class of the Interop Wrapper.
Your reasoning makes sense, but how do I use that reasoning in my code? How do I make a reference to the delegate if the delegate is public and declared inside the Interop Class.
public static class MyDevice
{
......
// Here is the delegate ... It is declared here
public delegate uint SchedulerCallback(IntPtr pData);
[DllImport("mydevice", EntryPoint = "#22", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern uint deviceScheduleAsynchronous(SchedulerCallback pCallback, IntPtr pData, ushort nPriority);
.....
}
And I use it like the following ...
main()
{
uint deviceID = MyDevice.Init();
uint schedulerID = MyDevice.deviceAsynchronous(MyDataCallback, (IntPtr)0, MyDevice.DEFAULT_SCHEDULER_PRIORITY);
MyDevice.Start();
}
private uint MyDataCallback(IntPtr data)
{
float[] pos = new float[2];
MyDevice.GetPosition(pos);
Console.WriteLine("{0} {1}",pos[0],pos[1]);
}
What the above does, it turns on the device and starts it, and it will print out the data in every iteration of the loop always asynchonously since it is within a thread. (The C++ API)
So what do you mean live reference? The delegate is created in the same class as a public delegate of the Interop class. Is that wrong?
Thanks for your help, any more help is appreciated.
--
Regards,
Mohamed Mansour
Microsoft Student Partner
"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:eQe3tqoOIHA.5208@xxxxxxxxxxxxxxxxxxxxxxx
"Mohamed Mansour" <m0@xxxxxxxxxxxxxxxx> wrote in message news:027BC83A-D030-4A15-B6EE-746CD430E506@xxxxxxxxxxxxxxxxHey 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
.
- Follow-Ups:
- References:
- 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.
- From: Mohamed Mansour
- 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.
- From: Willy Denoyette [MVP]
- 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.
- Prev by Date: Re: Using SQlite in c#
- Next by Date: Re: A dead process
- Previous by thread: 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.
- Next by thread: 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.
- Index(es):
Relevant Pages
|