Interaction between VB .NET and C/++


So you have some trouble building your application in C as the GUI is taking you a lot of time. You turn on your C/++ compiler and open up your project and thinks that it is very time consuming and you drop the idea and open up Just Cause 2 instead. Well i have a solution for you but you might think that it is far more complicated than the chicken and egg ideology.  Let us proceed to what i have in mind first.  

Solution

– By using VB .NET as our primary structure and C/++ as our backbone.  This can be done when you build your functions via C/++ in a dynamic link library (.dll) and your GUI that is built in VB .NET loads them.

Requirements

– Knowledge in C/++ and VB .NET
– Microsoft Visual C++ 6.0 and Microsoft Visual Studio 2008 (i’m using 1 for C and 2 for VB)
– some knowledge in building dynamic link library (.dll)  

Sample/ prototype

– i have built a simple calculator in VB .NET that performs 4 function which are (1.) Addition, (2.) Subtraction, (3.) Multiplication and (4.) Division.  However the functions are built in C form and it is placed in a .dll file which i have built as math.dll
  

Steps

Assuming that you already know how to build projects in Microsoft Visual Studio 2008, create a windows form project for VB .NET and drag the necessary controls as shown above. I’m not entirely good with the features provided in wordpress so i’ll be explaining the codes in detail rather than pasting the whole thing in a [ code][ /code] tag because numbers would show beside the codes and i think it’s a little messy this way.  

a
b
c

see what i mean?  

If you’ve already prepared the GUI in VB .NET lets place the respective codes for each controls.  

First place the codes below above all functions in VB .NET under Public Class Form1 

Declare Function Addition Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Declare Function Subtraction Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Declare Function Multiplication Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Declare Function Division Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer

Addition (RadioButton)
Private Sub rdAddition_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdAddition.CheckedChanged
lblEquate.Text = “+” ‘take note that lblEquate is the Label control between the textboxes on the left side
End Sub

Subtraction (RadioButton)
Private Sub rdSubtraction_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdSubtraction.CheckedChanged
lblEquate.Text = “-”
End Sub

Multiplication (RadioButton)
Private Sub rdMultiplication_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdMultiplication.CheckedChanged
lblEquate.Text = “*”
End Sub

Division (RadioButton)
Private Sub rdDivision_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdDivision.CheckedChanged
lblEquate.Text = “/”
End Sub

> (Button)
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If rdAddition.Checked = True Then
txtResult.Text = Addition(Val(txtNum1.Text), Val(txtNum2.Text))
ElseIf rdSubtraction.Checked = True Then
txtResult.Text = Subtraction(Val(txtNum1.Text), Val(txtNum2.Text))
ElseIf rdMultiplication.Checked = True Then
txtResult.Text = Multiplication(Val(txtNum1.Text), Val(txtNum2.Text))
ElseIf rdDivision.Checked = True Then
txtResult.Text = Division(Val(txtNum1.Text), Val(txtNum2.Text))
End If
End Sub

(TextBox)
as you can see there’s 3 textboxes. From left will be txtNum1, txtNum2 and txtResult

I think everything so far is pretty much understandable except for the declaration part.  What each does is that it loads the function from a .dll file called math.dll and “Addition” for example is the function name.  As you can see there are 2 parameters which are Integer num1 and Integer num2 and it returns an Integer.

Try running the executable file you built (and not from the compiler) instead. Generally you’ll find error occur when you placed 2 numbers in the txtNum1 and txtNum2 and hit the button “>” because it couldn’t find math.dll

The C/++ part

Now open up your C/++ compiler and create a Win32 Dynamic-Link Library using Microsoft Visual C++ 6.0 (i’m not sure where do i create one from Microsoft Visual Studio 2008)

#define WIN32_LEAN_AND_MEAN
#include

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

As you can see from the above, it is the primary strucutre for a .dll, while we use main for basic console programming we are required to use DllMain for .dll.  I hope you get what i’m trying to say here.

__declspec(dllexport) short Addition(short num1, short num2){
return num1 + num2;
}

__declspec(dllexport) short Subtraction(short num1, short num2){
return num1 – num2;
}

__declspec(dllexport) short Multiplication(short num1, short num2){
return num1 * num2;
}

__declspec(dllexport) short Division(short num1, short num2){
return num1 / num2;
}

The above code is the rest of the required codes.  Find __declspec under msdn homepage and you’ll find what it is about.  Once you have built the project, place the math.dll that you have created in the same folder as your VB .NET’s executable (debug or release).  Try and run the VB .NET executable and use the “>” button and you’ll find it calculating fine.

Enhancement

By using this method, i am pretty sure that you can unravel more mysteries between VB .NET and C/++.  With this concept applied, you are not required to build a GUI from scratch.  Although there may be a gap while building the 2 programs seperately as you may find it easily exploited – you might want to consider on protecting your .dll.

Support

Check out http://www.flipcode.com/archives/Interfacing_Visual_Basic_And_C.shtml for more information on interacting VB .NET and C/++.  As you might be wondering why did i use Integer datatype in VB .NET and short in C you could find the answer pretty much all of it in this site including __declspec.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: