Results 1 to 15 of 15

Thread: returning a string to VB

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    returning a string to VB

    I'm trying to return a string from a C++ function to my VB app.
    My VB IDE is crashing with an exception. Could someone point out what I'm doing wrong?

    Code:
    // Header File
    #ifndef Main_H
    #define Main_H
    
    #ifndef TESTDLL
    #define TESTDLL __declspec(dllimport)
    #endif 
    
    #include <string>
    
    using namespace std;
    
    TESTDLL string _stdcall GetName();
    
    #endif
    
    // ------------------
    // CPP File
    #define TESTDLL extern "C" __declspec(dllexport)
    
    #include <string>
    #include "test.h"
    
    using namespace std;
    
    TESTDLL string _stdcall GetName()
    {
    	return "Works";
    }
    Code:
    ' Module
    Declare Function GetName Lib MyDLLPath As String
    
    ' cmdButton
    msgbox GetName
    I also have an appropriate exports.def file in my C++ project.
    Any help is greatly appreciated.
    Bababooey
    Tatatoothy
    Mamamonkey

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The C++ string class and a VB string are totally different things. Look at the thread "returning a struct from a dll" or something like that to see the solution to your problem. The thread was started by Chris and is two pages long. The thing you need is very near to the end.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    Thanks for your help CornedBee.
    I found the topic, however, I still can't figure out which
    information in that post helps me. If I'm supposed to use BStr,
    can you give me an example....I'm not terribly comfortable with
    C++ yet.

    Thanks again
    Bababooey
    Tatatoothy
    Mamamonkey

  4. #4

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    Thanks for your help CornedBee.
    I found the topic, however, I still can't figure out which
    information in that post helps me. If I'm supposed to use BStr,
    can you give me an example....I'm not terribly comfortable with
    C++ yet.

    Thanks again
    Bababooey
    Tatatoothy
    Mamamonkey

  5. #5

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    doh
    Bababooey
    Tatatoothy
    Mamamonkey

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But Chris has given the code you need. You can nearly copy it line by line.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi! noble,
    I juz work out another example for you with return a string from VC++ dll to VB through three different method.

    PHP Code:
    extern "C" __declspec(dllexportvoid MyCallback(long lAddress)
    {
        
    //
        
    char    *Buff=NULL;
        
    BSTR    bstr;

        
    // Map the External CALLBACK function
        
    if (lAddress != NULL || lAddress 0)
        {
            
    //Map the external callback function
            
    MyExGetNameProc = (ExGetNameProc)lAddress;

            
    // Create + Fillup the tmp character array
            
    Buff= new char[128];
            
    memset(Buff'\0'128);
            
    sprintf(Buff"This is the return C String to Visual Basic through the callback function.");
        
            
    // Allocate BSTR buffer
            
    bstr SysAllocStringLen(NULLlstrlen(Buff));
            
    // convert from char to BSTR
            
    MultiByteToWideChar(CP_ACP,
                    
    0,
                    (
    LPSTR)Buff,
                    
    strlen(Buff),
                    
    LPWSTR(bstr),
                    
    lstrlen(Buff));

            
    // Send the error message back to external program
            
    if (MyExGetNameProc != NULL)
                
    MyExGetNameProc(bstr);

            
    // Delete the used memory
            
    SysFreeString(bstr);
            
    delete [] Buff;

            
    // Reset the callback function pointer
            
    MyExGetNameProc NULL;
        }
    }

    extern "C" __declspec(dllexportBSTR MyOLEString(void)
    {
        
    // ** This function is mainly desing for handling passing string
        //    back to MS Visual Basic program. As MS Visual basic internally
        //      handling the string differently from MS Visual C++    

        // Purpose of this function...
        // This function is will return the following string to MS Visual Basic
        // #This is a function return a C String to Visual Basic through the BSTR (Basic STRing)
        //  as define in OLE Automation Object.

        
    char *tmp = new char[128];
        
    memset(tmp'\0'128);
        
    sprintf(tmp"This is a function return a C String to Visual Basic as BSTR (Basic STRing).");
        return 
    SysAllocStringByteLen(tmpstrlen(tmp));
    }

    extern "C" __declspec(dllexportlong GetName(LPSTR lpszBufferlong MaxBuffer)
    {
        if (
    MaxBuffer 0)
        {
            
    // Reset the passing character array
            
    memset(lpszBuffer'\0'MaxBuffer);
            
    // Copy the string into this character array
            
    sprintf(lpszBuffer"This is the return C String to Visual Basic through the pass in string buffer.");
            
    // Set the return character copy
            
    return strlen(lpszBuffer);
        }
        
    // Set return value
        
    return 0;

    regards,
    Attached Files Attached Files

  8. #8

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    thank you very much Chris! I will try it out

    CornedBee, I didn't realize the thread you were referring to was
    2 pages long and I found what you what you said. Thanks for
    your help.
    Bababooey
    Tatatoothy
    Mamamonkey

  9. #9

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    Chris, your second method using the MyOleString Function works
    great...it was just what I was looking for. The other two give
    'bad DLL calling convention' errors in VB when I run them.

    My question is, (for Chris or CornedBee) I defined a class.....we'll
    call it test. The following code is part of my class definition:

    Code:
    Class Test
         {
         public:
              string ReturnString(void) const;
              // ...
         private:
              string StringToReturn;
         };
    
    string Test::ReturnString(void) const
    {
    	return StringToReturn;
    }
    I used your code for return the OLE string :

    Code:
    extern "C" __declspec(dllexport) BSTR GetName(void)
    {
         Test TestVar;
         char *tmp = new char[128];
         memset(tmp, '\0', 128);
         sprintf(tmp, TestVar.RetrunString);
         return SysAllocStringByteLen(tmp, strlen(tmp));
    }
    When I attempt to compile the DLL, I get the following error:
    Code:
    C:\Code\Test.cpp(17) : error C2664: 'sprintf' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    What should I do to fix this?
    Thanks in advance for any help.
    Bababooey
    Tatatoothy
    Mamamonkey

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    sprintf(tmp, TestVar.RetrunString.c_str());

    string is a part of the C++ runtime library and classes and therefore unknown to sprinf that only supports c strings (char*). The c_str() method of string returns a const char* that can be used in such functions. Note that it is const, so you can't modify it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    when i use c_str() i get this error:

    Code:
    error C2228: left of '.c_str' must have class/struct/union type
    Bababooey
    Tatatoothy
    Mamamonkey

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sorry, I forgot that ReturnString is a function.
    sprintf(tmp, TestVar.RetrunString().c_str());
    should work
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    thx so much, it works great now.

    Now I have to figure out how to pass a string from VB to the C++
    DLL.

    Chris?
    Bababooey
    Tatatoothy
    Mamamonkey

  14. #14
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    hope teh fifth button does what you looking for...
    Attached Files Attached Files

  15. #15
    Member
    Join Date
    Nov 2001
    Posts
    41
    Chris,

    I run you testapp in vb IDE, tried the five button. got "bad dll calling convention" after click ok of msgbox. any suggestion?

    Thanks.

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