LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to initialize CDotNetHandle correctly

Solved!
Go to solution

Hello everybody,

 

I have created a .NET Controller succesfully from an assembly written in C# I got.

 

I called the Initialize_...( ) and the Close_...( ) routine which seem to work fine.

 

Now I'm trying to call in between this two functions another function that should initialize a driver and needs CDotNetHandle *Exception_Handle as a parameter.

 

I declared this parameter with

CDotNetHandle *Exception_Handle; 

But now i get the error:

 

Local 'Exception_Handle' was referenced before being initialized.

 

Now how and with which value do I initialize this pointer?

 

Pls be detailed as my C knowledge is a little bit rusty 

 

 

0 Kudos
Message 1 of 9
(5,045 Views)

Hello daeda - 

 

You will actually want to declare an instance of a CDotNetHandle and then pass the address of this instance to the call - as below:

 

CDotNetHandle exception = 0;
System_Collections_Generic_List_T1_Add (list, item, &exception);

// Cleanup .NET handles
if (exception)
    CDotNetDiscardHandle(exception);

 

You should check out the example genericlist for further demonstration of this.  There are multiple other examples located in the samples\dotnet folder that should be helpful to you.

 

NickB

National Instruments

0 Kudos
Message 2 of 9
(5,042 Views)

Hi thank you for your fast answer. I had a look at the generic list example and other things I found in the help/find exeamples area.

 

But now when I try to call the function I get an error concerning the function call.

 

Is there any mistake I made or does the assembly has some problems beeing called from C?

 

Here is the error

 

Function CDotNetInvokeGenericStaticMember: (return value == -6571 [0xffffe655]

 

Here is my code I removed some other code that I know is working

 

void main(int argc, char **argv);

void main(int argc, char **argv)
{
  // Exception handles für die DotNet Bibliothek
  static CDotNetHandle exception = 0;
  
  // Muss aufgerufen werden bevor etwas aus der .NET Assembly aufgerufen werden kann
  Initialize_RmIntAdapter ();
  
  // startet und initialisiert die Bibliothek
  RmIntAdapter_RmIntAdapterCtrl_Start (&exception);
   
  // Schließt die Library wieder
  Close_RmIntAdapter (); 
  
  exit(1);
}

 

0 Kudos
Message 3 of 9
(5,006 Views)

Hmmm... It looks like your assembly is throwing an exception on the call to Start.  Error -6571 indicates that the assembly you're calling has thrown an exception.  Because you know this, you can check the exception object from CVI to see if you can get some more information, with code something like the following:

 

// some method that throws an exception
ExceptionThrower_ExceptionThrowingClass_ThrowException (thrower, &exception);
if (exception != NULL) // Implies previous function returned error -6571
{
    CDotNetGetExceptionInfo (exception, NULL, &message, &source, &trace, NULL, NULL);
    printf ("Exception occurred!\n"
            "\tThe message is %s\n"
            "\tThe source is %s\n"
            "\tThe stack trace is %s\n", message, source, trace);
    CDotNetFreeMemory (message);
    CDotNetFreeMemory (source);
    CDotNetFreeMemory (trace);
    CDotNetDiscardHandle (exception);
}

 

Let me know if this doesn't help solve things.

 

NickB

National Instruments

0 Kudos
Message 4 of 9
(4,955 Views)

Ok I will try this tomorrow.

 

During my research I found someone that had a simililar problem.

 

May this whole thing has something to do with the fact that my assembly is not in the GAC and not signed?

 

 

 

 

0 Kudos
Message 5 of 9
(4,943 Views)

So here is the message I get when I use your code

 

Exception occurred!
        The message is Der Typeninitialisierer f³r "RmIntAdapter.RmIntAdapterCtrl" hat eine Ausnahme verursacht.
        The source is RmIntAdapter
        The stack trace is    bei RmIntAdapter.RmIntAdapterCtrl.Start()

 

This is in german since the assembly is written by a german developer

 

Exception occurred!
        The message is The type initializer for "RmIntAdapter.RmIntAdapterCtrl" has caused an exception

        The source is RmIntAdapter
        The stack trace is    at RmIntAdapter.RmIntAdapterCtrl.Start()

0 Kudos
Message 6 of 9
(4,920 Views)
Solution
Accepted by daeda

Based on that exception, my guess is that the assembly you're loading has dependencies that can't be found.  They are likely in the same folder as the assembly you're loading, but because you're application is not in that same folder, the CLR has no way of finding them.  You can verify this by checking the fusion logs generated durning the assembly binding process.

 

If my guess is correct, the easiest way to solve what you're seeing is to either strongly name all of your assemblies and place them in the GAC, or move your CVI application into the same directory as all the assemblies it depends on.

 

NickB

National Instruments

0 Kudos
Message 7 of 9
(4,900 Views)

This seemed to be one problem.

 

Now I added all files I have in the folder where my project is, this leads to disappearing of this initializing problem on the one hand.

 

But also leads to a new error which isn't helping me either.

 

FATAL-RUN-TIME ERROR

 

Unknown source position, thread id 0x00000E34

 

A non-debuggable thread caused a 'Unknown' fault at address 0x7C812AFB

 

0 Kudos
Message 8 of 9
(4,879 Views)

Ok it seems like I got this thing to work.

 

I guess this error occurs due to the fact that the dll is not debuggable since this happens when I press the green button "debug project".

 

If I only build a debuggable executable and don't start it from the Labwindows IDE but by executing the exe in the windows explorer it runs smoothly.

 

Do I have to talk to the dll programmers and ask them to recompile the dlls as debuggable or do I have to make some changes to my settings in LabWindows.

0 Kudos
Message 9 of 9
(4,874 Views)