Where Customization Begins 

  • How to Create a Simple Win32 DLL

  • Many Other Tutorials That Don't Belong In Any Of The Above Sections Here.
Many Other Tutorials That Don't Belong In Any Of The Above Sections Here.
 #11977  by Buster67
 01 Jan 2011, 21:11
How to Create a Simple Win32 DLL

This article uses Visual C++ 6.0

Introduction

This tutorial will teach you how to use Visual C++ 6.0 to create a small DLL that can be called from C++, Visual Basic, Delphi or any other program capable at calling DLL functions. Specifically, we will look at how to call the DLL function from Indigo Rose development tools.

This DLL will have one purpose; to generate a unique filename using the Windows API function GetTempFileName. This function is useful if you need to store temporary data in a unique file for the user of your application and want to ensure that the file does not already exist. However, the point of this tutorial is not to generate unique filenames, but rather to show you how easy it is to create simple Windows DLLs using Visual C++.

The key here is "simple". Windows DLLs are capable of much, much more than outlined here. This tutorial will only cover simple, Win32 DLL creation for those quick-and-dirty projects.

Create the Project

The first step is to start Visual C++ and create our DLL project.

1) Start Visual C++ and choose File | New from the menu.

This will open the New dialog. This is the starting point for creating all kinds of projects. Make sure that you are on the "Projects" tab.

2) Select "Win32 Dynamic-Link Library" as the project type and name the project "MakeTempFilenameDLL".

We want to create a Win32 DLL, not a MFC DLL. MFC-based DLLs can also be very useful and offer a lot more functionality, however they require the overhead of the MFC library DLLs (over 1 MB) just to get started. That is too much overhead for our simple DLL needs.

Image

So, the New dialog should now look something like the one above. Click OK to continue to the next step.

3) Choose "A simple DLL project." and click Finish.

The next screen you will see looks like this:

Image

For this project, we will just start with a simple DLL. After you click the Finish button, you will see the New Project Information dialog which summarizes your settings. Click OK to complete the project creation. Your new project will now be ready to work on from the Visual C++ design environment. At this point, your project will compile and your DLL can be created, however it will not do anything because we have not created and exported any functions yet. That is the next step.

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs.

1) Open the "MakeTempFilenameDLL.cpp" file for editing.

Select the FileView tab on the Workspace bar. Then open the Source Files folder. Now double-click on the "MakeTempFilenameDLL.cpp" file. You will now be able to edit the file.

2) Add the "RetrieveTempFilename" function.

Next we want to add the actual code for the function. Our function will be called "RetrieveTempFilename". It will accept two string arguments. The first is "szDirectory" which is the directory that we want to generate the unique filename in. The second argument, "szPrefix" is a string that will be the prefix of the temporary filename. The "GetTempFileName" API function will use the first three characters of this string when generating the unique filename. The function will return the unique filename if successful, or the string "ERROR" if it fails.

Because we want to return a string, we need to make the returned string a global variable. This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished. We need to create a string that will last for the life of the DLL.

Your global string declaration and function should look like this:



You can copy and paste the code above right into your file below the "DllMain" function. If you build your project now it will compile successfully but the function will still not be callable from outside programs. That is because the Visual C++ compiler "decorates" the function names so that they would not be recognizable to the calling program. Although there are several ways around this problem, we will use an "Export.def" file.

Add an "Export.def" File

Adding an Export.def file will allow us to tell the compiler the proper names of our DLL functions that should be exposed.

1) Add a new text file to your project called "Export.def"

Choose File | New from the menu within Visual C++ and select the Files tab. Select "Text File" as the file type and type in "Export.def" as the filename. Click OK to create the file.

Image

2) Add the "LIBRARY" and "EXPORTS" sections to the "Export.def" file.

All that we need to do now is to add a few sections to the "Export.def:" file and we will be in business. The first section is the "LIBRARY" declaration with our DLL name. The second is the "EXPORTS" section which declares our exported functions.

LIBRARY MakeTempFilenameDLL
EXPORTS
RetrieveTempFilename

Copy and paste the above lines into the "Export.def" file. Now you can build your project. First of all, make sure you are building the "Release" build by selecting Build | Select Active Configuration, choosing "MakeTempFilenameDLL - Win32 Release" and clicking OK. Then just hit F7 to build the DLL. It should compile and link with no troubles.

Using the DLL from Indigo Rose Tools

At this point you have a fully-functional DLL that can be called from Visual Basic, another C++ application or any other program capable of loading can calling DLL functions. Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language.

The first step will be to put the DLL somewhere that the runtime engine can find it. In the case of Setup Factory or Visual Patch include it as a primer file and call it like this:


In the case of the other products, simply adjust the strDLLPath variable accordingly.

original link Here