Click here to Skip to main content
14,774,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi All ,

I need to convert a char array, char UniqueID[88] to a const char*

actually there is a data contract type string, UniqueID in C#.
and I have a extern char UniqueID[LEN] in another file that i can access in my cpp file
and we need to pass it from managed C++ to C#.
so I am trying to do as follows. but not able to succeed.

DataContracts::InformDataContract^ InformDataContract=gcnew DataContracts::InformDataContract();

InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (char *)UniqueID );

Please let me know your suggestions /ideas on this.

den2k88 5-Nov-14 3:22am
What error does it give? Can you post a bit more of the surrounding code? Help us to help you!
Member 3975629 5-Nov-14 8:35am
I have an extern variable char UniqueID[88] in another .h file . and i can that access in my file.
I am getting the below errors, though i've include the header file where char UniqueID[88] is declared.
InformManager.obj : error LNK2020: unresolved token (0A0003BF) "char * UniqueID" (?UUID@@3PADA)
InformManager.obj : error LNK2001: unresolved external symbol "char * UniqueID" (?UUID@@3PADA)
Debug\InformManager.dll : fatal error LNK1120: 2 unresolved externals
den2k88 7-Nov-14 6:09am
To make it work you should declare UniqueID in a .cpp (or .c) file, THEN refer to it with external in header and FINALLY including that header.

That is because whe you use the "extern" keyword you are substantially telling the compiler "look, somewhere in the code this symbol is defined so don't shoot an error right now and pass the job to the linker". But if that symbol is non-existant in any of the .obj files created by the compiler (and they depend entirely from the .c o .cpp files) it is the linker that issues the error.

Also, sorry for the dealy in the answer, I did not see your comment up until now.

you only need to insert the const qualifier:
InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (const char *)UniqueID );

Ensure that you not only pointing to the string in C++ but owning the buffer in your C++.

And you need to make a own copy on the C# side, because it is read only memory of the C++ environment.
Member 3975629 5-Nov-14 7:30am
I have a extern variable char UniqueID[88] in another file . and i can access in my file.
so do you want me to try something like this
DataContracts::InformDataContract^ InformDataContract=gcnew DataContracts::InformDataContract();

InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (const char *)UniqueID );

if i do so it is giving the below error .. please advise on this.
error C2440: 'type cast' : cannot convert from 'co
nst char *' to 'System::IntPtr'
No user-defined-conversion operator available that can perform this conv
ersion, or the operator cannot be called.
Hi All ,

I am able to resolve my problem. My problem is related to accessing a external variable
from one dll to another.while I am including native C++ headers in Managed C++ headers , it is not accepting. Hence I resolved this by addding the native header file in some other appropriate native C++ file and make it work as I need.

Thanks for your support and Help.
Sudhakar M

I know you fixed your link problem. Here's a different take on the typecast.

InformDataContract->UniqueID= Marshal::PtrToStringAnsi((IntPtr) (char *)UniqueID );

Since UniqueID is declared as a simple array, eg.

char UniqueID[88];

You only need one typecast, like so.

InformDataContract->UniqueID = Marshal::PtrToStringAnsi((IntPtr)UniqueID );

If UniqueID was a CString, you'd want to typecast it to
const char *
first, but it's not so the extra typecast is unnecessary.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900