Passing structure to a DLL function that takes void * as parameter...

M

Malkocoglu

I have a legacy DLL that i may have to use from C#
The problem with this DLL is that some functions take void * as arguments
Let me illustrate it in C/C++ first , let's say i have a database table and
i want to insert a row

#pragma pack(push, 8)
struct _EuroWhite{
char Panz[20];
si32 Extra;
char IntDex;
char PirCat;
char HatInd;
};
typedef struct _EuroWhite T_EuroWhite;
#pragma pack(pop)

int DB_Insert(int pi_dbid, void *pi_data, S_db_tab *pi_tab_def); //
prototype of the function that takes void *

// somewhere inside the C++ code...

T_EuroWhite ew;
memset(&ew, 0, sizeof(T_EuroWhite));
strcpy(ew.Panz, "blah"); ew.Extra = 123; ew.IntDex = 'a'; ew.PirCat = 'b';
ew.HatInd = 'c';
int rv = DB_Insert(connection_id, &ew, &ew_tab_def); // returns success
or error_code

When I switch to C# , i have to define the structure like this...
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct T_EuroWhite
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=20)]
public string Panz;
public int Extra;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string IntDex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string PirCat;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1)]
public string HatInd;
}
and my function prototype becomes
[DllImport("lib_database.dll", CharSet=CharSet.Ansi)]
public static extern int DB_Insert(int pi_dbid, void *pi_data, S_db_tab
*pi_tab_def);

// somewhere inside the C++ code...

T_EuroWhite ew = new T_EuroWhite();
// fill in ew structure...
ptr = (void *)&ew;
rv = DB_Insert(dh, ptr, ew_tab_def);

But till now , i could not succeed setting ptr = (void *)&ew;
Or i could not write &ew instead of ptr , compiler gives errors about
casting on both cases...
As the DB_Insert function can take any table structure address as a void*
argument
I cannot lock it to a specific table structure type , is there any way out ?
 
M

Mattias Sjögren

As the DB_Insert function can take any table structure address as a void*
argument
I cannot lock it to a specific table structure type , is there any way out ?

You can use multiple overloaded declarations of the function, one for
each structure type you want to pass in. In this case

[DllImport("lib_database.dll", CharSet=CharSet.Ansi)]
public static extern int DB_Insert(int pi_dbid, ref T_EuroWhite
pi_data, S_db_tab* pi_tab_def);



Mattias
 
M

Malkocoglu

Thanks but this typesafety thing will kill me , it is like leaving the
swiss-knife and using a fork instead....

Mattias Sjögren said:
As the DB_Insert function can take any table structure address as a void*
argument
I cannot lock it to a specific table structure type , is there any way
out ?

You can use multiple overloaded declarations of the function, one for
each structure type you want to pass in. In this case

[DllImport("lib_database.dll", CharSet=CharSet.Ansi)]
public static extern int DB_Insert(int pi_dbid, ref T_EuroWhite
pi_data, S_db_tab* pi_tab_def);


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top