Writing your own DLL and calling it from RM

This tutorial demonstrates how to write C++ code, build it into a DLL, and then call it from RM using the Win32API class. There are many reasons why you might want to write certain parts of your scripts in native code (performance, security, etc).

You will learn how to successfully make your first DLL call and also get a better idea how DLL calls work and how to use existing DLL’s that come with Windows.

I use Visual C++ to build my DLL’s, so if you have your own preferred compiler you will need to figure out how to build DLL’s on your own.

Tools

Visual C++

You can download it from Microsoft. Any version is fine. I use the express edition. All instructions are based on Visual C++ 2010 Express edition. If you are using a different version you might have slightly different instructions.

Setting up a project

You will build your DLL through the IDE, so you should set up your project so that the IDE automatically builds a DLL.

  1. Create a new project (ctrl+shift+N)
  2. Select win32 console application
  3. Choose a name, hit OK

Another wizard will come up.

  1. Next
  2. Select “DLL” and “Empty Project”
  3. Finish

Now right-click on the the “Header Files” and add a header file, and right-click “Source Files” and add a code file.

Writing your first program

Here’s a header and code file with a sample program to make your DLL calls with.

test.h

#define MY_FIRST_DLL __declspec(dllexport)

extern "C" MY_FIRST_DLL
int hello(void);

extern "C" MY_FIRST_DLL
int add(int x, int y);

test.cpp

#include "test.h"

MY_FIRST_DLL
int hello(void)
{
   return 4;
}

MY_FIRST_DLL
int add(int x, int y)
{
   return x + y;
}

Don’t forget the extern “C” if you are compiling C++ code.

Building your DLL

To build your DLL, in the menu, click Build –> Build Solution. If successful, the DLL will be created and placed in your solution folder under the Debug or Release folder, depending on your build configuration settings.

When you are distributing your DLL,  you should set the build configuration to “release” instead of “debug”. You might see this in a dropdown near the top, or you will need to go to Build –> Configuration Manager and set the current active configuration.

Calling your DLL

Open an RM project and place the DLL in your project’s folder. Then, in a new script, write the following

fnHello =  Win32API.new("test.dll", "hello", "", "L")
fnAdd = Win32API.new("test.dll", "add", "LL", "L")

The four arguments that are passed in are

  1. the path to the DLL (rooted under your project)
  2. the name of the function you want to call
  3. the types of the arguments that you want to pass in. Could be an array of strings like [‘L’, ‘P’, ‘L’] or a single string “LPL”. Probably more variations. You will commonly use integers (L) and pointers (P) to pass data around.
  4. Type of return value

The types of the arguments is obtained from the function signature. In the case of our hello function, it doesn’t take any arguments, so you don’t have to pass in anything, but our add function takes two integers.

The return value is also obtained from the function signature. Our hello function returns an integer.

Now you can call your function, passing in the appropriate arguments

p fnHello.call()
p fnAdd.call(2, 4)

After making the calls, you should see that 4 printed out successfully in your console for the hello call, and 6 for the add call.

Moving forward

At this point, you should now have a basic template for writing libraries that can be used in conjunction with RPG Maker. The opportunities are limitless.

Spread the Word

If you liked the post and feel that others could benefit from it, consider tweeting it or sharing it on whichever social media channels that you use. You can also follow @HimeWorks on Twitter or like my Facebook page to get the latest updates or suggest topics that you would like to read about.

You may also like...

13 Responses

  1. skourpy says:

    is it posible to call the function on event script call ?

  2. SoulPour777 says:

    I have a question here, Tsukihime. How can we integrate the C++ dll into RM? Second, does it support encryption dlls?

  3. now I only need to know how to create things in C++… XD

  1. August 26, 2015

    […] Learn some useful external tools that can be used directly by RMVXA scripts, like using dll via Win32API. Some problems are inherently calculation intensive, and sometimes they can be solved using dll to […]

Leave a Reply

Your email address will not be published. Required fields are marked *