How to create a DLL library in C and then use it with C#
2005-12-22 00:14
1051 查看
http://www.codeproject.com/csharp/UseCDLLlibinCS.asp
Assuming that you already know what a DLL is, let's begin with creating a simple one.
Start Visual Studio .NET.
Go to File->New->Project.
Select Visual C++ Project, and from the “Templates”, select “Win32 Project”.
Give the name to your project. This will be the name of your final DLL (in my case: TestLib).
Press OK.
Select DLL from “Application Type” (“Application Settings” tab).
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.
Start Solution Explorer (if it’s not displayed).
Right click to the “Source Files”, Add->Add New Item then select “C++ File” and give the name to it.
Press “Open”.
In the opened window, enter the following code:
Please note that
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#.
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.
Give the name to your application.
Press OK.
Into the specified class, insert the following two lines:
In C#, keyword
Your code should look something like this:
Please, note that
According to MSDN:
“The
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?
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.
Start Visual Studio .NET.
Go to File->New->Project.
Select Visual C++ Project, and from the “Templates”, select “Win32 Project”.
Give the name to your project. This will be the name of your final DLL (in my case: TestLib).
Press OK.
Select DLL from “Application Type” (“Application Settings” tab).
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.
Start Solution Explorer (if it’s not displayed).
Right click to the “Source Files”, Add->Add New Item then select “C++ File” and give the name to it.
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:
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.
Give the name to your application.
Press OK.
Into the specified class, insert the following two lines:
[DllImport("TestLib.dll")] public static extern void DisplayHelloFromDLL ();
In C#, keyword
externindicates 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.InteropServicesis required for operations with the DLL.
According to MSDN:
“The
System.Runtime.InteropServicesnamespace 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?
About Vladimir Tskhvaradze
Hi, My name is Vladimir. I'm 23. I graduated from Tbilisi State University with BS Degree in Physics. (It's wrong to think that physicists aren't good enough for software development and programming...it's a common misconception, right ?) I'm Brainbench C and Brainbench C++ Fundamentals certified programmer. My interests range from physics to programming and electronics. Currently, I work on Microchip PIC applications, where as you may know, programming is a necessity. If you have any questions or comments feel free to contact me at: vova_ts@hotmail.com Click here to view Vladimir Tskhvaradze's online profile. |
相关文章推荐
- How to create a DLL library in C and then use it with C#
- Create a C++ DLL and Use It in C# (P/Invoke)
- How to create a project from existing source in Eclipse and then find it?
- How to create aligned partitions in Linux for use with NetApp LUNs, VMDKs, VHDs and other virtual di
- how to make Completion Handlers and use it in Swift
- How to create (and deploy) a windows service in C# ?
- Create a C++ DLL and Use It in Visual C++
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- How to create simple and advanced pivot tables in C# and ASP.NET
- [Android]:Setting up a Library Project and how to use the library (工程引用和dll引用)
- How to create fully custom Role, User, Event, Resource classes for use with the Security and Scheduler modules
- how to save a c++ object in java object and use it
- (zhuan) Attention in Neural Networks and How to Use It
- Use ANT and Maven to create executable jar with dependent library
- What is reflection and how to use it in CSharp ?
- How to use outline levels to create a table of contents (TOC) in Word 2003 and in Word 2002
- How to Create the CAPL DLL and integrated it into CANOE
- How to use Comparator and Comparable in Java? With example
- How to build & install GLFW 3 and use it in a Linux project