Partager via


Slimmest way to call an exported function of unmanaged dll from a managed C# application

Many times I came to situation that someone don’t know a good way to call an exported function of unmanaged dll from a managed application C# or VB. Person might not have any working knowledge of unmanaged dll, if they are required to create one.

I am writing this post just to demonstrate, how to create a unm

anaged dll and then export a function from it and call that function from the managed C# application.

Create unmanaged dll to export a function

First of all, I create a dll project that exports symbols. I will get the default template project which is ready to export a function, but I will create my own function.

int MyWin32Function()
{
return 22;
}

If the declaration of the function is int MyWin32Function(); , then it will not be exported from the dll. You can check it from the dumpbin utility or Dependency Walker tool. How to make function exportable/visible to application? __declspec(dllexport) will be used to mark the function exportable. If the function declaration looks like below -__declspec(dllexport) int MyWin32Function(); , Is it exportable? Now if you compile it again and see from dumpbin, you see that the function is exported, but the name of the function is decorated. MyWin32Function will look like ?MyWin32Function@@SoMeChArS. This name decoration is because of name mangling of C++ compiler. It is not possible from the application to call the function with this name, the exported function from dll should be in human readable form, which is convenient, easy to document and easier to use. So, now the problem is, function is exported but with name decoration. We will need to eliminate name decoration so that application can use it easily. extern "C" will be helpful here, asking the C++ compiler to compile as C and no name decoration. Now, function declaration will look like below --

extern "C" __declspec(dllexport) int MyWin32Function();

The dll is ready to be used with exported MyWin32Function. Now, we need to call it from C# application.

 Now, you will need to call a unmanaged function from custom dll from a managed app, C#, need P/Invoke. I have discussed about the P/Invoke in my previous post. You can download P/Invoke Interop Assistant tool. You will need to create the signature of MyWin32Function(). I will use the declaration of my function in the tool and create a signature.

pinvoke

 

For simplicity, I will use the code in same class. Copy below code to C# application.

 /// Return Type: int
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint = "MyWin32Function")]
public static extern int MyWin32Function();

I will need to change the dll name to my dll name rather than <Unknown>.  

 /// Return Type: int
[System.Runtime.InteropServices.DllImportAttribute("MyWin32Dll.dll", EntryPoint = "MyWin32Function")]
public static extern int MyWin32Function();

 Now, You can call this function from C# application below-

 int a = 0;
a = MyWin32Function();
Console.WriteLine("returned from dll {0}",a);

To pass parameters from managed application to unmanaged dll function, you will need to work on marshalling the data structures.

Nitin Dhawan

Windows SDK - Microsoft