//the original C++ struct
typedef struct _tagHHP_DECODE_ MSG
{
DWORD dwStructSize; // Size of decode
structure.
TCHAR pchMessage[ MAX_MESAGE_LENG TH ]; // 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_LENG TH =4096;
public const int MAX_LEN=100;
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi)]
public struct _tagHHP_DECODE_ MSG
{
public UInt32 dwStructSize; // Size of decode structure.
//[MarshalAs(Unman agedType.ByValT Str, SizeConst=MAX_M ESAGE_LENGTH)]
[MarshalAs(Unman agedType.LPStr, SizeConst=MAX_M ESAGE_LENGTH)]
public string/*StringBuilder*/ pchMessage;
//MAX_MESAGE_LENG TH, decoded message data
[MarshalAs(Unman agedType.LPStr, SizeConst=MAX_L EN)]
public string /*StringBuilder*/ chCodeID; // AIM Id of symbology
[MarshalAs(Unman agedType.LPStr, SizeConst=MAX_L EN)]
public string /*StringBuilder*/ chSymLetter; // HHP Id of symbology
[MarshalAs(Unman agedType.LPStr, SizeConst=MAX_L EN)]
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(M AX_MESAGE_LENGT H);
// chCodeID=new StringBuilder(M AX_LEN);
// chSymLetter=new StringBuilder(M AX_LEN);
// chSymModifier=n ew StringBuilder(M AX_LEN);
// nLength=(UInt32 )1;
// dwStructSize=(U Int32)1;
//}
};
the C++ DLL function is defined as
int hhpCaptureBarco de( _tagHHP_DECODE_ MSG pDecodeMsg)
where
pDecodeMsg: Pointer to an _tagHHP_DECODE_ MSG structure
the C# version of this function is
[DllImport("hhpI mgrSdk.dll", CharSet=CharSet .Ansi)]
public static extern int hhpCaptureBarco de( ref _tagHHP_DECODE_ MSG
pDecodeMsg)
> now to my problem: i know that the strings (such as pchMessage) will be written inside hhpCaptureBarco de, 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.dwSt ructSize =
(UInt32)System. Runtime.Interop Services.Marsha l.SizeOf(decode Info);
The error message is
"An unhandled exception of type 'System.Argumen tException' 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 = hhpCaptureBarco de( ref decodeInfo) == 1 ) //1 is
success
i get the following error msg
"An unhandled exception of type 'System.TypeLoa dException' 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 hhpCaptureBarCo de 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 hope i made a point
I Apprciate any help.
Thanks
Mo
ma******@hotmai l.com