|
-
Jun 27th, 2003, 02:50 AM
#1
Thread Starter
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|