Showing posts with label Mixed Programming. Show all posts
Showing posts with label Mixed Programming. Show all posts

Wednesday, June 10, 2009

Passing Visual Basic Arrays to Assembly DLL Modules

Programmers can learn much about the innards of visual basic by doing some assembly language programming. Assembly language reveals the underlying computer architecture, and it harbors all of the secrets many programmers seek. Outside of educational value, Programmers can also write assembly modules that can be called from visual basic applications.

While a complete introduction to assembly language is beyond the scope of this article, programmers who know some of the x86 assembly language should have no trouble creating assembly DLL files for use in visual basic. The process of creating an assembly DLL file is the same as creating a DLL file in C++, and the calling convention is still standard call; however, programmers need to know some information about variable sizes and structure before they can create assembly DLL files for visual basic.

The following information is how variables correspond between visual basic and assembly language. This list is not complete, but other variables can be looked up on MSDN.

Visual basic – Assembly
byte – BYTE
Char – BYTE
boolean – WORD
Integer – WORD
Long – DWORD
Short – DWORD
String – DWORD Variable is passed by pointer.

While variables correspond quite harmoniously between visual basic and assembly, arrays are a different story. Visual basic does not store arrays in a straight forward manner; instead, visual basic stores arrays in an OLE_SAFEARRAY structure. When programmers wish to pass arrays to assembly language DLL files, they must work with the structure of the array.


SAFEARRAYBOUND struct
cElements DWORD ? ;Number of Elements
lLbound DWORD ? ; Lower Boundary
SAFEARRAYBOUND ends

OLE_SAFEARRAY struct
cDims WORD ? ; Number of dimensions
fFeatures WORD ? ; Bitfield indicating attributes
cbElements DWORD ? ; size of an element of the array
cLocks DWORD ? ; lock counter 0=Locked
pvData DWORD ? ; Pointer to data
rgsabound SAFEARRAYBOUND <> ; Contains info for dimensions
OLE_SAFEARRAY ends

Finally, here the following is an example of how to reference the structure from MASM32.

testfunction proc myArray:DWORD
mov eax, myArray
mov edx, [eax] ;EDX now has our safearray.

'Assign 1st value to eax.
mov eax, (OLE_SAFEARRAY ptr [edx]).pvData
ret
testfunction endp

Read More......

How to avoid auto string conversions in Visual Basic

When visual basic programs call outside sources like C++ DLL files, visual basic automatically converts Unicode strings into ASCII strings. After the outside function completes its task, visual basic will convert the ASCII string back into Unicode format. Sometimes the auto conversion done by visual basic can be a problem because programmers may want to keep the string in Unicode format. In order to get around the conversion, programmers should use byte arrays instead of strings.

bytearray() = mystring

The above tip can be very useful when programmers need to keep the string in Unicode format. Some windows API functions require Unicode strings to be passed in order for them to work. Hopefully Microsoft will eventually add an extra feature to allow programers to decide on the conversion process without a hack. Read More......

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.

Read More......