Interaction woth a c DLL

  • Thread starter Thread starter baboo
  • Start date Start date
B

baboo

I have to pass a structure with some strings and a few
arrays to a dll written in c from my VB.NET code. Any
information on how to go about doing it. Any website
addresses where related information can be found will also
be of great help.

Thanks
 
baboo said:
I have to pass a structure with some strings and a few
arrays to a dll written in c from my VB.NET code. Any
information on how to go about doing it. Any website
addresses where related information can be found will also
be of great help.

If you've got VB/VS.NET:

<F1>
VS.NET
.NET Framework
Programming with .NET Framework
Interoperating with unmanaged code

especially these sub topics:

Consuming unmanged DLL functions
Design Considerations For Interoperation
Interop Marshalling


If you only have the SDK: http://msdn.microsoft.com
 
Thanks for the info... i looked at that information but
there is not much that will help me solve my problem which
is:

I have to pass an array of strings inside a structure,
from .NET code to a 'C' DLL, though i am able to pass the
structure.. the c code gives me garbage values for that
array.
the .NET code is:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)
Public Structure Test
Dim ErrorValue As Double
Dim FieldNumber As Integer
Dim Dummy As Integer
Public ControlNames() As String
End Structure

The "ControlNames" array is an array of 15 strings with
each string being 50 characters long.

and the 'C' structure which takes the values is:

struct Test { double ErrorValue;
int FieldNumber;
int Dummy;
char sFixUpCtrl[15][50];
};

while debugging i find that the values for
the "ControlNames" array in 'C' are garbage.

any idea about how to go about fixing this problem??

~.~.~.~.~.~.
Baboo
 
Thanks for the info... i looked at that information but
there is not much that will help me solve my problem which
is:

I have to pass an array of strings inside a structure,
from .NET code to a 'C' DLL, though i am able to pass the
structure.. the c code gives me garbage values for that
array.
the .NET code is:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)
Public Structure Test
Dim ErrorValue As Double
Dim FieldNumber As Integer
Dim Dummy As Integer
Public ControlNames() As String
End Structure

The "ControlNames" array is an array of 15 strings with
each string being 50 characters long.

and the 'C' structure which takes the values is:

struct Test { double ErrorValue;
int FieldNumber;
int Dummy;
char sFixUpCtrl[15][50];
};

This one is a little problematic... Normally, a fixed length array or
string in side of a structure is not a problem... For example, if it
was a single 50 character string:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure Test
Public ErrorValue As Double
Public FieldNumber As Integer
Public Dummy As Integer

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=50)> _
Public sFixUpCtrl As String
End Structure

But your's is a fixed two dimensional array. I have never had that
come up before, and I really don't see a good way to do it with in the
bounds of what is currently defined in the framework...

As a work around (unless someone comes up with a better idea, or knows a
way to do this directly), I would suggest just passing a single string
buffer big enough to hold the entire thing, and then chunk the data on
the return. In theory that should work since arrays are just contiguos
blocks of memory - even if they are 2D. So maybe something like (you
may have to play with the numbers - I'm sort of sleepy right now :)

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure Test
Public ErrorValue As Double
Public FieldNumber As Integer
Public Dummy As Integer

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=750)> _
Public sFixUpCtrl As String
End Structure

And then, just do something like:

'Call your api call
Dim st As Test
.....

' chunk the string
Dim s(14) As String

For i As Integer = 0 To 14
s(i) = st.sFixUpCtrl.SubString(i*50, 50)
Next i

Oi! I'm getting sleepy... I hope I'm not doing something stupid in the
above little loop. But anyway, it's an idea that may work. I'll see if
I can find any more info on this tomorrow...
 
Back
Top