473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can i pass structure into c function that expect pointer

hi,
i have couple of qeustions:

1. I have built a interop of c dll. In this dll i have a function that
expect a pointer to a struct.
I want to pass a struct from my c# interop into this c function. how
can I change my c# struct into a pointer that i could send to the c
function?

2. how can i send a null to this function that ecpect pointer to
struct?

Appreciate help,
Guy

May 6 '07 #1
2 2994
I had a similar problem: I wanted to access the GNU MP library (infinite
precision arithmetic, http://gmplib.org/, ) via a C# class. To solve the
problem, I used VS C++. This allows you to create a .NET class that can be
accessed by C#, but that can contain pointers to structures.

The header file for GMP is gmp.h and contains the various C++ data types.
This
is the only GMP source you need (a dll for Visual C++ is available, see
below).

I defined a .h header file that included gmp.h and that also defined a "C++"
class, GMP. Some of the code therein looks like:
using namespace System;
using namespace System::Runtime::InteropServices;
#include "gmp.h"

typedef __mpz_struct * mpzPtr_t;
#define OTHER_MPZ(obj) ((mpzPtr_t)(obj->mpzPtr))
#define THIS_MPZ ((mpzPtr_t)(mpzPtr))

namespace gmpX {

public class GMP
{
public:
[DllImport("gmp.dll")]
static void mpz_add(mpz_t addIn, mpz_t add1, mpz_t add2);

[DllImport("gmp.dll")]
static void mpz_addmul_ui(mpz_t addIn, mpz_t mult1, unsigned long int
mult2);
...... and many more functions, corresponding to the C functions in gmp.h.
These do the multiple precision arithmetic.

I then defined a GmpInt class to be accessed via C# as:

public ref class GmpInt: IDisposable
{
private:
unsigned long mpzPtr;
public:
GmpInt();
GmpInt(unsigned int initVal);
GmpInt(int initVal);
~GmpInt();

double asDouble();

String ^ asString();

int asInt();

void add(GmpInt^ add1, GmpInt^ add2);

... and more functions corr. to the functions I needed. This class
represents an integer that can have as many digits as required. Note the
use of GmpInt^ to define the input. This allows C++ to look at the objects
as "pointers" to the corresponding C# object. Note the mpzPtr defined as an
unsigned long. This is a pointer to the C++ structure that GMP passes
around to do the arithmetic.

You define the functions for class GmpInt are defined in a .cpp file thus:
#include <malloc.h>
#include "stdafx.h"

#include "gmpX.h"
using namespace gmpX;

//3 constructors.
GmpInt::GmpInt()
{
mpzPtr_t bitPtr = new __mpz_struct;
mpzPtr = (unsigned long)bitPtr;
GMP::mpz_init(bitPtr);
}

GmpInt::GmpInt(unsigned int iVal)
{
mpzPtr_t bitPtr = new __mpz_struct;
mpzPtr = (unsigned long)bitPtr;
GMP::mpz_init_set_ui(bitPtr, iVal);
}

GmpInt::GmpInt(int iVal)
{
mpzPtr_t bitPtr = new __mpz_struct;
mpzPtr = (unsigned long)bitPtr;
GMP::mpz_init_set_si(bitPtr, iVal);
}

GmpInt::~GmpInt() //remember: this destructor is called upon Dispose
{
GMP::mpz_clear((mpz_ptr)mpzPtr);
delete (mpzPtr_t)mpzPtr;
}
void GmpInt::add(GmpInt^ add1, GmpInt^ add2) //add to big ints.
{
GMP::mpz_add(THIS_MPZ, OTHER_MPZ(add1), OTHER_MPZ(add2));
}

void GmpInt::addMultiply(GmpInt^ mult1, unsigned int mult2)
{
GMP::mpz_addmul_ui(THIS_MPZ, OTHER_MPZ(mult1), mult2);
}

..... and other functions similarly implemented by
calling the C++ function in the dll.

The GMP C dll for VS is available on the net, precompiled and ready to go:
http://www.cs.nyu.edu/exact/core/gmp/
If you do not have VS for C++, you can download a free "Express" version
from MS:
http://msdn.microsoft.com/vstudio/express/visualc/



<gu**********@googlemail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...
hi,
i have couple of qeustions:

1. I have built a interop of c dll. In this dll i have a function that
expect a pointer to a struct.
I want to pass a struct from my c# interop into this c function. how
can I change my c# struct into a pointer that i could send to the c
function?

2. how can i send a null to this function that ecpect pointer to
struct?

Appreciate help,
Guy

May 6 '07 #2
On 6 May 2007 02:37:03 -0700, gu**********@googlemail.com wrote:
>hi,
i have couple of qeustions:

1. I have built a interop of c dll. In this dll i have a function that
expect a pointer to a struct.
I want to pass a struct from my c# interop into this c function. how
can I change my c# struct into a pointer that i could send to the c
function?
Have you tried unsafe and fixed?

myData thisStuff = new myData();
unsafe {
fixed (myData* ptr = &thisStuff) {
c_library_function(ptr);
}
}
>
2. how can i send a null to this function that ecpect pointer to
struct?
This compiles - you don't need the fixed statement because null is
inherently fixed as it does not live on the managed heap:

unsafe {
int* voidPtr = null;
c_library_function(voidPtr);
}

rossum

>
Appreciate help,
Guy
May 6 '07 #3

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

Similar topics

11
by: lokb | last post by:
Hi, I have a structure which and defined a smart pointer to the structure. /* Structure of Begin Document Index Record */ typedef struct BDI_Struct{ unsigned char rname; unsigned short int...
8
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
0
by: brckcc | last post by:
I have a function in C which takes a pointer to a pointer to a structure. It then returns a linked list. How do I call, via P/Invoke, from C# to C. struct LinkedList { char *value; struct...
0
by: Pravin | last post by:
I have following architecture. c# code --> managed c++ code (wrapper) -> c wrapper code to third party tool --> third party tool written in c. c# code sends some data and a delegate to c++...
2
by: prakashgkhaire | last post by:
i have two structure where first structure consists val and structure pointer(linklist), 2nd structure consists, val, a varialbe of first structure, pointer of structure. Now i found to pass the...
12
by: Fred | last post by:
Is it possible to create a function which will take any number of structures as an argument? For example, what if I wanted depending on the circumstances to pass a structure named astruct, or...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.