Loading and using an unmanaged DLL

Hi all

Im trying to load a dll programmed in C, but I'm not sure exactly how to go about it. I read the following article:
http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

My problem is that Im not quite sure how to handle the return types. In the header file I several type definitions. Below you see some of the type definitions and two of the methods availeble in the DLL.

/********************** H FILE TOP *************************/

#ifdefined (OMA_LIB_INTERNAL)
# define OMA_DLLEXPORT DLLEXPORT

#else
# define
OMA_DLLEXPORT DLLIMPORT
#endif

/****** Constants *******/
#define OmaC_Ov_Default_Report_Name""
#define OmaC_Ov_Global_Object_Path""
#define OmaC_Ov_Unspec_Str"unspecified"

/****** Type Definitions *******/
typedefstruct OmaT_Ov_Writer OmaT_Ov_Writer;
typedefstruct OmaT_Ov_Reader OmaT_Ov_Reader;
typedefstruct OmaT_Ov_Vector OmaT_Ov_Vector;
typedefstruct OmaT_Ov_Reader_Iterator OmaT_Ov_Reader_Iterator;
......

/******************** FUNCTIONS *************************/

OMA_DLLEXPORT OmaT_Ov_Reader *
Oma_Ov_Reader_Open (
constchar *ov_file_name);

OMA_DLLEXPORT OmaT_Ov_Vector *
Oma_Ov_Reader_Vector_Get (OmaT_Ov_Reader *ov_reader_ptr,
constchar *report_name,constchar *obj_path,
constchar *group_and_stat_name);
......

My question is: How I use import this DLL?

At the moment I have an idea that it should be something like the following, but I have no clue how to define the structs which Im using and which gets returned. See below:

publicclassOMAInterface

{

// I guess I have to make a C# struct to get the OmaT_Ov_Vector

[StructLayout(LayoutKind.Sequential)]

publicstructOmaT_Ov_Vector

{

// ? how would I go about creating a struct for this type?

}

// The same accounts for the OmaT_Ov_Reader which is used every time the ov file is read

[StructLayout(LayoutKind.Sequential)]

publicstructOmaT_Ov_Reader

{

// again I dont know how to construct this

}

// Conversion of types is not considered yet

[DllImport("opoma_opt.dll")]

publicstaticexternOmaT_Ov_Reader* Oma_Ov_Reader_Open (constchar *ov_file_name);

{}

// Conversion of types is not considered yet

[DllImport("opoma_opt.dll")]

publicstaticexternOmaT_Ov_Vector* Oma_Ov_Reader_Vector_Get (OmaT_Ov_Reader *ov_reader_ptr,constchar *report_name,constchar *obj_path,constchar *group_and_stat_name);

{}

}

Any help is much appreciated

Christian

[8929 byte] By [Christian] at [2007-12-24]
# 1

here is an excilent post, at the bottom is I think exacly what you are looking for

http://www.codeproject.com/dotnet/PInvoke.asp

Archimagus at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

Hello,

I think you should use managed C++ here.
You could either write everything in managed c++ or only the componant interacting with the natice C code, or write a wrapper (calling your C function) on the top of those function and types to use them in your C# program.

Richard

Richard78 at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Is there any advantages with writing it in C++? and my big problem is how to sort out what the structs declared in the header contain.

/C

Christian at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

The biggest advantage is that you can mix native code and managed code (.NET code), it means that you can use componants written in C#, VB.NET or any other .NET language together with C or C++ code.

I would recommand you to write the componants interacting with native code in Managed C++ (= C++ /CLI), managed C++ is just another .NET language, you can create Forms etc....
For the componants that do not depend on native API use the language you want.

"dllImport" is ok for simple functions returning basic types.

Richard

Richard78 at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...
# 5

The DLL which Im trying to import is not something which I have made. So there I just want to use it. Moreover I have found oUt that the types(structs) used are not defined anywhere, which means that I cant make a wrapper class for the DLL, until I have those informations. This I have to get from the company which made the DLL... still waiting....

/C

Ps. Thanks for the good advice and a great article, especially the type conversion table is good...

Christian at 2007-8-31 > top of Msdn Tech,Visual C#,Visual C# General...