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. COM
  4. How to pass a string into COM

How to pass a string into COM

Scheduled Pinned Locked Moved COM
comhelptutorial
6 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I'm a begineer in COM. Please anyone help me. I have to pass a String value(CString) from an edit box into COM Object(LPCTSTR). ThankYou

    P S 2 Replies Last reply
    0
    • L Lost User

      I'm a begineer in COM. Please anyone help me. I have to pass a String value(CString) from an edit box into COM Object(LPCTSTR). ThankYou

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      You need to do something like this where your CString is called cString, and your com function is pInt->Fuction():

      BSTR bstr;

      bstr = ::SysAllocString(cString);
      pInt->Function(bstr);
      ::SysFreeString(bstr);


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      S L 2 Replies Last reply
      0
      • P Paul M Watt

        You need to do something like this where your CString is called cString, and your com function is pInt->Fuction():

        BSTR bstr;

        bstr = ::SysAllocString(cString);
        pInt->Function(bstr);
        ::SysFreeString(bstr);


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        S Offline
        S Offline
        Sayan Mukherjee
        wrote on last edited by
        #3

        Just an observation: If the requirement is only to send the contents of the CString to the server (as in this case), then the solution is okay. If the server changes it and the client needs it back, a BSTR * will be required. The IDL should have [in] BSTR bstr in the first case. For the second type it should have [in, out] BSTR *pbstr With best regards, Sayan Email:sayanmukherjee@indiatimes.com

        1 Reply Last reply
        0
        • P Paul M Watt

          You need to do something like this where your CString is called cString, and your com function is pInt->Fuction():

          BSTR bstr;

          bstr = ::SysAllocString(cString);
          pInt->Function(bstr);
          ::SysFreeString(bstr);


          Build a man a fire, and he will be warm for a day
          Light a man on fire, and he will be warm for the rest of his life!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks for you Help. I did as you explained. But it's showing an error. error C2664: 'SysAllocString' : cannot convert parameter 1 from 'class CString' to 'const unsigned short *' Please explain

          M 1 Reply Last reply
          0
          • L Lost User

            I'm a begineer in COM. Please anyone help me. I have to pass a String value(CString) from an edit box into COM Object(LPCTSTR). ThankYou

            S Offline
            S Offline
            soup
            wrote on last edited by
            #5

            The CString object exposes exactly that datatype as a cast, so do the following: myobjectvariable.myobjectmethod((LPCTSTR) myCString, ...); Should do the trick, hope it helps. Simon

            1 Reply Last reply
            0
            • L Lost User

              Thanks for you Help. I did as you explained. But it's showing an error. error C2664: 'SysAllocString' : cannot convert parameter 1 from 'class CString' to 'const unsigned short *' Please explain

              M Offline
              M Offline
              Matt Philmon
              wrote on last edited by
              #6

              There's a few things you need to take into account. Your COM object's interface that you're trying to reach... did you implement it using IUnknown or IDispatch? IUnknown will let you more or less pass any kind of data you want including good 'ol CHAR and CHAR* type stuff. IDispatch, which is required if your component is or ever will be used in Visual Basic or any scripting language (JavaScript or VB Script), is much more limited. From my experience and I don't doubt some will disagree, IDispatch is "safer" in most cases regardless of whether your object is needed in VB or not (particularly if you have to support Windows 95/98). IDispatch, however, greatly narrows down your options to a degree on types. Sure, there's ways around this restriction, but basically with IDispatch you have some basic integer types... after that you really have to use BSTR's and VARIANT's. These are a real pain in the... keister, particularly in C++ but you'll have better results. The solution using SysAllocString mentioned above would be the correct usage if you're using IDispatch and passing a BSTR. I'm gathering from the type you mentioned that you chose IUnknown. In this case, it's really quite simple, CString strData("Hello World"); // Load your smart pointer to your object IMyInterfacePtr pInterface; HRESULT hr = pInterface.CreateInstance(__uuidof(MyCoClass)); if (SUCCEEDED(hr)) { hr = pInterface->YourFunctionCall(strData.GetBuffer(strData.GetLength())); if (SUCCEEDED(hr)) { } else { } strData.ReleaseBuffer(); }

              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