Jump to content


Photo

Dll Tutorial


  • Please log in to reply
58 replies to this topic

#1 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 23 December 2009 - 06:10 AM

Introduction
I intend, with this topic, to introduce users to creating DLLs for Game Maker. I found it quite hard to find all the information I needed to make DLLs on the internet, so I hope to provide a centralised GMC beginner tutorial.
I will be using C++ in this tutorial with Microsoft Visual C++ 2008. I use the Express (free) version; however, this tutorial should apply to the paid version also. Other IDEs may work, but I cannot provide support.
The goal of this tutorial is not to teach you C++. I recommend you have a good knowledge of C++ before attempting to create a DLL.

Basics
There are some important things to grasp when creating DLLs for Game Maker.
The first is that Game Maker can only accept and pass 2 types of variables: reals and strings (in C++ terms, doubles and char*s). This means you cannot directly use structs or classes. You have to access these using an array of pointers. If people ask for a tutorial on doing so, I will write one.
Also, you must return a value with every function.
GM only supports up to 4 string parameters; if you have more than 4 arguments to a function, they must all be reals.

Important!
When using Microsoft Visual C++, you must change the compiler's settings. By default, the compiler will generate an executable.
This is not what we want. To compile a DLL, you will have to go into Project -> <ProjectName> Properties (or just press Alt+F7).
Posted Image
Now click on Configuration Properties, and look at Configuration Type, under Project Defaults. It should say Application (.exe). Open the drop down box and select Dynamic Library (.dll).
Posted Image
Now when you compile, it will create a DLL.
You must also specify to include the runtime library required to run VC++ programs on other computers (thank you PsichiX), also under Project Settings:
Posted Image


Your First DLL
Download link:
Posted Image
Contains a zipped folder with the GMK, the DLL and the C++ source.

The first DLL you will be making will be a simple one with 2 functions; one using reals and one using strings. They will be:
- Return the product of 2 numbers
- A Windows Message Box
Add a new empty project, and a new empty source file. Call the project "myfirst".
UPDATE: It seems some users have misread this section. It must be an EMPTY project.
The first thing, as always, is to determine which libraries we will require. I will use one library; windows.h for the MessageBox() function.
//So far...
#include <windows.h>
Next, because we are using a DLL, we have to tell the program that the functions are to be accessed from another function. We use a collection of keywords to accomplish this.
extern "C" __declspec(dllexport)
Let me go through that. C++ compilers garble function names. Using extern "C" stops this. We have to do this because GM references DLL functions by name, and without switching off garbling we won't know what the function names are.
__declspec(dllexport) tells the compiler that the function is to be exported.
This line must go before each function definition, like so:
extern "C" __declspec(dllexport) double SomeFunction(void)
To make things easier for us, we will use #define to tell the preprocessor to replace all occurences of "DLLEXPORT" with our function definition line.
//So far...
#include <windows.h>
#define DLLEXPORT extern "C" __declspec(dllexport)
Now we can simply use:
DLLEXPORT double SomeFunction(void)
The final step in creating the DLL is to write the functions. The first function is very easy and you should be able to write it yourself:
DLLEXPORT double Multiply(double a, double b) {
	return a * b;
}
The next function is nearly as basic (if you have not used it before, have a look at http://msdn.microsof.....28VS.85).aspx).
It looks like this:
DLLEXPORT double MsgBox(char* caption, char* message) {
	MessageBox(0, message, caption, MB_OK);
	return 0;
}
The complete C++ code is:
#include <windows.h>
#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT double Multiply(double a, double b) {
	return a * b;
}
DLLEXPORT double MsgBox(char* caption, char* message) {
	MessageBox(0, message, caption, MB_OK);
	return 0;
}
Push F7 to compile your DLL. Congratulations; you've created your first DLL!
Update: If you have Unicode set, it won't work.
LPCSTR = const char *
typedef const char* LPCSTR;
LPCTSTR = const char * OR const wchar_t*
#ifdef _UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif
These are excerpts from WinDef.h, a Microsoft header included automatically by windows.h.

Under General Options in Project Settings, ensure that Char is "no set".

Using Your DLL
Using a DLL is also quite complicated. Make a new folder somewhere for your Game Maker file, and copy the DLL to the folder. The DLL, by default, will be in the following path:
For XP or earlier
C:\Documents and Settings\USERNAME_HERE\My Documents\Visual Studio 2008\Projects\myfirst\Debug\myfirst.dll
For Vista or Windows 7
C:\Users\USERNAME_HERE\Documents\Visual Studio 2008\Projects\myfirst\Debug\myfirst.dll
Open Game Maker, and create a new game. Save it in your folder.
Add a script to your game called dll_init:
globalvar iMultiply, iMsgBox;
iMultiply = external_define("myfirst.dll", "Multiply", dll_cdecl, ty_real, 2, ty_real, ty_real);
iMsgBox = external_define("myfirst.dll", "MsgBox", dll_cdecl, ty_real, 2, ty_string, ty_string);
First things first. globalvar defines the variable as global. We need 2 global variables to hold the IDs of our C++ functions.
The function external_define is quite daunting to new users. It has a lot of arguments. I'll go through them step by step.
dll
This argument is the path to the DLL. In this case, it is "myfirst.dll", because we have simply copied the DLL to the game's location.
name
This is the function's name in the C++ source. We have 2 functions; Multiply and MsgBox.
calltype
The call type for the DLL. In this tutorial, I have used dll_cdecl.
restype
The return type for the DLL. In our case, both return a real.
argnumb
The number of arguments for the DLL. Both our functions have 2 arguments.
arg<1 to 16>type
The type of the given argument. We have 2 functions. One takes 2 real arguments, the other takes 2 string arguments.

Add another script called dll_free. This script simply calls the external_free function. It takes one parameter; the DLL's file name.
external_free("myfirst.dll");
Now we have to add scripts to call our functions. First is Multiply:
return external_call(iMultiply, argument0, argument1);
Finally MsgBox:
external_call(iMsgBox, argument0, argument1);

Now you're done! Just remember to call dll_init() in the game's start event, and dll_free() in the game's end event.



Using HWND in DLLs
This is just a brief write-up on using the Windows HWND type in DLLs. Game Maker, as you should know, can only use 2 types: reals and strings. This means you can't directly use the value obtained by window_handle() (a GML function) in a DLL. Instead, you have to typecast it. Unfortunately, a direct cast to HWND fails. This is easily overcome. An example:
DLLEXPORT double WindowMaximise(double hwnd) {
	HWND handle;
	handle = (HWND)(INT)hwnd;
	ShowWindow(handle, SW_MAXIMIZE);
	return 0;
}
As you can see, to use the HWND type, all you have to do is cast it first to an int, then a HWND.



Dev-C++ template
For those of you who use Dev-C++ despite it being 5 years since an update and unsupported, flyslasher has posted a template below. I have fixed it up and re-posted it here.

Go to C:\Dev-Cpp\Templates and create a file called 6-GMDLL.template and put this,:
[Template]
ver=1
Name=Game Maker DLL
IconIndex=3
Description=A Dynamically Linked Library (DLL) for Game Maker
Catagory=Basic

[Unit0]
CName=gmdllmain.c
CppName=gmdllmain.cpp
C=GMDLL_c.c
Cpp=GMDll_cpp.cpp
[Unit1]
CName=gmdll.h
CppName=gmdll.h
C=GMDLL_h.h
Cpp=GMDll_hpp.h

[Project]
UnitCount=2
Type=3
Name=My GM_DLL
Compiler=-DBUILDING_DLL=1
CppCompiler=-DBUILDING_DLL=1
Linker=--no-export-all-symbols --add-stdcall-alias

Make another file called GMDLL_cpp.cpp and put this,
#include "gmdll.h"
#include <windows.h>

/* Place your functions down here like so... */

DLLEXPORT double mult( double x, double y )
{
	  return(double)x*y;
}

and then again, make another file called GMDLL_hp.h and put this,
#ifndef _DLL_H_
#define _DLL_H
#define DLLEXPORT extern "C" __declspec (dllexport)
#endif /* _DLL_H_ */

Finally make a file called GMDLL_c.c and put,
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>

/* Place your functions down here like so... */

DLLEXPORT double mult( double x, double y )
{
	  return(double)x*y;
}

Then, just open up Dev-C++, and click File -> New -> Project and now a new Project option should come out called : Game Maker DLL.



Thanks!
Thank you for reading (and sorry for the huge post). If you have any suggestions for improvement, don't hesitate to ask.
Please reply if this tutorial helped you out.

Edited by PlasticineGuy, 12 February 2010 - 11:24 PM.

  • 4

#2 voltain

voltain

    V.C.G

  • GMC Member
  • 174 posts

Posted 23 December 2009 - 01:56 PM

Great tutorial! I've been wondering how to do this for a while now!
  • 0

#3 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 23 December 2009 - 02:00 PM

Thank you :(. I went through quite a bit of effort to type this up.

I just realised I included the wrong library in my completed code... I'll fix that.
  • 0

#4 PsichiX

PsichiX

    GMC Member

  • GMC Member
  • 399 posts
  • Version:GM:Studio

Posted 23 December 2009 - 04:14 PM

don't forget about select Multi-threaded (/MT) for Release, and Multi-threaded Debug (/MTd) for Debug in Code Generation -> Runtime Library, because then created library don't require microsoft redistributable package to run on computers which don't have this one.
http://img22.imagesh...egeneration.png

Edited by PsichiX, 23 December 2009 - 04:15 PM.

  • 0

freckles_sign.png


#5 ragarnak

ragarnak

    GMC Member

  • GMC Elder
  • 19468 posts
  • Version:GM8

Posted 23 December 2009 - 11:19 PM

I will be using C++ in this tutorial with Microsoft Visual C++ 2008.

Where did you get it ? As far as I know that is a non-free and quite costly compiler ...
  • 0

#6 Tuz

Tuz

    GMC Member

  • New Member
  • 106 posts

Posted 23 December 2009 - 11:29 PM

I will be using C++ in this tutorial with Microsoft Visual C++ 2008.

Where did you get it ? As far as I know that is a non-free and quite costly compiler ...

Express edition?
  • 0

#7 ragarnak

ragarnak

    GMC Member

  • GMC Elder
  • 19468 posts
  • Version:GM8

Posted 23 December 2009 - 11:47 PM

Express edition?

Too bad that the download-button (bottom-right) on that page leads to a search-page in which the first result brings you back to the page you posted. Thats what I call "going in circles" :( :(

Allso lets hope that the "express" edition can create DLLs (which I'm not at all certain of).
  • 0

#8 Tuz

Tuz

    GMC Member

  • New Member
  • 106 posts

Posted 24 December 2009 - 12:09 AM

Express edition?

Too bad that the download-button (bottom-right) on that page leads to a search-page in which the first result brings you back to the page you posted. Thats what I call "going in circles" :) :P

Allso lets hope that the "express" edition can create DLLs (which I'm not at all certain of).

You can create DLLs with it, i've tested. ;)
  • 0

#9 tuntis

tuntis

    GMC Member

  • New Member
  • 1839 posts

Posted 24 December 2009 - 12:19 AM

I will be using C++ in this tutorial with Microsoft Visual C++ 2008.

Where did you get it ? As far as I know that is a non-free and quite costly compiler ...

What does this have to do with the topic at all and why do you care?

As mentioned before, it could be an express edition, it could have been gotten from Dreamspark or they could have bought it.

Too bad that the download-button (bottom-right) on that page leads to a search-page in which the first result brings you back to the page you posted.

Don't attempt to open the download link in a new tab and it'll work fine.

Edited by Tuntis, 24 December 2009 - 12:20 AM.

  • 0

#10 halo shg

halo shg

    Programmer

  • GMC Member
  • 600 posts
  • Version:GM:Studio

Posted 24 December 2009 - 01:05 AM

I will be using C++ in this tutorial with Microsoft Visual C++ 2008.

Where did you get it ? As far as I know that is a non-free and quite costly compiler ...

Express is free (for non-commercial work).

Express edition?

Too bad that the download-button (bottom-right) on that page leads to a search-page in which the first result brings you back to the page you posted. Thats what I call "going in circles" :) :P

Allso lets hope that the "express" edition can create DLLs (which I'm not at all certain of).

Download link is fine. Maybe you just fail at clicking links.

Try this:
http://www.brotherso...load-65282.html
  • 0

Dev: MacBook Pro Retina Display 15" w/ Win7 (Parallels) running GM Studio: Master and GM8 (GMS: ANDROID JAVA EXTENSIONS ARE HERE!)
Devices: Samsung Galaxy Note II / Samsung Galaxy S III / iPhone 4S / iPad 2 / iPod Touch 5th gen



Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.


-Gore Vidal


#11 ragarnak

ragarnak

    GMC Member

  • GMC Elder
  • 19468 posts
  • Version:GM8

Posted 24 December 2009 - 01:10 AM

What does this have to do with the topic at all and why do you care?

Without it I can't follow the OP's tutorial and I'm a moderator. One of my duties is to keep this forum clean. That means : no links to cracked software, not even in a tutorial. Capiche ?

As for that "gotten from Dreamspark" ? Guess why its a https url : it for Microsoft students only, not for the whole world. Maybe that is allso why you first need to sign-in before you can download the thing.

or they could have bought it.

Quite possible. But there are not that many hobbyists that wish to fork over a lot of money for a C++ compiler when there are free alternatives available, like GCC.
  • 0

#12 tuntis

tuntis

    GMC Member

  • New Member
  • 1839 posts

Posted 24 December 2009 - 01:36 AM

Without it I can't follow the OP's tutorial and I'm a moderator. One of my duties is to keep this forum clean. That means : no links to cracked software, not even in a tutorial. Capiche ?

Let's get this straight. You see a well-detailed tutorial for the Visual C++ IDE with instructions on how to make a simple DLL. Otherwise from setting some IDE-specific settings, it's mostly a plain C++ DLL programming tutorial. The only link in it is a ZIP archive that contains a C++ source code file, a Game Maker project and a compiled DLL.

Why do you need to know how the OP has gotten the software if it's clear there are no "links to cracked software" in the post?

You are being ridiculous.

Guess why its a https url : it for Microsoft students only, not for the whole world.

Exactly what does https have to do with students?

Dreamspark is available for students around the world: not "Microsoft students" (whatever a "Microsoft student" is in the first place). And yet again, I don't understand the point you are making - it's also very likely that the original poster could be a student eligible for Dreamspark.

And to repeat a point made by others once more - the most likely option is that the topic author is using a completely free "express edition" from Microsoft.

I still don't understand why you need to know where people get their software. It's none of your business.

Edited by Tuntis, 24 December 2009 - 01:38 AM.

  • 0

#13 ragarnak

ragarnak

    GMC Member

  • GMC Elder
  • 19468 posts
  • Version:GM8

Posted 24 December 2009 - 01:38 AM

Express is free (for non-commercial work).

Thats possible, but that is not what the OP mentioned.

Download link is fine. Maybe you just fail at clicking links.

:) So if something works for you it must work for the whole world, and if they experience something different its ofcourse because of the clumsiness of that person and you should mock that person for it ? No other reason for a different experience is possible ? Really ?

And I would be carefull with "conclusions" like that if I where you. You're quite offending, and for what reason ?

http://www.brotherso...load-65282.html

Thats the express version, not the one the full version the OP mentioned and I responded to. Should I now write something nasty about you too ? Or do you think I should respond in a friendly manner, as you, I assume, only try to help ?
  • 0

#14 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 24 December 2009 - 01:43 AM

Thats the express version, not the one the full version the OP mentioned and I responded to. Should I now write something nasty about you too ? Or do you think I should respond in a friendly manner, as you, I assume, only try to help ?

Or, you could wait for the OP to return and clarify.

I'm sorry; I didn't mention Express edition, mainly because it works for either paid or free version. I'll update to clarify.
Anyway, as far as I can tell, the only advantage the paid edition provides is a built-in resource editor for Windows programs (not DLLs).
EDIT:

I use the Express (free) version; however, this tutorial should apply to the paid version also.

The download, if you're interested, can be found at the site listed (http://www.microsoft.com/Express/VC/):
Posted Image
Warning: it takes a while to download and install.

Ragarnak, you're probably thinking of Visual Studio, which has lots of other things involved in it, none of which I have ever used (because I don't have the package).

EDIT 2:

don't forget about select Multi-threaded (/MT) for Release, and Multi-threaded Debug (/MTd) for Debug in Code Generation -> Runtime Library, because then created library don't require microsoft redistributable package to run on computers which don't have this one.
http://img22.imagesh...egeneration.png

Can you please clarify? I've never heard of anything of the sort.

Edited by PlasticineGuy, 24 December 2009 - 02:21 AM.

  • 0

#15 antidote

antidote

    GMC Member

  • New Member
  • 117 posts

Posted 24 December 2009 - 03:10 AM

It still requires the MSVC redist plasticine, the stupidity of this site is beginning to wear on my nerves. Especially the moderators, ragarnak, don't be such a retard, it's not your JOB to QUESTION WHERE PEOPLE get their software, so don't do it.

Edited by antidote, 24 December 2009 - 03:10 AM.

  • 0

#16 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 24 December 2009 - 03:17 AM

It still requires the MSVC redist plasticine,

The what?
  • 0

#17 antidote

antidote

    GMC Member

  • New Member
  • 117 posts

Posted 24 December 2009 - 03:20 AM

the "Redistributable" package, which is the DLL subset that all programs made with Visual Studio require, you can however choose (with some minor editing of the solution) the version it requires for compatibility with older redistributables.

EDIT:
http://www.microsoft...;displaylang=en

Edited by antidote, 24 December 2009 - 03:22 AM.

  • 0

#18 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 24 December 2009 - 03:22 AM

That's odd. Is there any way to circumvent this? Does it have to be distributed with everything made with it? How big is it? If so, and it's huge, that makes the program kind of useless, and I'll just have to find some other compiler.
  • 0

#19 antidote

antidote

    GMC Member

  • New Member
  • 117 posts

Posted 24 December 2009 - 03:25 AM

if you call between 30-100 megs huge i guess. luckily the 2008 service pack is only 4.5 MB

Just kidding about the 30-100 MB part though :) it's only a few megs.

Edited by antidote, 24 December 2009 - 03:27 AM.

  • 0

#20 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 24 December 2009 - 03:27 AM

What does the service pack do? What is the service pack for, VC++ or Windows?

Edited by PlasticineGuy, 24 December 2009 - 03:28 AM.

  • 0

#21 antidote

antidote

    GMC Member

  • New Member
  • 117 posts

Posted 24 December 2009 - 03:28 AM

just some bug and security fixes nothing major.
  • 0

#22 PsichiX

PsichiX

    GMC Member

  • GMC Member
  • 399 posts
  • Version:GM:Studio

Posted 24 December 2009 - 03:33 AM

Redistributable package have built in runtime library, if you create app or dll with visual, then app/dll need this runtime library. if you do what i described then runtime library will be compiled with your program and other people don't need to download those libraries with Redistributable package

Edited by PsichiX, 24 December 2009 - 03:36 AM.

  • 0

freckles_sign.png


#23 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 24 December 2009 - 11:11 AM

Okay, I added the information in the original post. However, it seems to be already specified. I will use it anyway to be safe.
  • 0

#24 ragarnak

ragarnak

    GMC Member

  • GMC Elder
  • 19468 posts
  • Version:GM8

Posted 24 December 2009 - 11:53 AM

Or, you could wait for the OP to return and clarify.

:) I guess I should have done that.

