Managed String to Unmanaged VARIANT
-
Hello, I have the following unmanaged method:
BSTR UnmanagedClass::UnmanagedMethod( BSTR x, VARIANT y, BSTR z ) { ... }
I am wrapping that method with the following C++/CLI method:
String^ ManagedClass::ManagedMethod( String^ x, String^ y, String^ z ) { CComBSTR bstrX, bstrZ; bstrX.Attach( ( BSTR )( void * )Marshal::StringToBSTR( x ) ); bstrZ.Attach( ( BSTR )( void * )Marshal::StringToBSTR( z ) ); ???? return gcnew String( unmanaged->UnmanagedMethod( bstrX, ???, bstrZ ) ); }
The C++/CLI wrapper method will be accessed from C# as follows:
string x="some value", y="another value", z="a third value"; managed.ManagedMethod( x, y, z );
The problem I am having is converting the managed string handle (String^ y) to an unmanaged VARIANT parameter. I would like to be able to solve this problem without having to modify the unmanaged C++ code. Does anyone have any suggestions or ideas on how to do this? Thanks in advance,
----------------- Genaro
-
Hello, I have the following unmanaged method:
BSTR UnmanagedClass::UnmanagedMethod( BSTR x, VARIANT y, BSTR z ) { ... }
I am wrapping that method with the following C++/CLI method:
String^ ManagedClass::ManagedMethod( String^ x, String^ y, String^ z ) { CComBSTR bstrX, bstrZ; bstrX.Attach( ( BSTR )( void * )Marshal::StringToBSTR( x ) ); bstrZ.Attach( ( BSTR )( void * )Marshal::StringToBSTR( z ) ); ???? return gcnew String( unmanaged->UnmanagedMethod( bstrX, ???, bstrZ ) ); }
The C++/CLI wrapper method will be accessed from C# as follows:
string x="some value", y="another value", z="a third value"; managed.ManagedMethod( x, y, z );
The problem I am having is converting the managed string handle (String^ y) to an unmanaged VARIANT parameter. I would like to be able to solve this problem without having to modify the unmanaged C++ code. Does anyone have any suggestions or ideas on how to do this? Thanks in advance,
----------------- Genaro
-
picazo wrote:
Marshal::StringToBSTR
Returns an IntPtr. You can't just cast that to a BSTR you have to use a method like ToInt32().
led mike
led mike wrote:
You can't just cast that to a BSTR you have to use a method like ToInt32().
ToPointer would be a better method to use there.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
Hello, I have the following unmanaged method:
BSTR UnmanagedClass::UnmanagedMethod( BSTR x, VARIANT y, BSTR z ) { ... }
I am wrapping that method with the following C++/CLI method:
String^ ManagedClass::ManagedMethod( String^ x, String^ y, String^ z ) { CComBSTR bstrX, bstrZ; bstrX.Attach( ( BSTR )( void * )Marshal::StringToBSTR( x ) ); bstrZ.Attach( ( BSTR )( void * )Marshal::StringToBSTR( z ) ); ???? return gcnew String( unmanaged->UnmanagedMethod( bstrX, ???, bstrZ ) ); }
The C++/CLI wrapper method will be accessed from C# as follows:
string x="some value", y="another value", z="a third value"; managed.ManagedMethod( x, y, z );
The problem I am having is converting the managed string handle (String^ y) to an unmanaged VARIANT parameter. I would like to be able to solve this problem without having to modify the unmanaged C++ code. Does anyone have any suggestions or ideas on how to do this? Thanks in advance,
----------------- Genaro
You could do a String^ -> BSTR -> VARIANT.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
You could do a String^ -> BSTR -> VARIANT.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
could you provide some sample code of how to do this? I thought of doing that, but I couldn't get it to work. Thanks,
----------------- Genaro
picazo wrote:
could you provide some sample code of how to do this? I thought of doing that, but I couldn't get it to work.
Since you've already written code to convert String^ to BSTR, I presume your problem is with converting that to a VARIANT. To do that, you can use _variant_t which has a constructor that takes a BSTR, and use that. You can pass _variant_t anywhere a VARIANT is expected.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*) -
led mike wrote:
You can't just cast that to a BSTR you have to use a method like ToInt32().
ToPointer would be a better method to use there.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. (*Sample chapter available online*)Nishant Sivakumar wrote:
would be a better method to use
Sure but my level of apathy for people writing production code that can't blow their own nose is fairly high today. :-D I feel like starting a new CodeProject account with the user name "HellNo" for replying to all the "can you provide the code" lovelies. Or maybe I shoudl write a programming book titled "Copy Paste for Idiots"... wait...
Chapter One
Copy the following code and paste it into your code file at the appropriate location:
int n = 11;
Now modify the code until it produces the desired results.
There finished... now all I need is to send it off to a publisher.
led mike