PHP Web Host » visual basic using c++ dll /blog Software / Web development related blogs! Wed, 29 Sep 2010 05:25:40 +0000 en hourly 1 http://wordpress.org/?v=3.0 Compiling a C++ dll with MinGW to use with Visual Basic /blog/software/software_development/c/compiling-a-c-dll-with-mingw-to-use-with-visual-basic/ /blog/software/software_development/c/compiling-a-c-dll-with-mingw-to-use-with-visual-basic/#comments Tue, 16 Feb 2010 16:30:44 +0000 jsmcm http://www.softsmart.co.za/blog/?p=85 Using a dll compiled with C++ in visual basic is really rather easy, its just a little hard to find good (EASY) information of how to do it. There is a fair amount of information for Microsoft’s C++ compilers, but what about us who write in a text editor and compile with g++ on MinGW? This post will show you how you can be using a c++ dll in visual basic.

I’m a big fan of ANSI C++, MinGW and the g++ compiler. I don’t like MS C++ compilers for various reasons. One of those is that I don’t believe it generates ANSI compliant code, and I certainly can’t compile for Linux from it.

If you are a visual basic (VB) coder, it may be necessary at times to use a dll compiled in C++. Or, you may be in a similar situation to myself: all of your projects are written in VB and you are porting everything over to C++. However, you have a problem. Porting everything at once will take a long time. If you port a module to C++, you would want to start using that module straight away with your existing VB projects (until you have ported the whole lot).

So, here is a quick guide on accomplishing that. This guide is very simple and is intended to get you started in the right direction.

Create a cpp file called “test.cpp”. In this file, paste the following code:

// Start of file

extern “C” // only required if using g++
{

__declspec (dllexport) int __stdcall FooBar (void)
{
return 343;
}

__declspec (dllexport) int __stdcall TimesTwo (int val)
{
return val * 2;
}

}

// end of file

Now we need to compile our dll. There are two steps (maybe they can be converted into a single step, but as I said, this is a beginners guide and I’m not an expert (yet) on the g++ compiler).

To compile, in MinGW, cd to the directory that has your cpp file and type:

g++ -c -Dtest_dll test.cpp

This will create a file called test.o

Now type

g++ -shared -o test.dll test.o -Wl,–add-stdcall-alias

This will create test.dll

Now, open a normal VB  project and in the form’s code space, paste the following

Option Explicit

Private Declare Function FooBar Lib “C:\msys\1.0\home\John\Dll_with_VB_test\test.dll” () As Integer
Private Declare Function TimesTwo Lib “C:\msys\1.0\home\John\Dll_with_VB_test\test.dll” (ByVal val As Integer) As Integer

Private Sub Form_Load()

Debug.Print FooBar

Debug.Print TimesTwo(5)

End Sub

Take note of two things here:

1) The path to the dll must obviously be changed to be correct on your system

2) In the second declare, we pass an integer called val in to the dll. If you leave out the ByVal, the value is corrupted, so the ByVal bit is important.

That’s it, please post back here if you have any useful insights. Lets learn (and port our software) together!

]]>
/blog/software/software_development/c/compiling-a-c-dll-with-mingw-to-use-with-visual-basic/feed/ 0