I'm sorry; I didn't mention Express edition, mainly because it works for either paid or free version. I'll update to clarify.

Thanks for the clarification.

Ragarnak, you're probably thinking of Visual Studio, which has lots of other things involved in it, none of which I have ever used (because I don't have the package).

Yes, I somehow assumed they where the same.

The download, if you're interested, can be found at the site listed http://www.microsoft.com/Express/VC/

Yeah, that one got me going in circles. I however found the problem : The link uses an "onClick" javascript event and I've got JavaScript disabled. Alas, the "href" argument is allso there but its contents cannot be resolved (bad webpage coding if you ask me), hence the search-page result.

"halo shg" did post another link to the express version though, and that one didn't need JS. (thanks halo shg)
  • 0

#25 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 24 December 2009 - 12:10 PM

They're similar. Visual Studio includes Visual C++ (paid).
  • 0

#26 FmMan3

FmMan3

    GMC Member

  • GMC Member
  • 189 posts
  • Version:GM:Studio

Posted 28 December 2009 - 08:57 PM

Thanks for the tutorial, maybe I'll get around to following it soon, always wondered how to go about making DLL's and this seems like a good tutorial. Thanks so much.

btw, anyone who is anyone in programming should know what Visual C++ Express edition is... =/
  • 0
Signatures are nothing but a fad.

#27 ragarnak

ragarnak

    GMC Member

  • GMC Elder
  • 19468 posts
  • Version:GM8

Posted 28 December 2009 - 09:45 PM

btw, anyone who is anyone in programming should know what Visual C++ Express edition is... =/

Yes, like I allways assume any programmer worth anything should know what "retn" or "int 21h" is. :)

But to be serious, why ? Why should I know that specific compiler-version or even be aware of its existence ? Why do you think that that knowledge is so important ?

Maybe I'm using another (open-source ?) C(++) compiler and had no need for that express edition ... Maybe I do not even program in C(++) at all but prefer to use another language ?

In short : Step off that high horse of yours (before you fall off).
  • 0

#28 millzyman

millzyman

    GMC Member

  • GMC Member
  • 574 posts
  • Version:GM:Studio

Posted 30 December 2009 - 05:53 AM

Well rather than arguing about the express edition as the solution has been found (just stop fueling the fire and let it go out), i have actually run into a error. I use Visual C++ 2008 Express edition. And i am getting the following error:

Error1 C2664: 'MessageBox' : cannot convert parameter 2 from 'char *' to 'LPCTSTR'

Here is my code that i have used from your tutorial.

#include "stdafx.h"
#include <windows.h>
#define DLLEXPORT extern "C" __declspec(dllexport)

DLLEXPORT double MsgBox(char* caption, char* message) {
	MessageBox(0, message, caption, MB_OK);
	return 0;
}

