473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C++/CLI norms for calling functions that want String ^ when you have native char *

In MEC++ I could just call a function that was expecting a String __gc* even
though I had a native char *. The char * automatically got marshaled to a
String __gc*.

In the May CTP of VS 2005 I just get a compile-time error: cannot convert
parameter 1 from 'char *' to 'System::String ^'

What would be the normal C++/CLI way to handle this? And can I do it that
way in the May CTP?

I thought it might be the marshal_as template but I couldn't really find it
or enough information about it.

Bern McCarty
Bentley Systems, Inc.
Nov 17 '05 #1
3 2096
"Bern McCarty" <be**********@bentley.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
In MEC++ I could just call a function that was expecting a String __gc* even though I had a native char *. The char * automatically got marshaled to a
String __gc*.

In the May CTP of VS 2005 I just get a compile-time error: cannot convert
parameter 1 from 'char *' to 'System::String ^'

What would be the normal C++/CLI way to handle this? And can I do it that
way in the May CTP?


I use:

System::String * DotNetStringFromSTLString( const std::string &string_ )
{
return Marshal::PtrToStringAnsi((int*) string_.c_str());
}

which works, but for all I know is leaking like a swimming pool full of
porcupines.
Nov 17 '05 #2
Do this :-

ref class R
{
public:
void Test(String^ s)
{
}
};

void _tmain()
{
R^ r = gcnew R();
char* s = "hello world";
r->Test(gcnew String(s));
}

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
"Bern McCarty" <be**********@bentley.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
In MEC++ I could just call a function that was expecting a String __gc* even though I had a native char *. The char * automatically got marshaled to a
String __gc*.

In the May CTP of VS 2005 I just get a compile-time error: cannot convert
parameter 1 from 'char *' to 'System::String ^'

What would be the normal C++/CLI way to handle this? And can I do it that
way in the May CTP?

I thought it might be the marshal_as template but I couldn't really find it or enough information about it.

Bern McCarty
Bentley Systems, Inc.

Nov 17 '05 #3
Hi Steve

There is no leak, since PtrToStringAnsi creates a new String object and
copies the source string into it. The managed String object will get GC'd
eventually.

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com /* MVP tips tricks and essays web site */
"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:e%****************@TK2MSFTNGP12.phx.gbl...
"Bern McCarty" <be**********@bentley.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
In MEC++ I could just call a function that was expecting a String __gc*

even
though I had a native char *. The char * automatically got marshaled to a String __gc*.

In the May CTP of VS 2005 I just get a compile-time error: cannot convert parameter 1 from 'char *' to 'System::String ^'

What would be the normal C++/CLI way to handle this? And can I do it that way in the May CTP?


I use:

System::String * DotNetStringFromSTLString( const std::string &string_ )
{
return Marshal::PtrToStringAnsi((int*) string_.c_str());
}

which works, but for all I know is leaking like a swimming pool full of
porcupines.

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: Pawan Aggarwal | last post by:
I'm having trouble with calling an exported function in a native DLL compiled with eMbedded Visual C++ in C# application in PocketPC 2002 OS. Problem Description follows: I have one exported...
4
by: Steven Livingstone | last post by:
If I was using C# and wanted to call the printf function what would I do? Specifically i want to convert a string to octal and as there is no facility for doing this in C# i figured i'd look at...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
2
by: JoeB | last post by:
Hi Trying to call the following (c++) dll: void getVersionInfo( long p_lIndex, char* p_cVersionNumber, int p_iVersBufSize, char* p_cDescription, int p_iDescBufSize, BOOL& p_bCompulsory ); ...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
4
by: Henning M | last post by:
Hej All Im relativ new to VB.net and im trying to collect som device information using cfgmgr32.dll I use - Declare Function GetListLength Lib "cfgmgr32.dll" Alias...
2
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.