PDA

Click to See Complete Forum and Search --> : VC dll's and pointers in VB?


jskog
Jan 30th, 2003, 08:33 AM
Hello.

I am very new to VB programming, but I have to write a small program to check if it is possible to use a function from my DLL in VB.NET. (The DLL is written in VC.NET and it works just fine in other VC programs.)

The function is declared as

extern "C" _declspec (dllexport) float* RunFilter(char* , long);

and I would like some help on how to use it in VB, and how to handle the pointers.

In VB I have tried to import the function as

Public Declare Function RunMyFilter Lib "Mydll.dll" Alias "RunFilter" (ByVal indata As Byte(), ByRef datalength As Long) As Single()

but I don't know if I got the datatypes correctly... Does it seem correct?

When I use the function the arguments are declared as

Dim Buffer As Byte() = New Byte(lFileSize) {}
Dim lFileSize As Long

and I call the function like

Dim utdata As Single() = New Single(180) {}
utdata = RunMyFilter(Buffer, lFileSize)


The program compiles allright, but when I run it and it is supposed to "RunMyFilter", the program crashes and tells me:

An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveException' occurred in WindowsApplication1.exe

Additional information: Can not marshal return value.


Do you know what I have made wrong?

Best Regards
Johan

jskog
Jan 30th, 2003, 08:38 AM
I have also tried removing the
extern "C"
and using the mangled function name, but it did not change a thing.

To see where the problem was I also changed the function in the dll so that it returned float instead of the pointer float* to the bunch of floats that I want the function to return. (Ofcourse I also changed the VB prog to match this)

Then I did not get the marshal error, instead I got this error message:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an object.


Any clue?