Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / Tutorial – Make a DLL with VC++ Express

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 7th Feb 2011 16:03 Edited at: 7th Feb 2011 16:07
Required software: Visual C++ Express Edition (free).
Required knowledge: None

Open Visual C++, and on the start page click “Create project”. Select ‘Win32’ under projects, and then ‘Win32 project’ under Templates:


Next, you’ll have to type a name for your project. I’ll call mine “TutorialPlugin”, but you can call yours anything you like. Click OK, and then the Win32 application wizard will open. Just click Next, and then you should see this:



Select dll as the application type and then click “Finish” – Visual Studio will then create the files you need to get started. They’ll be listed in the Solution Explorer, on the left of the screen:



Most of the files here can be ignored, as they’re standard to all dlls – just make sure you don’t modify or delete them! The only important file is the one carrying the name of your project (here it’s ‘TutorialPlugin.cpp’), as this is where you’ll type all your commands.

To get started, open this file by double-clicking on it in the Solution explorer. At first you’ll see this standard code that Visual Studio has entered for you:



Let’s start with something simple, like having the dll return a value. Type the following code after the #include command:



The #define command creates a macro so that when you compile your code, the text ‘MYCOMMAND’ will be automatically replaced with the line ‘__declspec (dllexport)’. This line of code is vital as it tells the DLL that the function will be exported, which means that other programs (like DBPro) can call this function from outside the DLL. Without this line, DBPro won’t know that your function even exists!

Now let’s look at the rest of the code you just typed. The function starts with:


int tells the compiler that the function returns an integer value, GetValue is the name of the function, and ( void ) tells the compiler that no parameters will be passed in to the function. In DBPro you’d write this line as:


The {} curly brackets are used to define blocks of code in C++. The { marks the start of your list of commands for the function, while the } marks the end of the function. The next line, ‘int A = 4;’ tells the compiler to create an integer variable, called ‘A’, and assign it a value of 4. The semi-colon ( ; ) marks the end of the statement – without it, the compiler would run multiple lines together and probably crash.

After creating a variable B in the same way, the statement ‘A = A + B;’ simply adds the two values together (just like DBPro). Lastly, the line ‘return A;’ simply returns the answer of the sum. You could write the whole function in DBPro as:


Press F7 to compile and you have your first DLL… Unfortunately, it’s not quite ready for DBPro to use!



You have to add a string table to the DLL, so that DBPro knows which functions are there, what parameters they take and what values they return. This is stored in a resource file, but you can’t edit these easily in Visual Studio 2008 Express, so you have to add it manually.

Right click the “Header Files” folder in the Solution Explorer and selected Add->New Item…


A dialogue box will appear, where you can choose what to add. Select ‘Header file(.h)’ and name it ‘resource.h’. Click Add, and then open the file to edit it.

You need to type the following:

We only need the first line in this tutorial as the plugin only has one function, but you can have up to 999 functions in a single DLL. Note the pattern in the lines – IDS_STRINGn n, where n is a number. The number increases so as to define a unique entry in the string table (which we can now create).

Right click on the “Resource Files” folder in the Solution Explorer, and select Add->NewItem again. Select the header file option again, but type the filename as ‘TutorialPlugin.rc’ – note the .rc file extension, as this will make it a resource file rather than a header file. When you click ‘Add’, the file will be created but a window will pop up saying that you cannot edit resource files.

Close this window, then right click on the .rc file name in the Solution Explorer and select ‘Open With…’. A choice of options will appear, select Source Code (Text) Editor and click OK.



The resource file can now be edited like a C++ source file. Type the following code:



Everything within the {} curly brackets following the line STRINGTABLE makes up the string table. The single entry in the table is quite complex:

IDS_STRING1 "GET VALUE[%L%?GetValue@@YAHXZ%ReturnAValue"

Let’s break it down into its various parts:


Just two things to note:
1) Although L is used to signify the return type, you can use other data types as well. The full list of codes are:
L = Integer
F = Float
S = String
O = Double Float (capital o)
R = Double Integer (capital r)
D = Boolean, BYTE, WORD and DWORD
0 = Zero Character (no param)

2) The function’s decorated name looks a little garbled, but it is very important. Hit F7 to compile and then open your DLL in notepad (or any other simple text editor). Do a search for your function name (in this case, GetValue) as you typed it in the C++ Editor. You can then copy the decorated name into the string table – don’t forget to include the question mark at the start!

