CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63

    OCX return a string value to VB

    hi
    I have a VC++ 6.0 OCX which is used by a VB 6.0 program.
    I need to send a string as a argument and have the OCX change
    the string's value, how can I do that?

    The function looks something like this :

    void MyClass::changeStringValue(String_Pointer str1, String_Pointer str2)
    {
    char *c = "anything 2";
    *str1 = "anything";
    strcpy(str2, c...
    memcpy(str2, c....
    //neither copy function worked
    }

    This method worked for double and long values, it won't work
    for strings(i don't know how to declare a String_Pointer in OCX
    functions that are visible to VB)

    thanks

    awni

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585
    Try using a CString as the parameter. CStrings are converted to BSTR (an automation type) in the .odl file of an ActiveX control. Visual Basic should know how to handle a BSTR.
    Gort...Klaatu, Barada Nikto!

  3. #3
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63
    no luck

    I even tried VARIANT instead of CString , didn't work.
    BSTR, LPCSTR didn't either , but LPCSTR accepts strings as
    input , BSTR type gives a Type mismatch error.


    thanks
    awni

  4. #4
    Join Date
    Nov 2002
    Posts
    110
    How about do it the simpler but not as efficient way:

    1. VB program pass the string to the ocx (LPCTSTR)
    2. ocx return a modified the string to VB program (BSTR)
    3. VB program use the returned string to overwrite the original string

  5. #5
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63
    that works real good for single string returns, like

    Code:
     
    BSTR ReturnString()
    {
           return a_BSTR
    }
    but if i need to change 2 or more strings like
    Code:
    void Change2Strings(String_Pointer s1, String_Pointer s2)
    {
          *s1 = "something";
          *s2 = "something else";
    }
    then I'd need something else...

    I wonder if allocating an array of type BYTE, with a single
    element and allocating a string immediatly after that like

    Code:
       dim b(1) as byte
       dim s as String
       dim s2 as String
    I wonder if that would store the string right after the array
    in memory.

    I can then pass the array to Visual C++. VB passes the address
    of the first element, so if the string follows the array in memory, i can just add 1 to the location of the array, and i've got me the address of the first string, i have no idea how to get to the second string though, maybe add 255? (why am i telling you this?)
    I'll try it if i get a chance, and let you know(if i get a chance)

    thanks

    awni

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Since it's an OCX, you have to use IDL types and you really should use BSTRs, which is the standard string type for COM.

    So, in your OCX's idl, declare a function like this:
    Code:
    // idl
    [id(10), helpstring("method ChangeStrings")] HRESULT ChangeStrings([in, out] BSTR *pFirst, [in, out] BSTR *pSecond);
    Then in your C++ code, write:
    Code:
    HRESULT MyOCX::ChangeStrings(BSTR *pFirst, BSTR *pSecond)
    {
      // Change the strings
      BSTR tmp = *pSecond;
      // concatenate both strings into pSecond and set pFirst to an empty string
      *pSecond = SysAllocStringLen(*pFirst, SysStringLen(*pFirst) + SysStringLen(*pSecond));
       wcscpy(*pSecond + SysStringLen(*pFirst), tmp);
       SysFreeString(*pFirst);
       SysFreeString(tmp);
       *pFirst = SysAllocString(0, 0);
       return S_OK;
    }
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63
    thanks everyone
    Yves, where's a good source of information about
    passing different types between VB and VC?

    thanks

    awni

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Check in MSDN under:

    Component Development > Microsoft Interface Development Language (midl) > SDK documentation > Interface Definitions and Type Libraries > MIDL data types.

    There you have all the raw information. In any case, if you are a bit unsure what type you should use, but you know what it should be in VB, make a VB COM dll with functions that take resp. return these types and inspect the resulting dll in the OLEViewer.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  9. #9
    Join Date
    Jul 2001
    Location
    jordan
    Posts
    63
    thank you that was very helpful. I managed to pass/return types
    I didn't know how to pass/return before. There are still some that
    I'm having problems with, such as user defined types. I did see
    them in OLEViewer, but I don't know how to make them visible
    in both VB and VC. Any ideas?

    thanks

    awni

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured