Thread: Sending a string to C++ from VB 6.

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Sending a string to C++ from VB 6.

    Currently I'm rewriting an application for Win32. I just lost all of the source so I'm forced to re-write the code. This is a joint effort with another programmer, but he is using VB 6. I have a copy of this and have been studying VB quite a bit recently.

    My question is: How do I send a string from VB to C++ without using some third party software?

    I realize that I could just take the segment and offset of the string in VB and send that to the C++ function that operates on the string. I could use the string by using an offset into the string from the starting offset. However, this all feels quite clunky and very abstract.

    If we declare the variable to be of type Variant and pass BYREF to the C++ function, will we get the string we want or will we get garbage. How should we code the C++ function to receive the string from VB? Since VisualBasic deals with strings a bit different than C++, this is posing quite a problem.

    I realize that there are third party software vendors that allow multi-language projects to communicate with each other, but we would like to avoid the obvious overhead involved in such a program.

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227

    well...

    I think that you can always have him compile everything into an object file, and then link the object file with your files, and use it that way...

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I'm assuming you are talking about a DLL or OCX or something like that here, so you can send parameters.

    >How do I send a string from VB to C++

    Declare it as const char* in the C function.
    Declare it as ByRef String in VB.
    ( Test it with ByVal if it gets you garbage, it's been some
    time since I did this... )

    This way you should be able to get the string across.
    The other way 'round is a pain in the a##.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    No Genius That's For Sure
    Join Date
    Aug 2001
    Location
     >÷' >ø' >ù' >ú'&>û' CIS MajorIndiana CNC operator >ü'&>ý'FranceSkateboarding, Cno job7>þ' Swindon, UKCycling & ChessGraphic Analyst
    Posts
    127
    Compile your C++ as a dll.

    In the VB you declare the function you want from the C++ class the same way as you would declare an API function call.

    Then you just use the C++ function in the VB.

    I am not sure that there is a way to turn this around so that you are compiling VB to use inside C++. I think this is because adding VB to C++ slows down the C++ program. Adding C++ to VB- speeds up the VB program.

    If you want to see an example of C++ used to enhance VB code you can check out the January 2001 issue of VB Programmer's Journal.

    I do not know if the code would still be available from devx.com but it might be worth looking if you can't dig up an actual copy of the magazine.
    He is no fool who gives up what he cannot keep in order to gain what he cannot lose.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Thank you all for replying. You have given us several different ways to approach this and we appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM