473,507 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 6562
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3500
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
4
11329
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
2
4667
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
3
3483
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
3
3540
by: Sam Carleton | last post by:
I am writing Managed C++ code to call in to an Unmanaged C API. The reason for using Managed C++ over C# is that the Unmanaged C module is loaded via LoadLibrary(). DllImport cannot be used;...
12
11343
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
13
2860
by: Creativ | last post by:
I've looked through this thread and still have quetions. Suppose In visual studio 2005, I write the following #pragam managed class ManagedWrapper { void CallUnmanagedMethod() // The unmanaged...
6
3875
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.