AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

Creating and using your own VC++ dlls
Author: Pieter Philippaerts

Using the Windows API is a wonderful way to speed up your applications when they need to perform processor intensive tasks like graphics manipulation. However, the Windows API doesn’t always provide all the functionality you need for your application. Perhaps some 3rd party control does what you want to be done, but using these controls can be very expensive.
Therefore it’s better to sometimes write your own controls/dlls in another more powerful and faster language like Visual C++.
In this article, I’m going to show you how you can create a VC++ DLL that performs some simple yet processor intensive graphical functions.
This article assumes you have some knowledge of the C++ syntax. It is written for Visual C++ 5.0, but should also be compatible with Visual C++ 6.0.

1. Hello World

VC++ has several tools to greatly simplify the work that needs to be done. One of these tools is the MFC AppWizard, which allows you to create code for an empty DLL within a minute. All you have to do is to write the functions you want the DLL to export. The application wizard takes care of the rest.
Start a new project (File -> New) and select ‘MFC AppWizard (dll)’ from the Projects tab. Type the name of your DLL in the ‘Project Name’ textbox, and select the location where you want the project to be saved.

New DLL
(The ‘New’ window)

After clicking the OK button, Visual Studio will show you the following window:


(The Application Wizard Window)

Click the ‘Finish’ button to let the wizard generate the code.

On the FileView tab, you can see all the files the wizard generated. Two important files are the MyDll.cpp file, which will contain the code of the functions we’re going to export, and the MyDll.def file, which will contain the names of the functions we wish to export.
Double click on the MyDll.cpp file and add the following code to the end of the file:

void _stdcall MyFunction () {
      AfxMessageBox("Hello World");
      return;
}

This code declares a sub that shows a message box with the text “Hello World”. Note that this function uses the _stdcall calling convention, since VB can only handle _stdcall or _fastcall. Both calling conventions mean that the called function cleans up the stack and parameters are passed from right to left.
If you ever run into a ‘Bad DLL calling convention’ when you call a specific function from VB, it means that this function doesn’t support the stdcall or fastcall calling convention.

If you compile the DLL right now, you won’t have access to MyFunctions, since we didn’t export it yet. Trying to call the function would result in a ‘Can’t find DLL entry point’ error message.
To export the function, you’ll have to add it to the exports list, located in the MyDll.def file. Open this file and write ‘MyFunction’ (without the quotes) right under ‘EXPORTS’.

Before we’re going to compile our DLL, we’re going to set the ‘Active Configuration’ to ‘Release’ instead of ‘Debug’. This allows us to compile to smaller and optimised DLLs.
You can do this by going to Build -> Set Active Configuration… and then select ‘MyDll – Win32 Release’.

We’re now ready to compile our DLL and call it from VB. You can build it by either pressing F7 or by going to Build->Build MyDll.dll.
The following should appear in the Build Window (usually located at the bottom of the screen):

---------------Configuration: MyDll - Win32 Release---------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
MyDll.cpp
Linking...
Creating library Release/MyDll.lib and object Release/MyDll.exp
MyDll.dll - 0 error(s), 0 warning(s)

If the compiler gives you warnings or error messages, make sure you’ve done everything as described above.

We’re now ready to call the function from within a VB application.
Start Visual Basic and create a new project. Copy the following code into the new project:

Private Declare Sub MyFunction Lib "%PATH%\Release\MyDll.dll" ()
Private Sub Form_Load()
    MyFunction
End Sub

This code calls the exported function from our DLL, before the main window is shown on the screen. Do note that you have to change the library path in the declaration of the function to the path where the DLL is located. If the DLL is located in the Windows system directory, this library path can be set to “MyDll.dll”.

If you run the application, you should see something similar to this:


(The 'Hello World' application)

Click here to download the source code of the above programs.

Part 1 - Part 2 - Part 3 - Part 4 isn't available yet

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/