Sei sulla pagina 1di 4

C++ DLL Tutorial

By VitalDragon (9/18/04) Downloaded From: http://mdgames.atspace.com Note: This tutorial is not meant for beginners, it is meant for those who are experienced in GML, and somewhat experienced in (C/C++). This is not a C++ Tutorial, it does not teach you how to program in C/C++, just how to make GM Compatible DLLs using C++. Table of Contents Introduction Getting your compiler Exporting Functions Message (Box) DLL (String arguments) Addition DLL (Real Number arguments) Calling the DLLs from Game Maker Conclusion Introduction Everyone has heard of C++. Its the most popular high-level language there is. Its an extension of the C language, and is very powerful. You can easily program your DLLs in C++ for Game Maker, you should have no problems at all. Advantages of using C++ DLLs - Relatively small DLL size (smaller than using Visual C++ anyway) - Easy to program & Fast - A lot of power Getting and Adjusting your compiler Now youre ready to get your C++ Compiler. We are going to use Dev-C++, because it is compact, easy to use, and creates executables smaller than Microsofts Visual C++ (which is loaded with runtimes btw). Dev-C++ isnt a compiler, just an IDE for the Mingw compiler. Go to http://bloodshed.net/dev/devcpp.html. The version I am using for this tutorial is Dev-C++ 4.9.9.0. Download the one that includes the compiler. Set your Project for DLLs This is easy. After youve downloaded and installed your Dev-C++ compiler, just go to File -> New -> Project, and choose DLL. Itll ask you to save your project filenames, call them whatever you wish. Now you will see two filenames at the top, dll.h and dllmain.cpp, click on the dllmain.cpp tab, which is where we will be working. Exporting Functions You define exported functions using this code: extern "C" __declspec(dllexport) __stdcall [function and args here]; Then for the actual function you place extern "C" __declspec(dllexport) __stdcall right before it, for example: extern "C" __declspec(dllexport) __stdcall double myfunc(double arg1, double arg2)

The examples should clear everything up if youre confused. Message DLL (String Arguments) Finally, were ready to begin the actual DLL Programming. Make sure everything is adjusted or else it wont compile right. Heres the Dev-C++ code, should be in whatever your dllmain.cpp file is called. //Message Dev-C++ DLL Example /* Replace "dll.h" with the name of your header */ #include "mdll.h" #include <windows.h> LPSTR result = "Return this"; //Just a dummy return string extern "C" __declspec(dllexport) __stdcall LPSTR message(LPSTR title, LPSTR text); extern "C" __declspec(dllexport) __stdcall LPSTR message(LPSTR title, LPSTR text) { MessageBox(NULL,text,title,MB_OK); return result; //It has to return something or else it won't work }; DllClass::DllClass() { } DllClass::~DllClass () { } BOOL APIENTRY DllMain (HINSTANCE hInst */ , DWORD reason being called. */ , LPVOID reserved { switch (reason) { case DLL_PROCESS_ATTACH: break; case DLL_PROCESS_DETACH: break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; } /* Returns TRUE on success, FALSE on failure */ return TRUE; } /* Library instance handle. /* Reason this function is /* Not used. */ )

The main code, thats actually the message function is here: extern "C" __declspec(dllexport) __stdcall LPSTR message(LPSTR title, LPSTR text) { MessageBox(NULL,text,title,MB_OK); return result; //It has to return something or else it won't work }; You use the MessageBox function API, the arguments are title and text, and the result is in a string called result, which is just a dummy return string.

Some Notes: - GM has to have something returned - You must use the extern "C" __declspec(dllexport) __stdcall syntax before the function name and arguments Addition DLL Now were going to use Real Number arguments, instead of string arguments in the MessageBox example. //Addition Dev-C++ DLL Example /* Replace "dll.h" with the name of your header */ #include "adll.h" #include <windows.h> extern "C" __declspec(dllexport) __stdcall double addition(double arg1, double arg2); extern "C" __declspec(dllexport) __stdcall double addition(double arg1, double arg2) { return arg1+arg2; };

DllClass::DllClass() { } DllClass::~DllClass () { } BOOL APIENTRY DllMain (HINSTANCE hInst */ , DWORD reason being called. */ , LPVOID reserved { switch (reason) { case DLL_PROCESS_ATTACH: break; /* Library instance handle. /* Reason this function is /* Not used. */ )

case DLL_PROCESS_DETACH: break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; } /* Returns TRUE on success, FALSE on failure */ return TRUE; } The main part of the code, that does the addition is here: extern "C" __declspec(dllexport) __stdcall double addition(double arg1, double arg2) { return arg1+arg2; }; It takes arg1 and arg2 and adds them together, and then returns it as the result. Calling the DLLs from Game Maker This must be the easiest part of everything. Anyway, heres the calling code for the Message DLL: { global.message = external_define('message.DLL','message',dll_stdcall,ty_string,2,ty_stri ng,ty_string); external_call(global.message,"Title","Text") } Calling code for the Addition DLL: { global.addition = external_define('addition.DLL','addition',dll_stdcall,ty_real,2,ty_real ,ty_real); result = external_call(global.addition,12,12) } Conclusion Its pretty easy to program DLLs in Dev-C++, give it a try, and then make some DLLs.

Potrebbero piacerti anche