Need Help passing a string from VB into a C++ DLL??

This is a discussion on Need Help passing a string from VB into a C++ DLL?? within the C++ Programming forums, part of the General Programming Boards category; [FONT=Arial] I am a beginner and I cannot figure out how to pass a string from a VB calling program ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    Need Help passing a string from VB into a C++ DLL??

    [FONT=Arial]

    I am a beginner and I cannot figure out how to pass a string from a VB calling program to a C++ DLL. All I get in the C routine is garbage.

    The "Declare" argument in VB is:
    (ByRef s As String,.....


    And the string looks like:

    fln = "D:\\bin\\Data.txt"


    The VB call is:

    err = load_data(fln,...


    On the C++ side I have:

    int __stdcall load_data(char *s,...

    then I use:
    if(strlen(s) > 0)
    {
    strcpy(file_name,s);
    data_file = fopen(file_name,"rt");
    if(data_file != NULL)
    file = true;
    }
    This fails every time and I am forced to call a find file dialog. I need it not to fail and can not figure out why it does.

    A HUGE thank you if you can help.

    Dan
    Last edited by dan Burke; 04-23-2004 at 01:39 PM. Reason: fix a couple of errors

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Strings in VB are passed to C/C++ as BSTR. These can be casted into char * directly. But beware that BSTRs have 4 bytes before the string itself which holds the size of the string. The BSTR actually points to the string, though.

    Useful VB to C++ site:
    http://www.flipcode.com/articles/article_vbdlls.shtml

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your code is correct, except that you need to use ByVal for strings rather than ByRef.

    Returning a string from C to VB is slightly more complicated. The best way is to allocate the string in vb.

    Code:
    s = space(260)
    some_c_function(s)
    MsgBox s
    
    void __stdcall some_c_function(char * s)
    {
         strcpy(s, "Hello from C");
    }

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Excellant! Thank You

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    3
    Excellant! Thank You as well...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Sending a string to C++ from VB 6.
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2001, 02:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21