Here's a simple guide using visual C++ to build a DLL.
If you're using a different compiler you'll have to figure out how to get it to build DLL's that are compatible with win32API calls
Get visual C++
Download visual C++ from microsoft. Any version should be fine. I just get the express edition since it's free.
Setting up a project
If you are going to build your DLL through the IDE, 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.
4. Next
5. Select "DLL" and "Empty Project"
6. Finish
Now right-click on the the "Header Files" and add a header file, and right-click "Source Files" and add a code file.
Getting cl and setting it up
If you're going to compile your DLL via command-line manually, you will need to set up your environment first.
1. Go to your installation folder -> VC -> bin and you should see "vcvars32.bat"
2. Open command-line and then run that batch file to set up the environment.
3. Type cl and press enter and it should work.
Step 2 is optional; you can set your env vars to include visual studio folders.
Writing some code
Here's a header and code file
test.h
#define MY_FIRST_DLL __declspec(dllexport)extern "C" MY_FIRST_DLLint hello(void);test.cpp
Don't forget the extern "C" otherwise your calls from within RM will fail.Building your DLL
Now you can build your DLL. You can either build it through the IDE, or through command-line using the cl.exe compiler. I would recommend the IDE since it does a lot of optimizations for you.
Building in the IDE
1. In the menu, click Build --> Build Solution
2. If everything works, you should have your DLL in your project folder
That's it. Typically 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.
Building in command-line
Here's some basic instructions that don't consider any optimization settings. You can look up the compiler flags if you want.
The following command will build a DLL
cl /LD test.cpp /Fetest.dllBasically- LD specifies you are making a DLL
- Fe renames the output file. Useful if you want the output name to be different from the source filename
Now you should get a bunch of files (test.obj, test.exp, test.lib, etc) along with the test.dll you want. At some point you will probably want to look at optimizing your stuff.
Calling your DLL
Now that you have your DLL just make a project and create an object for your DLL function
myFunc = Win32API.new("test.dll", "hello", "", "L")The four arguments that are passed in are- the path to the DLL (rooted under your project)
- the name of the function you want
- 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.
- Type of return value
The types of the arguments are easy enough: just use whatever your function signature wants.
The return value is also pretty straightforward. You'll probably be working with integers/longs and pointers for the most part.
Now you can call your function, passing in the appropriate arguments (in this case nothing)
p myFunc.call()After making the call, you should see that 1 printed out successfully in your console.That's pretty much it. At this point it's just a matter of figuring out how to write your code and how to pass things back and forth.
Note: the only reason I'm writing DLL's is because certain things are too slow in ruby to be feasible, such as image processing. If I can do it in ruby and it's fast "enough", I would stick with Ruby since it makes it much easier to fix and build on.
Reference
Compiler options for cl
http://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx
Parameter types and their symbols
http://rubyforge.org/docman/view.php/85/3463/API.html#M000001 (suggest better reference)
Thanks to Zeriab and Solistra for the steps to get RM to talk to a DLL.
MGC for directing me to the compiler used in visual C++
If you're using a different compiler you'll have to figure out how to get it to build DLL's that are compatible with win32API calls
Get visual C++
Download visual C++ from microsoft. Any version should be fine. I just get the express edition since it's free.
Setting up a project
If you are going to build your DLL through the IDE, 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.
4. Next
5. Select "DLL" and "Empty Project"
6. Finish
Now right-click on the the "Header Files" and add a header file, and right-click "Source Files" and add a code file.
Getting cl and setting it up
If you're going to compile your DLL via command-line manually, you will need to set up your environment first.
1. Go to your installation folder -> VC -> bin and you should see "vcvars32.bat"
2. Open command-line and then run that batch file to set up the environment.
3. Type cl and press enter and it should work.
Step 2 is optional; you can set your env vars to include visual studio folders.
Writing some code
Here's a header and code file
test.h
#define MY_FIRST_DLL __declspec(dllexport)extern "C" MY_FIRST_DLLint hello(void);test.cpp
Code:
#include "test.h"MY_FIRST_DLLint hello(void){ return 1;}
Now you can build your DLL. You can either build it through the IDE, or through command-line using the cl.exe compiler. I would recommend the IDE since it does a lot of optimizations for you.
Building in the IDE
1. In the menu, click Build --> Build Solution
2. If everything works, you should have your DLL in your project folder
That's it. Typically 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.
Building in command-line
Here's some basic instructions that don't consider any optimization settings. You can look up the compiler flags if you want.
The following command will build a DLL
cl /LD test.cpp /Fetest.dllBasically- LD specifies you are making a DLL
- Fe renames the output file. Useful if you want the output name to be different from the source filename
Now you should get a bunch of files (test.obj, test.exp, test.lib, etc) along with the test.dll you want. At some point you will probably want to look at optimizing your stuff.
Calling your DLL
Now that you have your DLL just make a project and create an object for your DLL function
myFunc = Win32API.new("test.dll", "hello", "", "L")The four arguments that are passed in are- the path to the DLL (rooted under your project)
- the name of the function you want
- 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.
- Type of return value
The types of the arguments are easy enough: just use whatever your function signature wants.
The return value is also pretty straightforward. You'll probably be working with integers/longs and pointers for the most part.
Now you can call your function, passing in the appropriate arguments (in this case nothing)
p myFunc.call()After making the call, you should see that 1 printed out successfully in your console.That's pretty much it. At this point it's just a matter of figuring out how to write your code and how to pass things back and forth.
Note: the only reason I'm writing DLL's is because certain things are too slow in ruby to be feasible, such as image processing. If I can do it in ruby and it's fast "enough", I would stick with Ruby since it makes it much easier to fix and build on.
Reference
Compiler options for cl
http://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx
Parameter types and their symbols
http://rubyforge.org/docman/view.php/85/3463/API.html#M000001 (suggest better reference)
Thanks to Zeriab and Solistra for the steps to get RM to talk to a DLL.
MGC for directing me to the compiler used in visual C++
Last edited by a moderator:

