LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

call dll from Visual Basic 2005 in CVI

Hello!
I have a dll exported form Visual Basic 2005 and I want to use it in CVI. How can I do this?
The dll is created from a Class Library project in VB2005.
Thank you.
0 Kudos
Message 1 of 10
(6,645 Views)
Howdy aliasdealias,

LabWindows/CVI 8.0 and greater provide several options for working with .NET assemblies.  You can use the .NET Library to invoke methods and set/get properties from .NET assemblies.  Additionally, LabWindows/CVI also provides the Create LabWindows/CVI .NET Controller feature, which you can use to generate a LabWindows/CVI wrapper for a .NET assembly which is the in form of an instrument driver, source file, and header file.  You also can use the .NET Library functions in conjunction with the generated wrapper.

Refer to the LabWindows/CVI shipping examples located in the
<CVI>\samples\dotnet\ directory for some examples. Also check out the .NET Library help (Open the LabWindows/CVI Help, switch to the Contents tab and expand Library Reference >> .NET Library. These sections go into great detail on working with .NET assemblies.)

Hope this helps!

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 2 of 10
(6,627 Views)
Thanks for your reply,
so will I be needing a header .h file if I use the Create LabWindows/CVI .NET Controller feature? The dll is exported by VB2005 and it has no header file.
I tried creating a .NET Controller and it seems to import the dll but when I try to actually run it crashes with error:

 NON-FATAL RUN-TIME ERROR:   "c:\targetinstr.c", line 29, col 9, thread id 0x00000D98:   Function CDotNetLoadAssembly: (return value == -6579 [0xffffe64d]). The LabWindows/CVI .NET Library could not load the specified assembly.

This happens when i try to initialize the .NET Controller using the autogenerated function with a statement like this:
Initialize_proiectclass();
"targetinstr" is the target instrument for the .NET Controller.
Any idea why this happens?
0 Kudos
Message 3 of 10
(6,595 Views)
Hi aliasdealias, Header files don't exist in VB.NET, since the only output of the VB.NET compiler is .NET assemblies.  The .NET Controller wizard generates a C wrapper around your assembly. As I mentioned earlier, the wrapper is in the form of an instrument driver, source file, and header file.  That error you mentioned doesn't have anything to do with incorrect header files. Rather, the problem is that the .NET CLR is unable to locate the assemblies for which the wrapper was created for.  The key concept to remember when calling .NET assemblies from CVI is that iIf you plan to use an assembly in LabWindows/CVI that is not located in the Global Assembly Cache (GAC) or in the current directory of the calling executable, then you must call CDotNetRegisterAssemblyPath to register the assembly. If you use an assembly that is located in the GAC or in the current directory of the calling executable, it is not necessary to call CDotNetRegisterAssemblyPath to use the assembly; the assembly is registered automatically. If you don't put the assembly in the one of the above locations, then you need to make sure to call CDotNetRegisterAssemblyPath and pass the full path to the assembly before calling CDotNetLoadAssembly. Best Regards,

Message Edited by Jonathan N on 04-14-2008 08:44 AM
Jonathan N.
National Instruments
0 Kudos
Message 4 of 10
(6,575 Views)
Thanks for your reply, but is there any other way to import a dll form VB2005 into CVI  besides using .NET Controller? If so, how can this be done?
The dll I want to use uses the COM server to communicate with CANoe and although I succesfully managed to import it into CVI the program crashes when I call the dll.
Thank you.
 
0 Kudos
Message 5 of 10
(6,360 Views)
Hi aliasdealias,

The .NET Controller Wizard and the .NET Library we offer in CVI are the only ways working with assemblies inside of CVI. The .NET Controller Wizard should make your life much easier when trying to call assemblies from CVI.  When you say that your program is crashing, at what function call does it crash at? Does it close CVI or does your program just terminate?  Have you checked the return values of the function calls to make sure they are passing.
If an error occurs during a call to a function in the LabWindows/CVI .NET Library, the status return value contains the error code. You can then use the CDotNetGetErrorDescription call to get a description of the error code. 

The most common mistake I have seen customers fall into is if their assembly is not in the GAC or in the calling directory of the EXE, you must call CDotNetRegisteryAssemblyPath to register the assembly. I'd be happy to look at some small example code if you don't mind posting it that demonstrates the problem.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 6 of 10
(6,347 Views)

Hello,

this is the code that calls the start_canoe() routine from the dll:

int CVICALLBACK start (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   Initialize_testnew ();
   testnew_clasa__Create (&hTest, 0);
   testnew_clasa_start_canoe (hTest, 0);
   Close_testnew ();
   
    
   break;
  case EVENT_RIGHT_CLICK:

   break;
 }
 return 0;
}

This is the VB code for the testnew.dll file:

Public

Class clasa

   Public Sub start_canoe()

       Dim CAN As CANoe.Application

       CAN =

New CANoe.Application

   End Sub

End

Class

This project also has a reference to CANoe 6.1 Type Library (Vector.CANoe.Interop.DLL) which uses the COM server to communicate with CANoe.

Below is the error message that I get when I run the program from CVI:

 NON-FATAL RUN-TIME ERROR:   "c:\...\target instrument\target.c", line 95, col 5, thread id 0x00000A20:   Function CDotNetInvokeGenericMember: (return value == -6571 [0xffffe655]). The target invoked by the LabWindows/CVI .NET Library threw an exception.

This is the code refered by line 95 in the target.c file (from the generated target instrument):

__errChk(CDotNetInvokeGenericMember(
  __assemblyHandle,
  "testnew.clasa",
  __instance,
  CDOTNET_CALL_METHOD,
  "start_canoe",
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  0,
  __exception));

 

Hope this helps, looking forward for your answer.

Thanks.

0 Kudos
Message 7 of 10
(6,333 Views)
Hi aliasdealias,

The particular error you are getting indicates that the target object in the .NET object was called but it threw an exception - this is most probably due to passing some invalid parameter. For example, maybe your
hTest handle is invalid.

If your assembly is not in the GAC, you must register its path with CVI before you call the initialize function (or any other wrapper function).  As I mentioned earlier, you can register the path of the assembly by calling the CDotNetRegisterAssemblyPath library function with the full name of the assembly - the full name of the assembly is available in the __assembly constant in the generated source code. Make sure you pass the appropriate path on your machine. 

So before you make your call to
Initialize_testnew(), make sure you call the registration function.  Now, if you call that and it still doesn't work, then you will need to post the assembly so i can take a look at it. 

Best Regards,


Message Edited by Jonathan N on 05-15-2008 09:59 AM
Jonathan N.
National Instruments
0 Kudos
Message 8 of 10
(6,320 Views)
The assembly (testnew.dll) is in the GAC (in the project folder) so I don't think that's why the error occurs. I have used this method to succesfully use other assemblies and it worked, so I don't think it has anything to do with the handler. I posted the VB2005 code for the testnew.dll file in my previous post, so I'm thinking maybee it has something to do with using COM server to communicate with CANoe. That dll works fine when called from a VB project but not from CVI.
So if you know any other way of importing a dll into CVI without the use of .NET Controller, please let me know, it doesn't seem to work this way.
 
Thanks.
0 Kudos
Message 9 of 10
(6,259 Views)
Hi aliasdealias,

This may not be possible for you but I just wanted to bring it up.  Since you are talking to a COM server, why not just the CVI ActiveX Library our the ActiveX Controller Wizard to talk directly with the COM server? Right now, you are going through some extra un-needed layers. 

The issue you are having might have something to do with COM threading so try and make a call to CA_InitActiveXThreadStyleForCurrentThread (0, COINIT_APARTMENTTHREADED); before you start working with your .NET objects.  In C# and VB.NET, the default threading model is STA but in CVI its MTA. So try calling that function before hand to see if it helps.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 10 of 10
(6,232 Views)