473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unmanaged DLL structure conversion problem (unsigned char pointer)

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.
Oct 28 '09 #1

✓ answered by edsunder

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

7 6966
Plater
7,872 Expert 4TB
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*
Oct 29 '09 #2
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?
Oct 30 '09 #3
Plater
7,872 Expert 4TB
That was only for the char*, not the whole thing
Nov 2 '09 #4
I'm not sure I understand what you mean by that. Could you explain maybe with an example? Thanks for your help.
Nov 2 '09 #5
Plater
7,872 Expert 4TB
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
Nov 2 '09 #6
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.
Nov 5 '09 #7
Turns out it was a combination of a missing parameter and I needed to use Pack := 4 in the StructLayout
Nov 16 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
1
by: tony.fountaine | last post by:
I am working on a project to read a Bosch Measurement Data File (MDF). The file contains a number of blocks that can be read from the file using a baisc structure. For example the ID BLOCK is as...
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
7
by: Tremendo | last post by:
Hi, I need to consume an unmanaged DLL from managed C#. The DLL is "ae766.dll". I have problems with one function in it. Who developed the DLL, provided the following information, regarding that...
16
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
0
by: DavidT | last post by:
Hello, at first, exuse if the following question is simple to solve, but i normaly coding with C# and now have to use C++/CLI for one project. My Problem is that i have to use a native c++ sdk...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.