static std::string exposed through System::String * 
Author Message
 static std::string exposed through System::String *

I am working on a mc++ class library. I have a static std::string that I
would like to expose to the managed world.

struct Unmanaged
{
    std::string s1;

Quote:
}

std:string Unmanaged::s1 = "mystring";

I have tried the following:

class __gc ManagedAttempt1
{
   static  __property System::String __gc * get_myString()
    {
        return s1.c_str();
    }

Quote:
}

class __gc ManagedAttempt2
{
   static  __property System::String __gc * get_myString()
    {
        System::IntPtr ptr = System::IntPtr( reinterpret_cast< int >(
s1.c_str() ) );
        return System::InteropServices::MarshalPtrToStringAnsi( ptr );
    }

Quote:
}

Both return the string as designed. However, every so often I get a
NullReference exception. I assume it is during garbage collection. Can
anyone please comment.

George Economos



Tue, 08 Mar 2005 22:38:02 GMT  
 static std::string exposed through System::String *
sorry, should be

struct Unmanaged

{

static std::string s1;

Quote:
}

std:string Unmanaged::s1 = "mystring";


Quote:
> I am working on a mc++ class library. I have a static std::string that I
> would like to expose to the managed world.

> struct Unmanaged
> {
>     std::string s1;
> }
> std:string Unmanaged::s1 = "mystring";

> I have tried the following:

> class __gc ManagedAttempt1
> {
>    static  __property System::String __gc * get_myString()
>     {
>         return s1.c_str();
>     }
> }

> class __gc ManagedAttempt2
> {
>    static  __property System::String __gc * get_myString()
>     {
>         System::IntPtr ptr = System::IntPtr( reinterpret_cast< int >(
> s1.c_str() ) );
>         return System::InteropServices::MarshalPtrToStringAnsi( ptr );
>     }
> }

> Both return the string as designed. However, every so often I get a
> NullReference exception. I assume it is during garbage collection. Can
> anyone please comment.

> George Economos



Tue, 08 Mar 2005 22:49:31 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. int to std::string to System::String

2. System::String to std::string

3. System.String to std::string conversion

4. convert non std::string to std::string

5. static std string troubles.

6. String to std:string

7. null-terminated string to std::string

8. using std::string and string::npos...

9. Assignind a NULL C string to an std::string

10. convert between std::string and std::wstring

11. Warnings for std::vector<std::string>

12. typedef std::vector<std::string> Vector_String

 

 
Powered by phpBB® Forum Software