Results 1 to 5 of 5

Thread: Calling C++ Dlls from VB and returning info

  1. #1

    Thread Starter
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322

    Calling C++ Dlls from VB and returning info

    There are things that visual basic just sucks at doing. It’s a part of the visual basic life and it will always be that way because visual basic hides details from you. Which is a good thing, don’t get me wrong.

    Every once in a while you need to do something powerful and you’ll find visual basic to be slower then rush hour during a snow storm. So one way to get around the speed bumps of visual basic is to write the algorithm in a C++ dll and call it from visual basic. This is just a small tutorial on how to accomplish it. You have to know some console c++ in order to do this by the way =)

    What we are going to do is create a little c++ dll file that will provide a simple xor encryption.

    Step one:
    Create a win32 Dynamic Link Library project with a meaningful name.
    Add a source file and name it main.cpp
    Add a header file and name it main.h
    Add a text file and give it the same name as your project with the extension .def (Example: myproject.def)

    Step Two:
    Go to your menu and select project / settings. Click on the link tab and in the object/library modules add ws2_32.lib then select OK. You have to have that library or C++ will be a ***** about returning strings to visual basic.

    Step Three:
    In main.h add the following lines:

    #include <cstring> // This is used for strlen()

    // use __stdcall or visual basic will not be able to pass parameters to the function:
    void __stdcall xorString(char * text, const char * key);
    Step Four:
    In main.cpp add the following lines:

    #include "main.h"

    // Again, you have to use __stdcall or visual basic can’t pass parameters
    void __stdcall xorString(char * text, const char * key)
    {
    // This function takes text and xors it with a key.
    // xor is a silly cipher on its own but can be useful
    // when used with strong ciphers.

    int textSize;
    int keySize;

    textSize = strlen(text);
    keySize = strlen(key);

    for (int i = 0; i < textSize; i++)
    {
    // XOR Text chars with key chars
    text[i] = text[i] ^ key[i % keySize];
    }
    }
    Finally Step 5:
    Add the following to your projectnamehere.def file and rename the library part:
    LIBRARY "projectnamehere.dll" ;The quotes are suppose to be there =P
    EXPORTS ;List functions to export below this line =)
    xorString



    Compile it of course to make sure I didn’t make any mistakes, then go to your menu and select build / set active config. Now highlight win32 release and hit ok. Go back to your menu and select build / build projectnamehere.dll

    Go to your project file on your hard drive and inside your project folder you’ll see a folder named release. Grab the dll and copy and paste it to something easy to remember like C:\



    Now go start up a visual basic standard exe project and add a text box and a command button on the form.

    Step One:
    In the declarations of your project add the following:
    Note: Be sure to change C:\DLLFILENAMEHERE.dll to the location of the dll on your hard drive and don’t mess with the quotes!

    Private Declare Function xorString Lib "c:\DLLFILENAMEHERE.dll" (ByVal text As String, ByVal key As String) As Long
    Step 2:
    Just copy and paste this into your project:

    Private Sub Command1_Click()
    Dim str As String
    Dim key As String

    str = Text1.text ' Get Text
    key = "blah" ' Set key

    xorString str, key ' Call Dll to xor text =)

    Text1.text = str ' Show Text
    End Sub
    Run the project.....

    Now type in a sentence in the text box and click the command button. Your textbox should show some weird chars in it now, thats the cipher text. Now click the command button again and yay the text is back to normal. A very simple example of an xor encripton.

    Do note that the xor encryption doesn’t mind to throw in a null character which also happens to be something that most languages use to terminate strings with. So you would need to encode it if you actually wanted to work with it.




    How does xor encryption work?

    Well you take a text char, lets say ‘A’.
    You also take a key char, lets say ‘B’.
    Look at their binary numbers and match them up vertically


    A in binary = 0100 0001 ‘Text Char
    B in binary = 0100 0010 ‘Key Char
    ---------------------------------------------------|
    Xor them = 0000 0011 ‘Cipher Char

    If you have just one 1 in a column then set the cipher bit as 1
    If you have two 1’s in a column then set the cipher bit as 0
    If you have two 0s in a column then set the cipher bit as 0.

    A xor b = c
    C xor b = A

    Now to get the text char back:
    B in binary = 0100 0010 'key char
    Cipher char= 0000 0011
    ------------------------------
    A = 0100 0001 'Text Char.


    Its almost 4 AM and I'm out of it, so don't ***** at grammer errors...
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  2. #2
    Fanatic Member seec77's Avatar
    Join Date
    Jan 2003
    Posts
    596
    cool bro!
    this is really gonna be useful someday...
    Best Regards,
    seec77

    If you helped me, cosinder yourself thanked.

    Get each and every Garfield strip here!
    Here you can get all Calvin & Hobes strips!
    Damn UComics! It was probably unprofitable for them to allow us to just download Garfield and Calving & Hobes strips... so they made folder indexing unallowed on their server!!!

    I am 33% addicted to Counterstrike. What about you?
    I am 23% addicted to Star Wars. What about you?
    I am 0% addicted to Tupac. What about you?

  3. #3
    Junior Member
    Join Date
    Jan 2003
    Location
    53776564656E
    Posts
    24

    Lightbulb

    Thanks Maven. You just saved my day. And my week!

    Just one addendum. What I wanted to do was passing arrays of Doubles between the C++ dll and VB6. The information in this thread together with the information on
    this web site was enough for me to accomplish what I wanted to do.

    I declared my function as
    VB Code:
    1. Private Declare Function funcname Lib "c:\myfunc.dll" (ByRef ArrayA As Double,
    2. ByRef ArrayB As Double, ByVal ArraySize As Long) As Long
    and then checked my code by calling the function using something similar to
    VB Code:
    1. Dim ArrA() As Double
    2.     Dim ArrB() As Double
    3.     Dim ArrSize As Long
    4.    
    5.     ArrSize = 2
    6.     ReDim ArrA(ArrSize)
    7.     ReDim ArrB(ArrSize)
    8.    
    9.     ArrA(0) = Text1.Text
    10.     ArrA(1) = Text2.Text
    11.     funcname ArrA(0), ArrB(0), ArrSize
    12.     Text1.Text = ArrB(0)
    13.     Text2.Text = ArrB(1)
    Just notice that the Long in VB corresponds to the int in VC. This was my function head in VC:
    Code:
    void __stdcall myfunc(double *A, double *B, int arrsize);
    Last edited by jskog; Apr 23rd, 2004 at 09:43 AM.

  4. #4
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959
    So by using this method you can do all your main coding in C++ and all the GUI part in VB. Is that right ?

    How much slower would this combo of VB and c++ be as opposed to doing it all in C++ ?

  5. #5

    Thread Starter
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322
    Originally posted by Jmacp
    So by using this method you can do all your main coding in C++ and all the GUI part in VB. Is that right ?

    How much slower would this combo of VB and c++ be as opposed to doing it all in C++ ?

    Yes you would be able to do your gui in vb and coding in c++. Basically this is an example of mixed language programming.

    I've never actually sat down and profiled the differences bteween doing that and all in c++. It's a sure thing that there is overhead invovled because you have to do some things the vb way.

    On another note, when I get some free time I may do another example with ASM instead of C++.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

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