Results 1 to 6 of 6

Thread: Passing binary strings with ActiveX

  1. #1

    Thread Starter
    Lively Member ExciteMouse's Avatar
    Join Date
    Jul 2000
    Location
    Dallas, TX
    Posts
    78

    Question

    hello.

    I am making an ActiveX control in MFC that is supposed to work with Visual Basic.

    I want to make a function in the control that will return a binary string of non-constant size (unsigned char*)

    Let's just make the example easy...
    In the control there is a function called:
    GetFileData(LPCSTR FileName)

    how would i return the innards of the file to the user correctly? and this is a binary file, not a text file.

    thanks in advance

  2. #2

    Thread Starter
    Lively Member ExciteMouse's Avatar
    Join Date
    Jul 2000
    Location
    Dallas, TX
    Posts
    78

    Exclamation one more thing

    and i want to pass it back to the VB user as 'String' not as a byte array.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you pass it back as a null-terminated char* then VB will be able to interpret it as a string.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Lively Member ExciteMouse's Avatar
    Join Date
    Jul 2000
    Location
    Dallas, TX
    Posts
    78

    Exclamation

    but since this is a binary string, it may contain nulls in it allready. I want to return a varible declared as string in VB that is basically Unsigned Char* in C++.
    does that make sence?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I'd probably have a C prototype similar to this:
    Code:
    void GetData(unsigned char** ppubData, unsigned long *pulLength);
    Then in VB:
    Code:
    Declare Sub GetData ... (ByRef ppubData() As Byte, ByRef pulLength As Long)
    Dim dat() as Byte
    Dim len as long
    
    GetData dat, len
    I can't check that at the moment, but I think the theory's fairly sound.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Lively Member ExciteMouse's Avatar
    Join Date
    Jul 2000
    Location
    Dallas, TX
    Posts
    78

    Cool Finally got it.

    ok if anyone cares, this is how you do it with MFC C++

    in the ActiveX control or DLL you setup a function like so:

    Code:
    BSTR ReturnBinaryString() 
    {
    	CString strResult;
    	
    	//Buffer the string to how ever long
    	strResult.GetBufferSetLength(10);
    
    	//theres a null at position 3 of the 10 byte string
    	strResult.SetAt(3, '\0');
    
    	//return a SYSSTRING so you do not get dealloction leak
    	//(System will take care of deallocation for you)
    	return strResult.AllocSysString();
    
    	//now in VB this will return as a string of 10 bytes with a null at position 3.
    }
    *whew*

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