Web development by PHP-Web-Host.com   Website Hosting by PHP-Web-Host.com
Web Hosting by PHP-Web-Host.com

SoftSmart Blogs!

SoftSmart Technical, Software and Web Related Blogs

Tel (Lon, UK): 020 8133 3460

 

 

Blogs

Posts Tagged ‘c++ dll with VB’

Compiling a C++ dll with MinGW to use with Visual Basic

Tuesday, February 16th, 2010

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!

 


 

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

 

 

We accept PayPal

 

Web Hosting by PHP-Web-Host.com Website Hosting by PHP-Web-Host.com

[ Home ] | [ Software ] | [ Web Development ] | [ Resources ] | [ Contact ] | [ Privacy Policy ] | [ Acceptable Use Policy ] | [ Terms of Service ] | [ Sitemap ] | [ Clients ] | [ Affiliates ]

[ PHP Web Hosting ] | [ Linux Web Hosting ] | [ 500mb PHP Web Hosting ] | [ 1Gb PHP Web Hosting ] | [ 3Gb PHP Web Hosting ] | [ 6Gb PHP Web Hosting ] | [ 10Gb PHP Web Hosting ]

[ HTML Tutor ] | [ CSS Tutorial ] |