473,569 Members | 2,729 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("QtkD river.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_priorit y = 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 6567
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(La youtKind.Sequen tial, CharSet=CharSet .Ansi)]
struct StringInfoA {
fields...
[MarshalAs(Unman agedType.ByValT Str, 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******** ********@TK2MSF TNGP12.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******** ******@TK2MSFTN GP12.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(La youtKind.Sequen tial, CharSet=CharSet .Ansi)]
struct StringInfoA {
fields...
[MarshalAs(Unman agedType.ByValT Str, 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******** ********@TK2MSF TNGP12.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
3516
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 this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++...
4
11331
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 LNK2020: unresolved token (0A000007) memset LINK : error LNK2020: unresolved token (0A000008) free LINK : error LNK2020: unresolved token (0A00000A)...
2
4671
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 this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++...
3
3494
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
3542
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; functions pointers and GetProcAddress() is being used. The API function definition looks like this: int GetString( char* pszOutStr, int *nStrLen)...
12
11351
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), I am calling a managed C# method(I use gcnew to instantiate the managed class). One of the parameters of this C# method is a delegate. I need to...
17
7226
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 to show the array data to the end user. Can I do that? How?
13
2863
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 class /method is imported from a C++ DLL generated by vc 6.0 { std::string* inputString = new string();
6
3877
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 definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5217
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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 we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.