Development Blog‎ > ‎

C#, nFringe and DllBind

posted Mar 1, 2012, 1:48 PM by Mavrik Games   [ updated Mar 1, 2012, 2:53 PM ]
I know it hasn't been very long since my last post, but I've found out a lot of interesting things in the last 24 hours and I thought I would share to make it easier for anyone else searching for some answers.

First of all, if you haven't heard of DllBind, go check it out. It's a way for UDK (non licensee) developers to implement external C code into UnrealScript. I wanted to try using this to encode the Json data from the UDK Gem save system so that it would be harder for players to cheat. As I started looking into it, I read that any language that can be passed through a C compiler could be used, so I wondered if there was any reason I couldn't use C# instead of C++, given that I'm much more familiar with it due to my work with XNA on The Last Pod Fighter. I found out that the DllBind functionality in UnrealScript is only able to use un-managed dlls, and C# of course uses managed binaries and a JIT compiler (just like Java). I wasn't too discouraged as I actually do have a fair amount of experience with C++, but I'm just more comfortable with C#. Just as I had given up on the idea, I ran across a tutorial that talked about using C# for DllBinding in UDK. It shows how to use a Visual Studio template created by Robert Giesecke to export your managed code to un-managed dlls. He also shows how to marshal string parameters and return values so that they're compatible with UDK's strings (as mentioned on the DllBind page, UDK's strings are quirky). As far as I know, you don't need to marshal other data types though, as these are the same in both languages.

I downloaded Robert's template according to the instructions on his page, but it didn't work exactly like it was supposed to. After some debugging though, I figured out why. In the archive, there are seven or eight files in the DllExport folder that need to be copied over to the DllExport folder in your project. I suspect that the process of creating a new project using the template is supposed to do this for you, but for whatever reason it didn't (maybe because I'm using VS C# Express edition?). Also, in order to compile the dll for my class, I had to install Visual Studio C++ Express as well because there are some binaries that his system uses that aren't included with the C# version.

I finally had a dll that compiled without any errors or warnings, so I opened up my UnrealScript project and started to add a DllBind to my save class. Apparently, you're not allowed to have anything else in the class other than the function declarations, so I cleared it out of the save class and made a new class with only the function declarations. This is where I hit another hurdle. The IDE I use for my scripts is Pixel Mine's Visual Studio based IDE nFringe, and even though I have the latest version, its parser didn't recognize the dllimport keyword so my script wouldn't compile. I was afraid that I would have to resort to compiling via command line, but I did some Googling and found out that they've included a way to add keywords to their parser by adding the keyword in a file called ScriptModifiers.xml in the Development/Src/Core folder. The xml file isn't there to begin with - you can download an example from their website, which includes a whole bunch of extra keywords, or just write your own following the proper schema - but once you have it, you just add your keyword to the appropriate section and restart Visual Studio. After adding the dllimport and dllbind keywords to the "function" category, my scripts compiled just fine and I was able to call my C# functions from UnrealScript!

It was a lot of trouble to go through just to encode the save data and discourage cheating, but I think that there's a real possibility that I'll use DllBinds in the future, so it was totally worth it.