Click here to Skip to main content
6,629,377 members and growing! (21,486 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate

How to create a DLL library in C and then use it with C#

By Vladimir Tskhvaradze

An easy-to-use example of how to create a DLL library in C and then use it with C#.
C++, C#.NET 1.1, WinXP, Dev
Posted:13 Mar 2005
Views:104,757
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
36 votes for this article.
Popularity: 5.70 Rating: 3.66 out of 5
3 votes, 8.3%
1
4 votes, 11.1%
2
4 votes, 11.1%
3
8 votes, 22.2%
4
17 votes, 47.2%
5

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.

  1. Start Visual Studio .NET.
  2. Go to File->New->Project.
  3. Select Visual C++ Project, and from the �Templates�, select �Win32 Project�.
  4. Give the name to your project. This will be the name of your final DLL (in my case: TestLib).
  5. Press OK.
  6. Select DLL from �Application Type� (�Application Settings� tab).
  7. Check �Empty Project� (we need to create our project from scratch, right?), and press Finish.

OK, now we should attach an empty source file to our blank project.

  1. Start Solution Explorer (if it�s not displayed).
  2. Right click to the �Source Files�, Add->Add New Item then select �C++ File� and give the name to it.
  3. Press �Open�.

In the opened window, enter the following code:

#include <stdio.h>


extern "C"
{
  __declspec(dllexport) void DisplayHelloFromDLL()
  {
    printf ("Hello from DLL !\n");
  }
}

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:

  1. Start Visual Studio .NET.
  2. Go to File->New->Project.
  3. Select Visual C# Project. From the �Templates�, you can either select �Console Application�, or an �Empty Project� just like it was described above.
  4. Give the name to your application.
  5. Press OK.

Into the specified class, insert the following two lines:

[DllImport("TestLib.dll")]

public static extern void DisplayHelloFromDLL ();

In C#, keyword extern indicates that the method is implemented externally.

Your code should look something like this:

using System;
using System.Runtime.InteropServices;     // DLL support


class HelloWorld
{
    [DllImport("TestLib.dll")]
    public static extern void DisplayHelloFromDLL ();

    static void Main ()
    {
        Console.WriteLine ("This is C# program");
        DisplayHelloFromDLL ();
    }
}

Please, note that System.Runtime.InteropServices is required for operations with the DLL.

According to MSDN:

�The System.Runtime.InteropServices namespace provides a collection of classes useful for accessing COM objects, and native APIs from .NET�

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.

A list of licenses authors might use can be found here

About the Author

Vladimir Tskhvaradze


Member

Occupation: Other
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
General"An attempt was made to load a program with an incorrect format" Pinmemberlonik.r23:50 21 Jul '09  
QuestionHow to use this .dll and .exe in a System, which having only .net frame work, (not Visual Studio) PinmemberMember 37763672:14 27 Nov '08  
QuestionDo you Think it is working ?? PinmemberMember 339892421:15 12 Oct '08  
AnswerRe: Do you Think it is working ?? PinmemberJonghoon Han6:34 19 Nov '08  
GeneralHow Do i Create a MyLib Pinmembervijaymunnoli1:09 12 Jun '08  
QuestionWhy doesn't this work with Visual Studio 2005 Pinmembercanhestros4:44 3 Apr '08  
GeneralRuntime exception while loading dll [modified] Pinmemberparazitu3:52 3 Nov '07  
GeneralReally helped me. Pinmemberramachandragowda6:31 2 Oct '07  
GeneralNice code Pinmemberbijulsoni14:23 16 Aug '07  
Generalwhat about using classes Pinmembersoorajvs20051:14 13 Aug '07  
QuestionDoes not work on other pc PinmemberRafan312:59 1 May '07  
AnswerRe: Does not work on other pc [modified] PinmemberMichael900014:18 28 Jun '08  
GeneralEmbedded Resource in .NET Pinmembertanveerakl0:48 4 Jul '06  
Generalreleasing resources Pinmemberfatih isikhan4:08 5 Dec '05  
QuestionUsing strings parameters in C++ DLL PinmemberBillMcNeil9:05 1 Dec '05  
Generalextern not necessary PinmemberMark Zudeck9:55 27 Oct '05  
GeneralNice article ! PinmemberGiorgi Moniava11:01 17 Oct '05  
Generalexport/import a class... Pinmemberd00_ape5:13 20 Jun '05  
GeneralCreate Dll library in C and then use it with C#, but can I create Dll Library in C# and then use it with C, VC/C++ ? PinsussLiTaiNien19:18 17 Jun '05  
GeneralMarshalling Pinmemberpjcrosbie18:19 8 Apr '05  
Generalhow to create reusable library(DLL),using recursive algorithms? Pinmemberdcvam18:39 3 Apr '05  
GeneralImportant: Handling bool returns PinmemberMark Jerde5:59 30 Mar '05  
GeneralC# calling a Cpp dll PinmemberSHG22:25 28 Mar '05  
Generalgreat article PinmemberNik Vogiatzis4:12 27 Mar '05  
GeneralC# dll like C dll Pinmembermsali4:06 26 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Mar 2005
Editor: Smitha Vijayan
Copyright 2005 by Vladimir Tskhvaradze
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project