Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2007

Tip: Looking for answers? Try searching our database.

VB.NET / DLL structure passing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sam - 08 Jun 2007 00:45 GMT
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?
Ben Voigt [C++ MVP] - 09 Jun 2007 18:51 GMT
>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
[quoted text clipped - 9 lines]
> /*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.

> /*4 bytes*/  int        ProjectFilesStart[7];
>          byte       EEPROM[50];
[quoted text clipped - 25 lines]
> 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?
sam - 09 Jun 2007 21:50 GMT
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.