DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Passing a string from a dll to VB

  1. #1
    Steve Killick Guest

    Passing a string from a dll to VB


    Hi all,

    Thank you to Cris for helping me out with creating a random array of unsigned
    chars. Works like a charm!

    I am writing a Windows dll in C++ which encrypts a string passed from VB
    and returns an encrypted string back to VB. But, how to pass strings back
    and forth?

    The function declaration in the dll is:
    std::string _stdcall Encrypt(std::string DataIn)
    VB dosen't like this and the test VB app crashes and burns as soon as I call
    Encrypt().

    Please help.


  2. #2
    ralph Guest

    Re: Passing a string from a dll to VB


    "Steve Killick" <webmaster@cesco.com> wrote:
    >
    >Hi all,
    >
    >Thank you to Cris for helping me out with creating a random array of unsigned
    >chars. Works like a charm!
    >
    >I am writing a Windows dll in C++ which encrypts a string passed from VB
    >and returns an encrypted string back to VB. But, how to pass strings back
    >and forth?
    >
    >The function declaration in the dll is:
    >std::string _stdcall Encrypt(std::string DataIn)
    >VB dosen't like this and the test VB app crashes and burns as soon as I

    call
    >Encrypt().
    >
    >Please help.
    >


    VB never heard of a STL "string". You will have to use either a BSTR, or
    a nul-terminated char string (a 'C' or API string). The former is the easier
    to use.

    Check out the article "Introduction to Using STL with ATL by Craig McQueen"
    on the MSDN disk or at the website.






  3. #3
    Andy Friedman Guest

    Re: Passing a string from a dll to VB


    STL strings won't work between VB and C++, but you can use char*. You will
    also need to pass a second, VB-alloc'd string buffer to receive the return
    value (since VB can't free the memory alloc'd by C++). The function's return
    value can be the number of chars copied to the buffer. For example:

    [VB Code]
    'This Declare must be defined in a VB module.
    'Note the usage of ByVal and ByRef.
    Declare Function Encrypt Lib "c:\encrypt.dll" (ByVal DataIn As String, ByRef
    DataOut As String) As Integer

    Dim length As Integer
    Dim DataIn, DataOut, Encrypted As String

    DataIn = "somestring"
    DataOut = String(50, " ") 'allocate a string buffer
    length = Encrypt(DataIn , DataOut)
    Encrypted = Left(DataOut, length) 'chop off extra garbage

    [C++ Code]
    int WINAPI Encrypt(const char* DataIn, char* &DataOut)
    {
    // do encryption scheme
    ...
    // copy encrypted string into DataOut buffer
    strcpy(DataOut, encrypted_str);
    return strlen(DataOut);
    }

    I would wonder though why you wouldn't put this in a C++ COM object instead.
    It's much more natural to use COM with VB than Win32 Dlls.

    Andy

    "Steve Killick" <webmaster@cesco.com> wrote:
    >
    >Hi all,
    >
    >Thank you to Cris for helping me out with creating a random array of unsigned
    >chars. Works like a charm!
    >
    >I am writing a Windows dll in C++ which encrypts a string passed from VB
    >and returns an encrypted string back to VB. But, how to pass strings back
    >and forth?
    >
    >The function declaration in the dll is:
    >std::string _stdcall Encrypt(std::string DataIn)
    >VB dosen't like this and the test VB app crashes and burns as soon as I

    call
    >Encrypt().
    >
    >Please help.
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center