Connecting Tech Pros Worldwide Help | Site Map

Unmanaged DLL structure conversion problem (unsigned char pointer)

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: 3 Weeks Ago
I'm making a "wrapper" for an unmanaged DLL (written, I'm fairly certain in C). I have a c++ "wrapper" that I'm trying to convert to VB.net. I've got most of the program working, but I've hit a brick wall.

I'm trying to convert the following two structures from their c++ versions to their vb.net versions. I cannot be sure which is the problem because both must be passed at the same time. The error I am getting is thrown by the object and indicates "Invalid structure size specified". I can cause this error on other objects when I either don't set the dwSize variable, leave off a parameter or change the parameter type incorrectly. Note that the dwSize parameter is supposed to contain the size of each structure. I'm successfully calculating that for my other structures and I'm using the same method on these and the results appear correct, so I do not believe the problem is found there. The object uses a lot of structures and the rest of them are working. These two are the only ones with a) an unsigned character pointer b) an unsigned long array and c) doubles.

C++ versions:
Expand|Select|Wrap|Line Numbers
  1. //
  2. // Texture data
  3. //
  4.  
  5. typedef struct tagFACE_TEXTUREDATA
  6. {
  7.     DWORD dwSize;
  8.     LONG  nTextureType;
  9.     LONG  nWidth;
  10.     LONG  nHeight;
  11.     LONG  nBitCount;
  12.     DWORD dwPalette[256];
  13.     BYTE* pImageData;
  14.     CHAR  szImageFile[260];
  15.     LONG  nColorTranslation;
  16.     DWORD dwColor;
  17.     LONG  nTextureSize;
  18.     LONG  nTextureScaling;
  19.     BYTE* pMaskData;
  20.     CHAR  szMaskFile[260];
  21. FACE_TEXTUREDATA;
  22.  
  23. //
  24. // Texture properties
  25. //
  26.  
  27. typedef struct tagFACE_TEXTUREPROP
  28. {
  29.     DWORD  dwSize;
  30.     DOUBLE eGloss;
  31.     DOUBLE eContrast;
  32.     BOOL   bRepeat;
  33.     DOUBLE eDropX;
  34.     DOUBLE eDropY;
  35.     DOUBLE ePlacingPointX;
  36.     DOUBLE ePlacingPointY;
  37.     DOUBLE eWidth;
  38.     DOUBLE eHeight;
  39.     LONG   nTransformation;
  40. FACE_TEXTUREPROP;
  41.  
Here are my VB structures:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     ' Texture data
  3.     Public Structure FACE_TEXTUREDATA
  4.         <MarshalAs(UnmanagedType.U4)> Public dwSize As UInteger
  5.         <MarshalAs(UnmanagedType.I4)> Public nTextureType As Integer
  6.         <MarshalAs(UnmanagedType.I4)> Public nWidth As Integer
  7.         <MarshalAs(UnmanagedType.I4)> Public nHeight As Integer
  8.         <MarshalAs(UnmanagedType.I4)> Public nBitCount As Integer
  9.         <MarshalAs(UnmanagedType.ByValArray, ArraySubType:=UnmanagedType.U4, SizeConst:=256)> Public dwPalette() As UInteger
  10.         <MarshalAs(UnmanagedType.I1)> Public pImageData As Byte
  11.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szImageFile As String
  12.         <MarshalAs(UnmanagedType.I4)> Public nColorTranslation As Integer
  13.         <MarshalAs(UnmanagedType.U4)> Public dwColor As UInteger
  14.         <MarshalAs(UnmanagedType.I4)> Public nTextureSize As Integer
  15.         <MarshalAs(UnmanagedType.I4)> Public nTextureScaling As Integer
  16.         <MarshalAs(UnmanagedType.I1)> Public pMaskData As Byte
  17.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> Public szMaskFile As String
  18.     End Structure
  19.  
  20.     ' Texture properties
  21.     Public Structure FACE_TEXTUREPROP
  22.         <MarshalAs(UnmanagedType.U4)> Public dwSize As UInteger
  23.         <MarshalAs(UnmanagedType.R8)> Public eGloss As Double
  24.         <MarshalAs(UnmanagedType.R8)> Public eContrast As Double
  25.         <MarshalAs(UnmanagedType.I4)> Public bRepeat As Integer
  26.         <MarshalAs(UnmanagedType.R8)> Public eDropX As Double
  27.         <MarshalAs(UnmanagedType.R8)> Public eDropY As Double
  28.         <MarshalAs(UnmanagedType.R8)> Public ePlacingPointX As Double
  29.         <MarshalAs(UnmanagedType.R8)> Public ePlacingPointY As Double
  30.         <MarshalAs(UnmanagedType.R8)> Public eWidth As Double
  31.         <MarshalAs(UnmanagedType.R8)> Public eHeight As Double
  32.         <MarshalAs(UnmanagedType.I4)> Public nTransformation As Integer
  33.     End Structure
  34.  
I don't think I'm missing any parameters - the help file seems to agree with me on this, but it's theoretically possible that there is something missing (though I don't think so for other reasons as well). My suspicion is that it's dwPalette, pImageData or pMaskData with (in my uninformed opinion) more suspicion on the pImageData and pMaskData.

Any help would be appreciated. I've pretty much exhasted my knowledge of this stuff a while ago. That said, I am very close to the finish line and if I can solve this, I believe that will be the last thing I need to make this work.
best answer - posted by edsunder
Turns out it was a combination of a missing parameter and I needed to use Pack := 4 in the StructLayout
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: 3 Weeks Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


Have you tried UnmanagedType.LPStr or UnmanagedType.LPArray maybe?
Or maybe even IntPtr?
I don't know how to do it with structs.
I know for function calls I've seen luck with StringBuilder for char*
Newbie
 
Join Date: Oct 2009
Posts: 5
#3: 3 Weeks Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


Thanks for your reply!

I can't seem to make it let me use LPArray even if I convert it to an array:
Expand|Select|Wrap|Line Numbers
  1. <MarshalAs(UnmanagedType.LPArray)> Public pImageData() As Byte
  2.  
it tells me "System.TypeLoadException: Cannot marshal field 'pImageData' of type 'FACE_TEXTUREDATA': Invalid managed/unmanaged type combination (Arrays fields must be paired with ByValArray or SafeArray)."

What am I missing?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: 2 Weeks Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


That was only for the char*, not the whole thing
Newbie
 
Join Date: Oct 2009
Posts: 5
#5: 2 Weeks Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


I'm not sure I understand what you mean by that. Could you explain maybe with an example? Thanks for your help.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: 2 Weeks Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


Oh ok, the structure for VBNET in this is much different then c#
Hmm I don't know.

Looking back at the struct its really got to be IntPtr (or some unmanaged pointer type) and you need to use special functions to retreieve the unmanaged data
Newbie
 
Join Date: Oct 2009
Posts: 5
#7: 2 Weeks Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


After looking at this for a week, I'm thinking that I'm missing a parameter. I've contacted the developer of the DLL. Let's hope they respond. :) Thanks for your help.
Newbie
 
Join Date: Oct 2009
Posts: 5
#8: 6 Days Ago

re: Unmanaged DLL structure conversion problem (unsigned char pointer)


Turns out it was a combination of a missing parameter and I needed to use Pack := 4 in the StructLayout
Reply

Tags
dll, marshallas, structure, unmanagedtype


Similar Visual Basic .NET bytes