How to import use dynamic libraries in your project

Often before .NET programmer the task arises: to use the functionality of DLLs written by another developer, on unmanaged code. Sometimes the functionality of this library can be very extensive. Manually in my project every class, function, constant, etc. – a very long time. Fortunately, there are tools that partially automate this process. One of them we’ll talk.

PInvoker для Visual Studio

You will need

  • PC c Visual Studio 2008/2010;
  • — the Internet.


1. If you have a dynamic library *.dll written in unmanaged code, e.g. C or C++ and its header (header) files, and you need to declare your project a whole bunch of designs from the library, it is a very good solution for you may be the tool PInvoker. PInvoker is freely distributed as an extension for the Visual Studio IDE (supports versions 2005, 2008 and 2010), as well as in a separate application.
Working with him is simple and convenient. PInvoke PInvoker imports definitions for the managed code in C# or VB.NET from the header files for C/C++ and associated DLLs. You will only have to choose from the list of available functions, structures, enumerations, constants, delegates, etc. necessary and import it into your project.
First you need to download and install PInvoker. Download from the official site installer PInvoker.msi or extension PInvokerAddin.msi for Visual Studio. I think this problem should arise. Just run the install file and follow the instructions.

2. Consider the example of how to work with this useful tool. Start Visual Studio, create a new project. In the tools menu -> Tools there is a new item: PInvoker. Click on it and you will open the settings window PInvoker Addin. Select the language of your project Language: C# or VB.NET. In the drop-down list of Profiles, select edit profiles.

Редактирование профилей импорта PInvoker

3. A window will open profile management. Select Create a new import profile and click Next.

4. Now we need to add a header header-files. Can drag them with the mouse on the window PInvoker Profiles Wizard. Next, specify the directory where these files are stored.

Добавление файлов заголовков

5. In the next step you must specify the dynamic library. It also can drag the mouse on the window of the profile wizard PInvoker.

Добавление динамической библиотеки DLL

6. After clicking on Finish will start the import process definitions from the selected dynamic library. On successful completion of the operation will be indicated by the inscription successful Import and appears in the field list of function names from the DLL.

Завершение настройки профиля импорта PInvoke определений из DLL

7. Now you can easily import the PInvoke definition to your project. In the left field (1), in field Type select type: function, procedure, structure, enumeration, constant, delegate, etc. In field Name , select the name of your desired design. By single-clicking its description will appear in the Central field of the window PInvoker (2). Double – opens its definition in the header file. Clicking Insert definition (3), you insert the definition into your project, C#/VB.NET. Now you can use import dynamic library in your project.

Импорт PInvoke определений из DLL в проект Visual Studio

Post Comment