String Conversions in Managed C++ 
Author Message
 String Conversions in Managed C++

Two questions:

1. Is there a newsgroup devoted specifically to managed
C++?

2. I've played around in C++ for some time, but am
completely new to the managed extensions.  I am
attempting to create a library that wraps library
functions that take char* parameters. Of course, these
params must be exposed to the managed caller as
System::String objects.

What's the best way to perform this conversion?  Anyone
got an example?

Regards,

Stephen



Fri, 14 Jan 2005 03:11:45 GMT  
 String Conversions in Managed C++

Quote:

>Two questions:

>1. Is there a newsgroup devoted specifically to managed
>C++?

AFAIK, this is the right place.

Quote:
>2. I've played around in C++ for some time, but am
>completely new to the managed extensions.  I am
>attempting to create a library that wraps library
>functions that take char* parameters. Of course, these
>params must be exposed to the managed caller as
>System::String objects.

>What's the best way to perform this conversion?  Anyone
>got an example?

For functions in DLLs, use Platform Invoke (aka P/Invoke and PInvoke).
Alternatively, and to interact with non-DLL functions, use methods of
class Marshal. Here's an example of printing a String* using the
Standard C library function puts().

#using <mscorlib.dll>
using namespace System;

#include <stdio.h>

void do_puts(String* s)
{
   using namespace System::Runtime::InteropServices;

   IntPtr p = Marshal::StringToHGlobalAnsi(s);
   // Call unmanaged function.
   puts(static_cast<const char*>(p.ToPointer()));
   Marshal::FreeHGlobal(p);

Quote:
}

int main()
{
   String* s = S"doug";
   // puts(s); // Fails - cannot convert from String* to char*
   do_puts(s);
   return 0;

Quote:
}

This is all you need to use immutable strings. To get a string you can
modify, use StringBuilder.

--
Doug Harrison
Microsoft MVP - Visual C++
Eluent Software, LLC
http://www.eluent.com
Tools for VC++, VS.NET, and Windows



Fri, 14 Jan 2005 04:49:24 GMT  
 String Conversions in Managed C++
This is most helpful.  Thanks very much.



Quote:

> >Two questions:

> >1. Is there a newsgroup devoted specifically to managed
> >C++?

> AFAIK, this is the right place.

> >2. I've played around in C++ for some time, but am
> >completely new to the managed extensions.  I am
> >attempting to create a library that wraps library
> >functions that take char* parameters. Of course, these
> >params must be exposed to the managed caller as
> >System::String objects.

> >What's the best way to perform this conversion?  Anyone
> >got an example?

> For functions in DLLs, use Platform Invoke (aka P/Invoke and PInvoke).
> Alternatively, and to interact with non-DLL functions, use methods of
> class Marshal. Here's an example of printing a String* using the
> Standard C library function puts().

> #using <mscorlib.dll>
> using namespace System;

> #include <stdio.h>

> void do_puts(String* s)
> {
>    using namespace System::Runtime::InteropServices;

>    IntPtr p = Marshal::StringToHGlobalAnsi(s);
>    // Call unmanaged function.
>    puts(static_cast<const char*>(p.ToPointer()));
>    Marshal::FreeHGlobal(p);
> }

> int main()
> {
>    String* s = S"doug";
>    // puts(s); // Fails - cannot convert from String* to char*
>    do_puts(s);
>    return 0;
> }

> This is all you need to use immutable strings. To get a string you can
> modify, use StringBuilder.

> --
> Doug Harrison
> Microsoft MVP - Visual C++
> Eluent Software, LLC
> http://www.eluent.com
> Tools for VC++, VS.NET, and Windows



Fri, 14 Jan 2005 21:04:13 GMT  
 String Conversions in Managed C++
The common language runtime provides Platform Invocation Services, or
PInvoke, that enables managed code to call C-style functions in native
dynamic-linked libraries (DLLs). The same data marshaling is used as for
COM interoperability with the runtime and for the "It Just Works," or IJW,
mechanism.

For Example:

#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

[DllImport("msvcrt", EntryPoint="puts")]
extern "C" int puts([MarshalAs(UnmanagedType::LPWStr)] String *);

int main()
{
   String * pStr = S"Hello World!";
   puts(pStr);

Quote:
}

YOu can refer to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmx...
ml/vcManagedExtensionsSpec_10.asp

Best Regards

leolin



Mon, 17 Jan 2005 16:30:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. conversion from managed System::String to unmanaged UUID?

2. explicit / implicit conversion in managed C++

3. String conversion and double-to-string conversion

4. String conversion and double-to-string conversion

5. String conversion and double-to-string conversion

6. Managed C++ String to char*

7. Managed c++ web service can't receive variables of type String

8. Passing System::String to Managed C++ Functions

9. managed c++: how convert String in LPCSTR ?

10. System::String *sa and LPTSTR in new C++ managed (studio 7)

11. System::String *sa and LPTSTR in new C++ managed (studio 7)

12. using Managed dlls from Managed C++

 

 
Powered by phpBB® Forum Software