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;
}
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;
}