By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,682 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Passing Managed char[] from C# to Unmanaged C++ dll

P: n/a
Hi,

I seem to be having a problem I can't quite figure out. Currently, I have
the following code (below). In the SendMsg function, I create a 'Qtkmsg'
which converts the string 'text' into a null terminated character array that
is sent through the DllImport interface 'QtkSendMesg'. However, when I
receive this struct in the C++ DLL, the character array that I passed to it
is filled with random characters. What am I doing wrong when passing the
char[] from managed to unmanaged code?

Thanks in advance,

Chris

In various C# files:

public int SendMsg( int callid, int target, int device, int type, int
priority, int code, string text )

{

int result = -1;

QtkMesg Qtkmsg = new QtkMesg( callid, target, device, type, priority, 1,
text);

result = QtkSendMesg( ref Qtkmsg );

return result;

}

----------------------------------------------

[DllImport("QtkDriver.dll", CharSet=CharSet.Auto)]

private static extern int QtkSendMesg( ref QtkMesg pMesg);

----------------------------------------------

public struct QtkMesg

{

public int qm_id; // The message id number (filled in by library) ie -
office use only

public int qm_target; // The message target pager number or a
annunciator address)

public short qm_device; // The device ID number of the output device

public short qm_type; // the type of message (see message types)

public short qm_priority; // the priority of the message

public short qm_code; // extra parameter for bleep code or other
information

public char[] qm_text; // the text of the message

public QtkMesg( int id, int target, int device, int type, int priority,
int code, string text )

{

// constructor

this.qm_id = id;

this.qm_target = target;

this.qm_device = Convert.ToInt16(device);

this.qm_type = Convert.ToInt16(type);

this.qm_priority = Convert.ToInt16(priority);

this.qm_code = Convert.ToInt16(code);

char[] test = new char[text.Length+1];

text.CopyTo(0, test, 0, text.Length);

test[test.Length-1] = '\0';

this.qm_text = test;

}

}

===============================

In C++ DLL:

struct QtkMesg

{

long qm_id;

long qm_target;

short qm_device;

short qm_type;

short qm_priority;
short qm_code;
char qm_text[80];

};

-------------------------------------------

long QtkSendMesg( QtkMesg* pMesg )

{

driverManager* pManager = driverManager::GetInstance();

if ( pManager )

return pManager->SendMesg( pMesg );

else

return INVALID_MESG_ID;

}
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Please see the topic concerning default string marshalling with PInvoke (Platform Invoke).

You should declare your structure a bit like this:

For the C++ structure:
struct StringInfoA
{
fields...
char f2[256];
};

The C# type definition:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
struct StringInfoA {
fields...
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public String f2;
}

Please see marshaling strings samples:
http://msdn.microsoft.com/library/de...ingstrings.asp
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

I seem to be having a problem I can't quite figure out. Currently, I have
the following code (below). In the SendMsg function, I create a 'Qtkmsg'
which converts the string 'text' into a null terminated character array that
is sent through the DllImport interface 'QtkSendMesg'. However, when I
receive this struct in the C++ DLL, the character array that I passed to it
is filled with random characters. What am I doing wrong when passing the
char[] from managed to unmanaged code?

Thanks in advance,

Chris

In various C# files:

[...]

Nov 15 '05 #2

P: n/a
Thanks! I'll give it a go.
"Cezary Nolewajka" <c.*********************@no-sp-am-eh-mail.com> wrote in
message news:Oe**************@TK2MSFTNGP12.phx.gbl...
Please see the topic concerning default string marshalling with PInvoke
(Platform Invoke).

You should declare your structure a bit like this:

For the C++ structure:
struct StringInfoA
{
fields...
char f2[256];
};

The C# type definition:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
struct StringInfoA {
fields...
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public String f2;
}

Please see marshaling strings samples:
http://msdn.microsoft.com/library/de...ingstrings.asp
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
"Chris" <c w a n @ n o s p a m - v i g i l . c o m> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

I seem to be having a problem I can't quite figure out. Currently, I have
the following code (below). In the SendMsg function, I create a 'Qtkmsg'
which converts the string 'text' into a null terminated character array that is sent through the DllImport interface 'QtkSendMesg'. However, when I
receive this struct in the C++ DLL, the character array that I passed to it is filled with random characters. What am I doing wrong when passing the
char[] from managed to unmanaged code?

Thanks in advance,

Chris

In various C# files:

[...]
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.