473,385 Members | 1,464 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,385 software developers and data experts.

C# marshal struct C++ DLL issue stringbuilder

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)

> now to my problem: i know that the strings (such as pchMessage) 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.Siz eOf(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 hope i made a point


I Apprciate any help.
Thanks
Mo

ma******@hotmail.com
Nov 16 '05 #1
0 8724

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
0
by: mo A | last post by:
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 ...
7
by: Mo | last post by:
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 ...
1
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
13
by: Just Me | last post by:
The following almost works. The problem is Marshal.PtrToStringAuto seems to terminate at the first null so I don't get the full string. Any suggestions on how to fix this? Or how to improve the...
12
by: Sadeq | last post by:
Is there a way to read a struct field by field? I want to write a rather general function, like: public string EncodeStruct (struct strct) { .... } which accepts a general struct type,...
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.