Introduction

After spending some time trying to implement this simple task, I started to search similar code examples over the Internet. I was really very surprised when found that all examples were slightly different from what I needed. Finally, I realized that there is no (at least spending 30 min in the net) easy-to-use example, that’s why I decided to write this article.

Assuming that you already know what a DLL is, let's begin with creating a simple one.

Please note that __declspec(dllexport) is an obligatory prefix which makes DLL functions available from an external application.

extern “C” (with brackets) is also very important, it shows that all code within brackets is available from “outside”. Although code will compile even without this statement, during runtime, you’ll get a very unpleasant error. So, do not forget to include it.

Build this application and your DLL will be ready-to-use.

Now it’s time to create an application which will use our DLL, as the main title explains. We will create this type of application using Microsoft’s C#.

Creating a simple C# application:

Start Visual Studio .NET.

Go to File->New->Project.

Select Visual C# Project. From the “Templates”, you can either select “Console Application”, or an “Empty Project” just like it was described above.

OK, now build this application, and then copy the previously built DLL into the Debug/Release directory of the current application. The DLL should be in the same directory as your main application.

Now start an application. If everything you’ve made was correct, you should get something like this:

That’s all, really simple isn’t it?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

The answer is no, we do not need to create our project from scratch. Some of what is in this article will be done for us automatically and more usefully if we leave the checkbox unchecked for creating an empty project and by checking the checkbox for exporting symbols. When we choose the checkbox for exporting symbols, a nice little macro is generated that makes use of __declspec easier.

I highly recommend that beginners do not select the option to create an empty project and that they also select the option to generate symbols.

Unable to load DLL 'TestLib.dll' <- this is because of the following errors - which prevented the .dll from being built, which of course means it is not saved so cannot be found ... which is what it says.

The C2059 is as you rightly noted in relation to the extern "C" instruction which does not work in ANSI C
That error caused the LNK1120 error.

http://Online-Safety.Net
Save a Child from sexual abuse and being bullied to death today.

can you give me one of your instant messengers id ? i wanna ask u some questions wich can be helpfull in real time, althought if u have sufficient time , also i promise i wont stress u with anything when u are busy . regards and best wishes.

I voted like this, because there are some problems which relates to this subject, but the writer didn't mention them at all.
For example: When you want to put more that one function in the dll, there will be a problem that throws an exception in the C# project ..
But I don't say that the task wasn't helpful ..
Thanks

Hi, i have Windows XP SP3 and using Visual Studio 2005.
I´m trying to use some methods from imported dll (eg. [DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Output(int address, int value)).
But when I call this method, I still have this exception: "An attempt was made to load a program with an incorrect format".
I tried to switch Platform target to x86 in all projects, but with no success.
Any suggestions?

This happens if the .NET application is being built with the wrong CPU in mind. You can change the target platform in settings. Change it to x86. This happens because the .DLL is being built for x86 and the .NET app is being built for x64. By default it is set to automatic but if you are building on an x64 platform then it chooses x64.

Hello,
Thank you for your article. Its excellent and working in any system which one having Visual Studio. But when I copy the same .dll and .exe file in to a new system, which one having only .net frame work, not visual studio, then the .exe file is not working. How can we solve this problem. If we can run this application in any system, which one having .net framework, then its a good achievement.
Thank you

Please try atleast once by yourself before posting blunders.
I tried this hell and and belive me this bull sh*t is not at all working. It gives me an error showing a static entry point to the DLL was not found.
For heven's sake please dont post such articles. It causes a waste of time - for you and ourself..
Good luck to write error free programs in future.