LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Using .Net dll (having delegates in function parameters) in CVI application

Hi,

 

I have to call the functions of .Net 3.5 dll from CVI application 8.0. So by using the .Net controller I have created a wrapper of .net dll in CVI application and now i am able to call the functions (having simple parameters) of .Net dll successfully from CVI application but some of the functions in the .net dll contains complex parameters such as delegates and structures containing delegates as member.

 

The .Net controller has created certain functions for each delegate defined in the .net dll but I am not able to figure out how to use the delegate object in CVI application to pass it as parameter to .net dll functions.

 

Could you please confirm whether the "delegates passed as function parameter" and "delegate as structure member" are supported in Labwindow/CVI 8.0? If yes then could you please provide any sample CVI application showing this functionality?

 

Thanks,

Sarabjit

0 Kudos
Message 1 of 6
(3,839 Views)

Basically, CVI treats .NET delegates just like any other .NET object. You can create a delegate object using the appropriate Create wrapper function for that delegate - this returns a 'handle' that represents the delegate object. You can pass delegates to .NET method calls or store them in struct fields, etc by passing the delegate object's 'handle' to the CVI wrapper functions for the .NET method or struct field getter/setter.

 

There is a shipping CVI sample that shows how to use .NET delegates. This sample is samples/dotnet/timer/timer.prj. This sample creates a TimerCallback delegate by calling the System_Threading_TimerCallback__Create C function that wraps the System.Threading.TimerCallback delegate constructor. Then it passes this delegate handle to the System_Threading_Timer__Create function that wraps the System.Threading.Timer constructor to construct a timer object.

 

0 Kudos
Message 2 of 6
(3,835 Views)

Thanks Mohan for the reply.

 

I am facing issues while creating the handle for the delegate in CVI application.

 

The following is the signature of the delegate in C#:

 

public delegate void BTTEST_NOTIFY_CB();

 

and following is the wrapper of this delegate in CVI application

 

int CVIFUNC BTTest_BTTEST_TRANSFER_CB__Create(BTTest_BTTEST_TRANSFER_CB * __instance,  CDotNetHandle object,   void * method, CDotNetHandle * __exception)                                   

 

 

So when the user will call this create method, what values will be passed to the following parameters:

 

1)  CDotNetHandle object

2)  void * method


 

The sample "samples/dotnet/timer/timer.prj." is not available in CVI8.0 installation. If you have this sample then can you please send it to me?

 

 

Thanks,

Sarabjit Singh

0 Kudos
Message 3 of 6
(3,823 Views)

Ah, the .NET delegate and event support is only available in CVI 8.5 and higher - this feature was introduced in CVI 8.5. This is why you do not see the timer sample in 8.0, and the generated delegate wrapper function is not really useful. If you have access to CVI 8.5 or higher, then use that to generate and call the wrapper.

0 Kudos
Message 4 of 6
(3,815 Views)

Thanks for the reply .

 

Actually I have only CVI 8.0. I have to provide callback functionality in .Net dll which i was trying by passing delegate as function parameter. If delegate is not supported in CVI 8.0 then can I use AsyncCallback parameter?  I have created .Net wrapper for a dll containing AsyncCallback as parameter. The function signature in wrapper class is:

 

int CVIFUNC TestDotNet_TestClass_TestCallbackInCVI(TestDotNet_TestClass __instance,   char * file,  System_AsyncCallback completedCallback, char ** __returnValue,  CDotNetHandle * __exception)           

Will CVI 8.0 support AsyncCallback parameter in wrapper function. If yes, can you please provide any example for the same?

 

Thanks and Regards,

Sarabjit Singh

0 Kudos
Message 5 of 6
(3,807 Views)

Hi Sarabjit,

 

System.AsyncCallback is also a delegate, and so, you cannot use it from CVI 8.0. Basically, I think you want to have some CVI code called from your .NET code using delegate/event/callback mechanism. Unfortunately, calling from .NET to CVI like this is not possible in 8.0. There are two other options:

 

1) Polling: From your CVI code you can poll for the .NET 'event' condition by repeatedly calling a .NET method in your .NET assembly and when the event is signalled, then you can take the appropriate action in your CVI code.

 

2) P/Invoke: In .NET, you can call any DLL using the PInvoke (Platform Invoke) technology. So, in your .NET code, you can set up the delegate, etc, and then when your .NET delegate/callback gets called, it can then use PInvoke and call a DLL built in CVI. Note that PInvoke can only call DLLs, and so, you would need an extra DLL to use this method.

 

The above are the only two options I can see for calling back from .NET in CVI 8.0.

0 Kudos
Message 6 of 6
(3,802 Views)