Wednesday, June 10, 2009

Calling C++ DLL files from Visual Basic

Since visual basic hides many details, programmers are limited on control. In general, programmers benefit from fewer details because they save on development time; however, some tasks are difficult or impossible to do without having access to more details then what visual basic provides. In this article, I'm going to illustrate how programmers can interface a C++ DLL with Visual Basic so that programmers can use C++ to solve specific problems with a visual basic program.

I am going to make the assumption that programmers know how to program in C++ and that they are using the Visual C++ compiler. Other compilers should still work, but programmers may have to check documentation for creating window DLL projects. Programmers who don't know C++ will not benefit from this article because they must learn the C++ language first, and teaching C++ in one article is impossible.

Step 1: Open Visual C++
a. Create a win32 Dynamic Link Library project with a meaningful name.
b. Add a source file and name it main.cpp
c. Add a header file and name it main.h
d. Add a text file and give it the same name as the project with the extension .def (Example: myproject.def)

Step 2. Add a reference to ws2_32.lib
a. In the main menu, select project and then settings.
b. Click on the link tab.
c. In the object and library modules, add ws2_32.lib
d. Hit ok.

The ws2.32.lib library is required for our project because it is used when working with visual basic strings.

Step 3. Include the following code in main.h






#include // 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);

The xorString function is the export function that we are going to call from visual basic. The function is going to accept a message string and a key string from visual basic. The output of the function is going to be a childishly simple encrypted message. Programmers should NEVER rely on the xor algorithm for securing information because it can be broken very quickly. In this article, the xor algorithm is being used for an example only.

Step 4. Include the following code in main.cpp




#include "main.h"

using namespace std;

// 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 such as AES.

int textSize;
int keySize;

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

for (int i = 0; i <>

Step 5. Include the export function in the definition file. The following should be included in the text file we changed into a .def file in step 1.





LIBRARY "projectnamehere.dll" ;The quotes are suppose to be there.
EXPORTS ;List functions to export below this line.
xorString ; The function we wish to call from visual basic.

Step 6. Save and build the DLL file.

After the DLL file is built by the compiler, it will need to be located on the hard drive. The file should be in the project folder under debug or release. After the DLL file is found, it should be moved to another location so that it will be easier to remember. I would personally recommend C:\. Next, we will call the DLL file from visual basic.

Step 1. Open visual basic.
a. Select windows application from the project menu.
b. On the form, add a command button named Command1.
c. On the form, add a text control named Text1

Step 2. In code view, paste the following declaration at the top of the file.





Private Declare Function xorString Lib "c:\DLLFILENAMEHERE.dll" (ByVal text As String, ByVal key As String) As Long

The above code is our function declaration, and it tells visual basic where to find our DLL file, the name of the function, parameters the function expects, and the return type of the function.

Step 3.
Add the following code to the module.





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

After the above code has been added, the project can be executed. Simply type some text into the text box and click the command button. The text should change to weird looking characters. After clicking the command button a 2nd time, the text should change back to normal.

No comments:

Post a Comment