How To Quickly Import Dynamic Library Functionality Into Your Project

Table of contents:

How To Quickly Import Dynamic Library Functionality Into Your Project
How To Quickly Import Dynamic Library Functionality Into Your Project

Video: How To Quickly Import Dynamic Library Functionality Into Your Project

Video: How To Quickly Import Dynamic Library Functionality Into Your Project
Video: Using Dynamic Libraries in C+ 2024, November
Anonim

Often a. NET programmer is faced with the task of using the functionality of a dynamic DLL written by another developer in unmanaged code. Sometimes the functionality of this library can be very, very extensive. Manually register each class, function, constant, etc. in your project. - a very long time. Fortunately, there are tools that partially automate this process. We will talk about one of them.

PInvoker for Visual Studio
PInvoker for Visual Studio

Necessary

  • - PC with Visual Studio 2008/2010;
  • - the Internet.

Instructions

Step 1

If you have a dynamic library *.dll written in unmanaged code, for example, in C or C ++, as well as its header files, and you need to declare a whole bunch of library constructs in your project, then a very good solution for you can be the tool PInvoker. PInvoker is freely distributed as an extension for the Visual Studio IDE (versions 2005, 2008 and 2010 are supported), as well as as a standalone application.

Working with it is simple and convenient. PInvoker imports PInvoke definitions for managed C # or VB. NET code from C / C ++ header files and their associated DLL. You just have to choose from a list of available functions, structures, enumerations, constants, delegates, and so on. required and imported into your project.

First you need to download and install PInvoker. Download the PInvoker.msi installer or the PInvokerAddin.msi extension for Visual Studio from the official website. I think there shouldn't be any problems with this. Just run the file you want and follow the instructions.

Step 2

Let's look at an example of how to work with this useful tool. Start Visual Studio, create a new project. A new item has been added to the Tools -> Tools menu: PInvoker. Click on it and the PInvoker Addin settings window will open. Select the language of your project Language: C # or VB. NET. In the Profiles drop-down list, select edit profiles.

Editing PInvoker Import Profiles
Editing PInvoker Import Profiles

Step 3

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

Image
Image

Step 4

Now you need to add the header files. You can drag and drop them onto the PInvoker Profiles Wizard window. Next, specify the directory where these files are stored.

Adding header files
Adding header files

Step 5

The next step is to specify the dynamic library itself. You can also safely drag it with the mouse to the window of the PInvoker profile wizard.

Adding a dynamic DLL
Adding a dynamic DLL

Step 6

After clicking on the Finish button, the process of importing definitions from the selected dynamic library will begin. The successful completion of the operation will be indicated by the inscription Import successful and the list of names of functions from the DLL that appears in the left field.

Completing Configuring a PInvoke Import Profile from DLLs
Completing Configuring a PInvoke Import Profile from DLLs

Step 7

Now you can easily import PInvoke definitions into your project. In the left field (1), in the Type field, select the type: function, procedure, structure, enumeration, constant, delegate, etc. In the Name field, select the name of the required structure. By a single click, its description will appear in the central field of the PInvoker window (2). Double - will open its definition in the header file. Clicking the Insert definition button (3) will insert the definition into your C # / VB. NET project. Now you can use the imported DLL function in your project.

Recommended: