C# marshal struct C++ DLL issue stringbuilder

M

Mo

I am having problem with marshaling struct in C#.

//the original C++ struct
typedef struct _tagHHP_DECODE_MSG
{
DWORD dwStructSize; // Size of decode
structure.
TCHAR pchMessage[ MAX_MESAGE_LENGTH ]; // decoded message data
TCHAR chCodeID; // AIM Id of symbology
TCHAR chSymLetter; // HHP Id of symbology
TCHAR chSymModifier; // Modifier characters.
DWORD nLength; // length of the decoded
message
};

//the same in C# is

public const int MAX_MESAGE_LENGTH =4096;
public const int MAX_LEN=100;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct _tagHHP_DECODE_MSG
{
public UInt32 dwStructSize; // Size of decode structure.

//[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_MESAGE_LENGTH)]
[MarshalAs(UnmanagedType.LPStr, SizeConst=MAX_MESAGE_LENGTH)]
public string/*StringBuilder*/ pchMessage;
//MAX_MESAGE_LENGTH, decoded message data

[MarshalAs(UnmanagedType.LPStr, SizeConst=MAX_LEN)]
public string /*StringBuilder*/ chCodeID; // AIM Id of symbology

[MarshalAs(UnmanagedType.LPStr, SizeConst=MAX_LEN)]
public string /*StringBuilder*/ chSymLetter; // HHP Id of symbology

[MarshalAs(UnmanagedType.LPStr, SizeConst=MAX_LEN)]
public string /*StringBuilder*/ chSymModifier; // Modifier
characters.

public UInt32 nLength; // length of the decoded message

//this constructor is used with StringBuilder case
//public _tagHHP_DECODE_MSG(int x)
//{
//x is some unimportant value
// pchMessage=new StringBuilder(MAX_MESAGE_LENGTH);
// chCodeID=new StringBuilder(MAX_LEN);
// chSymLetter=new StringBuilder(MAX_LEN);
// chSymModifier=new StringBuilder(MAX_LEN);
// nLength=(UInt32)1;
// dwStructSize=(UInt32)1;
//}
};

the C++ DLL function is defined as

int hhpCaptureBarcode( _tagHHP_DECODE_MSG pDecodeMsg)
where
pDecodeMsg: Pointer to an _tagHHP_DECODE_MSG structure

the C# version of this function is
[DllImport("hhpImgrSdk.dll", CharSet=CharSet.Ansi)]
public static extern int hhpCaptureBarcode( ref _tagHHP_DECODE_MSG
pDecodeMsg)

will be written inside hhpCaptureBarcode, i.e they should ne defined
as StringBuilder, i.e i should provide a non default struct for my
struct (as u can tell, its all commented out)

the problem is, when i do that i get tow errors.

1) first error: in my app, i do
_tagHHP_DECODE_MSG decodeInfo=new _tagHHP_DECODE_MSG();

//this line gives me an error when i use StringBuilder instead of
string for
//my variables in the struct
decodeInfo.dwStructSize =
(UInt32)System.Runtime.InteropServices.Marshal.SizeOf(decodeInfo);

The error message is
"An unhandled exception of type 'System.ArgumentException' occurred
in barcode.exe Additional information: Type _tagHHP_DECODE_MSG can not
be marshaled as an unmanaged structure; no meaningful size or offset
can be computed."

However, This doesnt happen if i use String


2) if i ignore the previous line (comment it out), and i use
StringBuilder, i get the following error message on this line

int nResult;
if ( (nResult = hhpCaptureBarcode( ref decodeInfo) == 1 ) //1 is
success

i get the following error msg

"An unhandled exception of type 'System.TypeLoadException' occurred
in barcode.exe Additional information: Marshaler restriction:
StringBuilders can not be used in structure fields.
The same effect can usually be achieved by using a String field and
preinitializing it to a string with length matching the length of the
appropriate buffer."

However, i dont get the same error when i use string, but the result
returned by hhpCaptureBarCode is 0 (nResult=0 -which indicated there
was a problem reading the barcode, i verified that the barcode works
with the demo program provided by the Vendor)

I Apprciate any help.
Thanks
Mo

(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top