.NET Programming Hints - Importing Native Dynamic Link Libraries

<< Click to Display Table of Contents >>

Navigation:  Programming with Common Vision Blox > Compiler specific hints > .NET languages and CVB Programming Hints > Further .NET Programming Hints >

.NET Programming Hints - Importing Native Dynamic Link Libraries

 

Importing Native Dynamic Link Libraries

If a native DLL of a CVB Tool has NO .NET wrapper you can write your own to access the functions in it.

For all the Image Manager DLLs and some CVB tool DLLs we already provide wrapper DLLs and the Code Documentation *.xml for the Intelli Sense.

A reference how to include the wrapper DLLs in your projects please could be found here.

 

To write your own .NET wrapper requires more work than importing an ActiveX Control. You can create a wrapper class which grants access to the needed DLL.

To write a C# wrapper you need the header file of the DLL to import.

According to the given declarations the functions can be imported and types can be created. The C# wrapper class can be compiled as a DLL and added to a project as described in Importing ActiveX Controls or simply added uncompiled (Add Existing Item in the Solution Explorer).

 

Functions

Generally all imported functions are defined as follows:

 public static extern <Return-Type> <Function-Name> (<Parameters>)

so that the compiler knows that the functions can be accessed freely and that they are defined externally.

 

To show the compiler where to find the function and how to interprete the DLL an attribute has to be used:

 [DllImport("<DLL-Name>", CharSet=System.Runtime.InteropServices.CharSet.Ansi)];

That is all the .NET environment needs to invoke and handle the function.

 

Example:

Here the GetGlobalDirectDrawEnabled function from the CVutilities.dll will be imported.

At first have a look at the function in the header file:

 IMPORT(BOOL) GetGlobalDirectDrawEnabled (BOOL& value);

 

This function returns a BOOL (not a bool, see Data Types and Marshalling) and expects to get a BOOL as reference.

In C# two options for parameters as references are available: ref and out.

We can use out here because the function simply returns the value and does not process the given BOOL.

Thus according to the description above the code should look like this:

 [DllImport("cvcutilities", CharSet=System.Runtime.InteropServices.CharSet.Ansi)]

 public static extern bool GetGlobalDirectDrawEnabled(out bool val);

 

In most cases the importing will be as simple as described here.

Many functions also use custom structs and enums.

Some exceptions from the simple importing process shown here are described in Data Types and Marshalling.

 

Constants, Structs, and Enums

Some functions with more complicated interfaces expect and/or return custom structs.

When creating these structs make sure that their contents have the same order as they have in the header file.

Also the following attribute should be inserted before the structure definition if it contains more than one data type to ensure the data is laid out in memory correctly:

 [StructLayout(LayoutKind.Sequential)]

 

As a rule of thumb return types or parameters which have a range of numbers should be encapsulated in an enum - even if they are defined as constants in the header file.

This guarantees type safety and eases program verification, readability and debugging.