but i also added "stdafx.h" as it says that it is required to compile the project.

Any ideas?
  • 0
Amigo Gamer (Link no longer available)
Amigo Gamer. New site for the development of Dusk and Dawn: Civilisations. Go there for updates about the game! Game is still being worked on.

#29 PsichiX

PsichiX

    GMC Member

  • GMC Member
  • 399 posts
  • Version:GM:Studio

Posted 30 December 2009 - 08:28 AM

MessageBox(0, (LPCTSTR)message, caption, MB_OK);

btw. you could start from empty project (not win32 app or dll option in project creating window) to make project without stdafx

Edited by PsichiX, 30 December 2009 - 08:32 AM.

  • 0

freckles_sign.png


#30 millzyman

millzyman

    GMC Member

  • GMC Member
  • 574 posts
  • Version:GM:Studio

Posted 30 December 2009 - 08:43 AM

Oh really?

Well now i am getting

Error 1 error C2664: 'MessageBox' : cannot convert parameter 3 from 'LPCSTR' to 'LPCTSTR'
c:\Documents and Settings\********\My Documents\Visual Studio 2008\Projects\Mbox\Mbox\Mbox.cpp 11

Sorry i should say.

I also put (LPCSTR) infront of caption as it came up with this.

Error 1 error C2664: 'MessageBox' : cannot convert parameter 3 from 'char *' to 'LPCTSTR' c:\Documents and Settings\********\My Documents\Visual Studio 2008\Projects\Mbox\Mbox\Mbox.cpp 11

Edited by millzyman, 30 December 2009 - 08:45 AM.

  • 0
Amigo Gamer (Link no longer available)
Amigo Gamer. New site for the development of Dusk and Dawn: Civilisations. Go there for updates about the game! Game is still being worked on.

#31 cspotcode

cspotcode

    GMC Member

  • New Member
  • 3 posts

Posted 30 December 2009 - 10:55 AM

You can use MinGW instead of Visual C++. Dev-C++ can install MinGW for you and has a project template for DLLs. I had to change "void" to "double" in 2 places and add a "return 0;", and voila! I could test the template in GM.

As for static vs dynamic linking, I'm not sure which has been used in this case. It might not matter if it's not using the MVC runtime. Would someone else please test the dll on their computer? I packed it up in a Game Maker exe that tests it. It should show a "Hello World" popup when you run it.

testDllDriver.exe

If you're wary of running a strange exe, here's a gmk just to show I'm not trying anything sneaky. I can post the DLL's source code as well.

testDllDriver.gmk

EDIT: I forgot to clarify, static linking is what /MT does. Dynamic linking is what /MD does.

Edited by cspotcode, 30 December 2009 - 10:56 AM.

  • 0

#32 PsichiX

PsichiX

    GMC Member

  • GMC Member
  • 399 posts
  • Version:GM:Studio

Posted 30 December 2009 - 11:21 AM

milzyman, please read corretctly what i write and analize what you writing:

MessageBox(0, (LPCTSTR)message, caption, MB_OK);

Error 1 error C2664: 'MessageBox' : cannot convert parameter 3 from 'LPCSTR' to 'LPCTSTR'

Where you lost "T"? :whistle:
  • 0

freckles_sign.png


#33 freaked

freaked

    freak up!

  • New Member
  • 890 posts

Posted 30 December 2009 - 11:42 AM

Just use MessageBoxA instead of MessageBox.
I think that should do it.

Also,
LPCSTR = Long Pointer to C STRing
LPCTSTR = Long Pointer to ConsT STRing.

A typecast should remove the const-ness.

Edited by freaked, 30 December 2009 - 11:43 AM.

  • 0

#34 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 30 December 2009 - 11:53 AM

MessageBox is a synonym for MessageBoxA.

I told you to make an empty project.

That is quite strange; I do not receive that error, and it shouldn't matter. LPCSTR/LPCTSTR is a typedef for const char * and in this case you should be able to convert between them loosely. If not, a typecast will work. E.g.
MessageBox(0, (LPCTSTR)message, caption, MB_OK);

Update: If you have Unicode set, it won't work.
LPCSTR = const char *
//WinDef.h
typedef const char* LPCSTR;
LPCTSTR = const char *
//WinDef.h
#ifdef _UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif

EDIT: cspotcode, that works. Which compiler switch did you use?

Edited by PlasticineGuy, 07 January 2010 - 11:28 AM.

  • 0

#35 cspotcode

cspotcode

    GMC Member

  • New Member
  • 3 posts