Once the string table is complete, you’re ready to build and test your DLL. In Visual Studio, click Build->Configuration Manager.


Change the project configuration from Debug to Release, and click Close. When you compile again, your DLL will be placed in a folder called “Release”. It will also be much smaller, as Visual Studio removes a lot of stuff designed to aid the debugging of your DLL.

Copy this file to the folder “Dark Basic Professional\Compiler\plugins-user”, and DBPro will then be able to use it. Type this code in DBPro:


When you run this code, you should see the number 9 printed on screen. You’ve also just written your first plugin!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 7th Feb 2011 16:06
This is my first tutorial, so comment and criticism would be appreciated! I know that all the information I've included here is available on the forums, but I had to cobble it together from several different sources - I felt that beginner's in C++ like me could do with having it in one place!

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Coldfire
15
Years of Service
User Offline
Joined: 22nd Oct 2008
Location: Indiana
Posted: 8th Mar 2011 23:06
Not to shabby. I already knew how to make dlls, but I was curious anyways, so I took a look. I honestly wish I could have found a thread like this when I was learning. I had to search multiple threads to learn everything in this one thread. Cheers on putting them together in a neat little resource!

Two things you may want to add a bit about returning floats and strings. From the help files(put it in a code block to save clutter):
Admiral MH
13
Years of Service
User Offline
Joined: 10th Feb 2011
Location: TX, USA
Posted: 9th Mar 2011 21:23
Great tutorial! Very thorough with all the pictures.
EDGECOM
17
Years of Service
User Offline
Joined: 7th Sep 2006
Location: US
Posted: 15th Mar 2011 19:30 Edited at: 15th Mar 2011 19:32
nice tutorial

my tutorial might make for some good reading also and a few free apps that may help. feel free to use what ever from my tutoral if you would like to add to yours or what ever.

I know mine is out of date and I don't feel like rewriting it

http://forum.thegamecreators.com/?m=forum_view&t=153453&b=18
Visigoth
19
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 5th Jul 2011 00:34
good tutorial. This has helped me alot, and I think it too should be stickied. The only comments I have are 1. Maybe add another function to show how to pass in a parameter from DBPro, and 2. Maybe a better explanation and/or routine on how and when to grab the decorated name. Other than that, very nice job.
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 5th Jul 2011 18:42 Edited at: 5th Jul 2011 18:44
Glad you liked it!

Quote: "Maybe add another function to show how to pass in a parameter from DBPro"

I want to extend this tutorial a little to cover this as well as looking at how DBP commands can be called from a custom C++ Dll, but I haven't had the time. If you want some advice, I can recommend the DBP help files on "Creating a 3rd party plugin", in the "Technical Documents" section.

Basically, if you have a function:


then in the String Table you would fill it in as:

(Just made up some letters for the decorated name, don't quote me on it!).
"[" signifies a return value
"LLL" then means return one integer, take two integers as inputs.

Then in DBP you simply call:


Quote: "Maybe a better explanation and/or routine on how and when to grab the decorated name"


I actually say this at one point:
Quote: "2) The function’s decorated name looks a little garbled, but it is very important. Hit F7 to compile and then open your DLL in notepad (or any other simple text editor). Do a search for your function name (in this case, GetValue) as you typed it in the C++ Editor. You can then copy the decorated name into the string table – don’t forget to include the question mark at the start!
"


I believe that if you buy the full version of VC++, it comes with a tool to automatically generate a string table for you - I don't do enough programming to justify the expense, so I just create the whole thing manually. If you leave your string table till you have a lot of functions to add, it can be quite straightforward (if a little boring) to construct as all the decorated names appear in a single line of text in the DLL, sorted into alphabetical order.

We spend our lives chasing dreams. Dark Basic lets us catch some of them.
Visigoth
19
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 7th Jul 2011 05:59
hey Robert the Robot,
no, I was just saying to make it a really good all in one tutorial, to show how to pass in the params from DBPro. I already know how to do this. As for the timing of getting the decorated name, in your tutorial, you give up the decorated name before you search for it. Go thru it step by step and you'll see what I mean. I also understand this as well, but, just suggesting to make it a little simpler for the step by step guys. Thanks for the time and effort for posting this. Good job.

Login to post a reply

Server time is: 2024-03-28 10:56:09
Your offset time is: 2024-03-28 10:56:09