cppdll

Upload: leo-vasquez-blas

Post on 03-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 cppdll

    1/4

    C++ DLL TutorialBy 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

    IntroductionGetting your compiler

    Exporting FunctionsMessage (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. Itsan 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 compilerNow 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 VisualC++ (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 amusing 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 goto 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)

  • 7/29/2019 cppdll

    2/4

    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 inwhatever your dllmain.cpp file is called.

    //Message Dev-C++ DLL Example

    /* Replace "dll.h" with the name of your header */

    #include "mdll.h"

    #include

    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 /* Library instance handle.

    */ ,DWORD reason /* Reason this function is

    being called. */ ,

    LPVOID reserved /* Not used. */ )

    {

    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;

    }

  • 7/29/2019 cppdll

    3/4

    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 theMessageBox example.

    //Addition Dev-C++ DLL Example

    /* Replace "dll.h" with the name of your header */

    #include "adll.h"

    #include

    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 /* Library instance handle.

    */ ,DWORD reason /* Reason this function is

    being called. */ ,

    LPVOID reserved /* Not used. */ )

    {

    switch (reason)

    {

    case DLL_PROCESS_ATTACH:

    break;

  • 7/29/2019 cppdll

    4/4

    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 MakerThis 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)

    }

    ConclusionIts pretty easy to program DLLs in Dev-C++, give it a try, and then make some

    DLLs.