HighTechTalks DotNet Forums  

VB.NET / DLL structure passing

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss VB.NET / DLL structure passing in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
sam
 
Posts: n/a

Default VB.NET / DLL structure passing - 06-07-2007 , 07:45 PM






I have a third-party DLL (unmanaged) for which I am trying to call a
function. The function requires a structure (as an argument) which
utilized several variables I am unable to translate.

The original C header structures contain (amongst other things that I
have already translated):


typedef byte TPacketType[int(100)];

struct TModuleRec
{
/*1 byte*/ bool Succeeded;
/*4 bytes*/ char *Error;
/*4 bytes*/ char *ProjectFiles[7];
/*4 bytes*/ int ProjectFilesStart[7];
byte EEPROM[50];
TPacketType PacketBuffer;
}

I've been able to translate everything so far except for these
particular variables. I've tried (as suggested on some interop articles
I've read)

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public
Class TPacketType
Public TPacketType(100) As Byte
End Class

<StructLayout(LayoutKind.Sequential)> Public Class TModuleRec
<MarshalAs(UnmanagedType.U1)> Public Succeeded As Boolean
Public Err(4) As Byte
Public ProjectFiles(7) As Byte
Public ProjectFilesStart(7) As Integer
Dim EEPROM(50) As Byte
Public EEPROMFlags(50) As Byte
Public PacketBuffer As TPacketType
End Class


for the booleans, but this still results in "non-primitive or
non-blittable" structure errors (I remarked out the unmarshalled Bools
during testing).

I'd really like to understand why the MarshalAs isn't working (as well
as how to translate the other lines). Can someone offer some suggestions?

Reply With Quote
  #2  
Old   
Ben Voigt [C++ MVP]
 
Posts: n/a

Default Re: VB.NET / DLL structure passing - 06-09-2007 , 01:51 PM







"sam" <spammenot (AT) gtnincs (DOT) com> wrote

Quote:
I have a third-party DLL (unmanaged) for which I am trying to call a
function. The function requires a structure (as an argument) which utilized
several variables I am unable to translate.

The original C header structures contain (amongst other things that I have
already translated):


typedef byte TPacketType[int(100)];

struct TModuleRec
{
/*1 byte*/ bool Succeeded; /*4 bytes*/ char *Error;
/*4 bytes*/ char *ProjectFiles[7];
You have an array of pointers, which I don't believe .NET can handle, never
mind VB.

You can make this an array of IntPtr, and use Marshal to pin the objects and
fill in all the pointers yourself, or you could write a .NET compatible
wrapper with C++/CLI. The latter is usally much easier, if you have ever
used C or C++ before.

Quote:
/*4 bytes*/ int ProjectFilesStart[7];
byte EEPROM[50];
TPacketType PacketBuffer;
}

I've been able to translate everything so far except for these particular
variables. I've tried (as suggested on some interop articles I've read)

StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> Public Class
TPacketType
Public TPacketType(100) As Byte
End Class

StructLayout(LayoutKind.Sequential)> Public Class TModuleRec
MarshalAs(UnmanagedType.U1)> Public Succeeded As Boolean
Public Err(4) As Byte
Public ProjectFiles(7) As Byte
Public ProjectFilesStart(7) As Integer
Dim EEPROM(50) As Byte
Public EEPROMFlags(50) As Byte
Public PacketBuffer As TPacketType
End Class


for the booleans, but this still results in "non-primitive or
non-blittable" structure errors (I remarked out the unmarshalled Bools
during testing).

I'd really like to understand why the MarshalAs isn't working (as well as
how to translate the other lines). Can someone offer some suggestions?


Reply With Quote
  #3  
Old   
sam
 
Posts: n/a

Default Re: VB.NET / DLL structure passing - 06-09-2007 , 04:50 PM




Quote:
You can make this an array of IntPtr, and use Marshal to pin the objects
and fill in all the pointers yourself, or you could write a .NET
compatible wrapper with C++/CLI. The latter is usally much easier, if
you have ever used C or C++ before.

After looking for resources on this sort of thing for several days, it
sound like your suggestion of writing a C++ wrapper for this thing might
be the way to go. I can find no examples of something like this on the
web, and every example I do find of marshalling seems to fail with
errors--there appear to be quite a few pratfalls involved. I have some
C++ experience, but writing a managed wrapper might be a better way to
go. I've considered it. Another thing I've considered--I don't even know
if it's possible--is to build a byte array of all this data and then
pass the pointer.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.