Okey, long time didn’t post a good stuff.
On this post I’ll show you how to make a C# unmanaged DLL, As we now DLL C# doest like C, using code injection with C DLL will starting execute our code by call DLLMain fuction, we don’t following that way to make DLL in C#, we will using another method like using dllexport then use GetProcAddress to get our address of fuction then run the fuction.
Robert Giesecke has made a project template to make a dll using that method on C#, so lets we say thanks to Robert Giesecke.
Tools We Need :
- Project Template From Robert Giesecke.
- Visual Studio C# 2010 / 2011 (Im using C# 2010).
- Knowledge about C#.
Ok Lets Start :
First download Robert Giesecke Project Template from her site, after you downloaded the file, put it on Visual Studio C# 2010/2011 Project Template.
Look at the image, I have show you the path, and you have put the zip (don’t extract the template’s zip) there.
After you put the zip on Visual C# Project Template folder, now open your Visual Studio 2010 / 2011, Create a C# new project, find Unmanaged Export Library from Visual C# Installed Templates, Give a name of your project then OK.
After create a project u will have dllexport folder on your project and UnmanagedExports.cs, open UnmanagedExports.cs its will be the class have function same as dllexport function on C, and this file will be the entry point for our DLL. Look at the first code off UnmanagedExports.cs
PHP Code:
[DllExport("adddays", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static double AddDays(double dateValue, int days)
{
return DateTime.FromOADate(dateValue).AddDays(days).ToOADate();
}
You can change DllExport function name with change adddays to what ever you want. And I will change my code to be.
PHP Code:
[DllExport("DLLMain", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static void DLLMain()
{
}
If you want doing something like write memory or reading or patching or what ever in DLLMain, yeah you can do it, but maybe if we split the class its will be neat. Ok so lets make Windows Form class, give the name Main for our new Windows Form Class.
Ok after creating new Windows Form Class, lets we call the our form when DLLMain function is execute. In unmanagedExports.cs call your windows form call your main form with make a thread.
PHP Code:
[DllExport("DLLMain", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static void DLLMain()
{
new Thread(() =>Application.Run(new Main())).Start();
}
Oke, Just build / publish your dll and use DLL, I’ll make tutorial how to make injection for this DLL later, couse I have to work now .
But if you want to make it injection. Like what I say. First you have injected DLL into process, and second you have find your function address (at this case DLLMain function address) then execute that process. You can use BlackMagic, WhiteMagic, or Syringe DLL library to make it easy to inject this dll method. or you can follow dwar to call export function from injected dll but it in C. i'll make it with C#
Sorry for my English, if you have question free to ask.
Thx To :
- Xcvd
- Robert Giesecke
=========================UPDATE=================== =========
I have make tutorial for inject dll make from this tutorial, here is the link