Posted 30 December 2009 - 12:04 PM

I didn't use any compiler switches other than whatever was default. I tried to find a special option in Dev-C++ similar to the /MT /MD option in VC++, but I couldn't. So whatever is happening is Dev-C++ default.

Also since I just realized Dev-C++ is ancient and unsupported, I should clarify that I installed MinGW separately from Dev-C++ and I'm switching to Code::Blocks as my IDE. I'm gonna see what Code::Blocks does by default. It might just be that, by default, MinGW either statically links or doesn't link to problematic libraries.

EDIT: Here it is. Default install of MinGW, default install of Code::Blocks (it autodetected MinGW), default DLL project template. Just like before I had to change the return type of the function from void to double to make it compatible with GM, and consequently had to add a "return 0;". Other than that, no changes.
testDllDriver2.exe
testDllDriver2.gmk

Edited by cspotcode, 30 December 2009 - 12:16 PM.

  • 0

#36 PsichiX

PsichiX

    GMC Member

  • GMC Member
  • 399 posts
  • Version:GM:Studio

Posted 30 December 2009 - 12:09 PM

PlasticineGuy: right, in project options he should switch Char set to "no set" (in general options).

Edited by PsichiX, 30 December 2009 - 12:10 PM.

  • 0

freckles_sign.png


#37 flyslasher

flyslasher

    GMC Member

  • GMC Member
  • 1064 posts

Posted 30 December 2009 - 12:10 PM

I thought it might be good to post this here, for setting up GM compatible DLL's for Dev-C++. Its probably exactly the same as setting up as the tutorial of this topic...

You can get Dev-C++ here

After you install, or already got it go to C:\Dev-Cpp\Templates and create a file called 6-GMDLL.template and put this,
[Template]
ver=1
Name=Game Maker DLL
IconIndex=3
Description=A Dynamic Link Library (DLL) That Can Be Used With Game Maker
Catagory=Basic

[Unit0]
CName=gmdllmain.c
CppName=gmdllmain.cpp
C=GMDLL_c.txt
Cpp=GMDll_cpp.txt

[Unit1]
CName=gmdll.h
CppName=gmdll.h
C=GMDLL_h.txt
Cpp=GMDll_hpp.txt

[Project]
UnitCount=2
Type=3
Name=My GM_DLL
Compiler=-DBUILDING_DLL=1
CppCompiler=-DBUILDING_DLL=1
Linker=--no-export-all-symbols --add-stdcall-alias

then make another file called GMDLL_cpp.txt and put this,
#include "gmdll.h"
#include <windows.h>

/* Place your functions down here like so... */

export double mult( double x, double y )
{
	  return(double)x*y;
}

and then again, make another file called GMDLL_hp.txt and put this,
#ifndef _DLL_H_
#define _DLL_H_
/* Begin User-Defined */
#define export extern "C" __declspec (dllexport)
/* End User Defined   */

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */


class DLLIMPORT DllClass
{
  public:
	DllClass();
	virtual ~DllClass(void);

  private:

};


#endif /* _DLL_H_ */

and finally make a file called GMDLL_c.txt and put,
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>

/* Place your functions down here like so... */

export double mult( double x, double y )
{
	  return(double)x*y;
}

Then, just open up Dev-C++, and click File -> New -> Project and now a new Project option should come out called : Game Maker DLL.

You will still need to finish Plasticine tutorial to figure out how to make GM read the DLL right so... GOOD LUCK!

Edited by flyslasher, 30 December 2009 - 11:12 PM.

  • 0
Posted Image

#38 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 31 December 2009 - 09:38 AM

Won't work.
- export is already defined as a C++ keyword; that will throw an error or at least a warning.
- You left in all the default items from Dev-C++'s template. Templates suck. Really. They are okay if you leave the default files empty.

Edited by PlasticineGuy, 31 December 2009 - 12:12 PM.

  • 0

#39 flyslasher

flyslasher

    GMC Member

  • GMC Member
  • 1064 posts

Posted 31 December 2009 - 11:39 PM

Won't work.
- export is already defined as a C++ keyword; that will throw an error or at least a warning.
- You left in all the default items from Dev-C++'s template. Templates suck. Really. They are okay if you leave the default files empty.


I don't know, but most of its from another tutorial a mod posted, and I made it into a template really so I could always have it when I needed it, without having to remember where to find everything and for the most part it works, but then again I'm real suckish at C++ so I never made anything more than a few tests.
  • 0
Posted Image

#40 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 01 January 2010 - 03:54 AM

MinGW (what Dev-C++ uses) does not throw warnings by default. Turn those on; you'll see the compiler complaining about redefining keywords.
I'm just saying take out all the crap that comes with it. You don't need:
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
This is for C++ DLL linking. GM uses it differently, so there is no need. Also, your code is fishy. Typecasts are slow ([citation needed]). There is no need to cast x * y to a double, because they are both already doubles (and since you defined the return type as double, the compiler will automatically convert).

Edited by PlasticineGuy, 01 January 2010 - 11:37 AM.

  • 0

#41 flyslasher

flyslasher

    GMC Member

  • GMC Member
  • 1064 posts

Posted 01 January 2010 - 04:49 AM

I'm still learning C++ and you can just use that as an example. It was really meant to show anything but give the idea of where to start putting functions but still I should have seen it as it is obvious. I am not too sure on how everything works, and still it confuses me to hell.. just putting my two cents of what helped me, was make a template of the code. Of course your tutorials code could also be used in a template.

Edited by flyslasher, 01 January 2010 - 04:52 AM.

  • 0
Posted Image

#42 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 01 January 2010 - 04:50 AM

So, in other words, you attempted to upstage my tutorial with one that has bloat and far fewer learning points, and you don't even understand it fully?
  • 0

#43 flyslasher

flyslasher

    GMC Member

  • GMC Member
  • 1064 posts

Posted 01 January 2010 - 05:09 AM

Thats not what I was trying, or atleast not what I was aiming for. I was just more or so mainly suggesting that it might be helpful to make a template if u had Dev-CPP. I didn't mean to upstage you. Sure, maybe you could perfect my "ill" written template and make use of it your self in your tutorial.
  • 0
Posted Image

#44 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 01 January 2010 - 05:20 AM

I would, but I don't use Dev-C++ (it's 5 years old and unsupported). I might think about it.

Edited by PlasticineGuy, 01 January 2010 - 05:21 AM.

  • 0

#45 flyslasher

flyslasher

    GMC Member

  • GMC Member
  • 1064 posts

Posted 01 January 2010 - 05:25 AM

There might be some sort of way to make templates of some sort for visual c++ and atleast I would appreciate a tutorial on how to do it ^^, but even then this tutorial has helped me alot too :P.
  • 0
Posted Image

#46 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 01 January 2010 - 11:36 AM

There are templates for Visual C++, but unfortunately it appears that the only way to do so is using JavaScript etc. to make a fully fledged wizard. So instead I made a header and added its directory to the linker.
  • 0

#47 jabelar

jabelar

    GMC Member

  • GMC Member
  • 3003 posts

Posted 07 January 2010 - 08:30 AM

Thanks this is an interesting tutorial. I'll have to try doing my own dlls to really make sure I understand it well. It is really all the details (like the above posts about unicode text and your tips about casting the handle types and such) that can really get you and it is nice when someone like yourself takes the time to share what they've learned.
  • 0

#48 Recreate

Recreate

    Furry

  • GMC Member
  • 2992 posts
  • Version:GM:Studio

Posted 24 January 2010 - 08:03 PM

Worked fine for me using code::blocks.
Thanks for this! :snitch:
  • 0

If the post that you are reading was created prior to 2011. For the safety of the general public, It is not to be regarded under any circumstances.
Please don't ask me to join your group at anything.


#49 HaRRiKiRi

HaRRiKiRi

    GMC Member

  • GMC Member
  • 1364 posts

Posted 12 February 2010 - 10:12 PM

Update: If you have Unicode set, it won't work.
LPCSTR = const char *
CODE
//WinDef.h
typedef const char* LPCSTR;

LPCTSTR = const char *
CODE
//WinDef.h
#ifdef _UNICODE
typedef const wchar_t* LPCTSTR;
#else
typedef const char* LPCTSTR;
#endif

Under General Options in Project Settings, ensure that Char is "no set".

Can actually explain what this is about? I think I have the problem with this because it says syntax error : 'string' when compiling. And this this is the part I don't understand. Do we need to make the WinDef.h ? And if so then do we need to import it in the main.cpp?

Good tut thou. :D
  • 0

#50 PlasticineGuy

PlasticineGuy

    GMC Member

  • New Member
  • 2384 posts

Posted 12 February 2010 - 11:20 PM

Sorry, you got a bit confused. WinDef.h is a Microsoft header automatically included by windows.h. I was showing why with Unicode the DLL fails to compile.

As for your error, post the full compile log and your source code.

Edited by PlasticineGuy, 12 February 2010 - 11:21 PM.

  • 0