Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Managed String to Unmanaged VARIANT

Managed String to Unmanaged VARIANT

Scheduled Pinned Locked Moved Managed C++/CLI
csharpc++helptutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    picazo
    wrote on last edited by
    #1

    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

    L N 2 Replies Last reply
    0
    • P picazo

      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

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • L led mike

        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

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        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*)

        L 1 Reply Last reply
        0
        • P picazo

          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

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          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*)

          P 1 Reply Last reply
          0
          • N Nish Nishant

            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*)

            P Offline
            P Offline
            picazo
            wrote on last edited by
            #5

            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

            N 1 Reply Last reply
            0
            • P picazo

              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

              N Offline
              N Offline
              Nish Nishant
              wrote on last edited by
              #6

              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*)

              1 Reply Last reply
              0
              • N Nish Nishant

                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*)

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups