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

C

Chris

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;

}
 
C

Cezary Nolewajka

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/d...en-us/cpguide/html/cpconmarshalingstrings.asp


Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply


Chris said:
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:
[...]
 
C

Chris

Thanks! I'll give it a go.


message 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/d...en-us/cpguide/html/cpconmarshalingstrings.asp


Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply


Chris said:
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:
[...]
 

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