473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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 ?
Nov 15 '05 #1
2 3335
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Thanks but this typesafety thing will kill me , it is like leaving the
swiss-knife and using a fork instead....

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:#a*************@TK2MSFTNGP12.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: John Phung | last post by:
How do you pass a data structure into a function? I'm using c++ to solve the producer and consumer problem and so far, I cannot find an example of how to do this. So, if anyone can help me, that...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
13
by: frakie | last post by:
Hi 'body, I'm experiencing difficulties on parameter passing. I wrote a library in last months, and now it crashes few times in a month because of errors in parameter passing: using gdb on the...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.