473,320 Members | 2,112 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,320 software developers and data experts.

trouble witch using struct from C dll

Hello, I'm a "expert of beginner" in C#.
I have a dll - in C. And in this dll is this struct:
typedef
struct msg_s { /* please make duplicates of strings before next call
to emi_read() ! */

int op_type; /* of "op_t" type: operation type; submit (>0), response
(<0) */

union {

struct {

const char* from;
const char* to;
const char* notif_addr;
int notif_type; /* not_t */
const char* validity; /* DDMMYYHHmm */
const char* timestamp; /* DDMMYYHHmmss */
dst_t deliv_status;
int deliv_reasoncode;
const char* deliv_timestamp; /* DDMMYYHHmmss */
const char* text;
int priority;
int bit8; /* data coding scheme */
int numbits; /* number of bits in TD (text) if bit8=1 */
int msg_class;
int rpid; /* rpid == -1 =use RPID value "0000" */
/* rpid == 0 =do not use RPID value */
const char* xservices; /* extra services */
} submit;
struct {
int ack;
int errcode;
const char* sysmsg;
} response;
} uu;
} msg_t;

How can I use this struct in C#?
How can I make in C# the same code as is in C:

msg_t* msg = 0;
msg = emi_msg( emi); //emi_msg is a function whitch return a pointer
optyp = msg->op_type;
Thanks a lot

Pavel Spaleny

Mar 22 '07 #1
1 2108
Hi Pavel,

I borrowed an example that is originally from http://www.ddj.com/dept/windows/184406285
to get a small example that works

---------------------------------------------------
// C-code
#include <stdio.h>
#include <math.h>

typedef struct { double re,im; } Complex;

__declspec(dllexport) void __stdcall MakeComplex(Complex * C)
{
C = (Complex*) malloc(sizeof(Complex));
C->re = 0.0;
C->im = 0.0;
}

__declspec(dllexport) void __stdcall KillComplex(Complex * c)
{
free(c);
}

__declspec(dllexport) void __stdcall PlayComplex(Complex * C)
{

printf("Re = %f\n", C->re);
printf("Im = %f\n", C->im);
printf("Abs = %f\n", sqrt(C->re*C->re + C->im*C->im));
}
---------------------------------------------------
// C#-code
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct Complex
{
// if these two lines are swapped - we swap(re,im)
public double re;
public double im;
};
public class myComplex
{
[DllImport("cplx")]
public static extern void MakeComplex(out Complex C);

[DllImport("cplx")]
public static extern void KillComplex(ref Complex C);

[DllImport("cplx")]
public static extern void PlayComplex(ref Complex C);
public static void Main()
{
Complex C = new Complex();
C.re = 1.0;
C.im = 2.0;

PlayComplex(ref C);
Console.WriteLine("C = ({0},{1}i)", C.re, C.im);

Complex C2;
MakeComplex(out C2);
Console.WriteLine("first C2 = ({0},{1}i)",C2.re,C2.im);

C2.re = 2.2;
C2.im = -1.2;
Console.WriteLine("then C2 = ({0},{1}i)",C2.re,C2.im);
PlayComplex(ref C2);

KillComplex(ref C2);

// risky
// PlayComplex(ref C2);
}
}
---------------------------------------------------

If the C-dll is compiled into cplx.dll an exe made from the C#-code
should produce:

Re = 1.000000
Im = 2.000000
Abs = 2.236068
C = (1,2i)
first C2 = (0,0i)
then C2 = (2.2,-1.2i)
Re = 2.200000
Im = -1.200000
Abs = 2.505993

First we create a Complex in C# called C. We give it the value (1 +
2i) and send it to the C-level in PlayComplex(ref C)

Then we create a second Complex C2 in C, this is risky I think, since
we create a "pointer" in C# but allocate memory in C. Also we later
Kill the Complex with KillComplex. There is probably no way to tell in
C# that C2 is dead. But since C is C we can call PlayComplex again
(but this is most likely really dangerous).

You have to be careful and take great car in who is allocating memory
an who is not. You can probably not allocate memory in C# with "new"
and then free is in C - even if you can you will most likely end up
getting trouble.

Also GC.KeepAlive(C) might be of interest for you if you use the
complex C in a C-dll for a long time.

Keywords that were important to me the first time i tried this are:
* [StructLayout(LayoutKind.Sequential)]
* [DllImport("cplx")]
* __declspec(dllexport)
* __stdcall

I am not sure I use them in the best way, but this short example works
for me.

Also: if you end up getting a lot of trouble and never really need the
struct in C# you could just use an IntPtr to just remember where it
is. (But that is perhaps also shadowprogramming.)
--

Per Erik Strandberg
..NET Architect - Optimization
Tomlab Optimization Inc.
http://tomopt.com/tomnet/

On Mar 22, 2:26 pm, "yucik...@gmail.com" <yucik...@gmail.comwrote:
Hello, I'm a "expert of beginner" in C#.
I have a dll - in C. And in this dll is this struct:

typedef
struct msg_s { /* please make duplicates of strings before next call
to emi_read() ! */

int op_type; /* of "op_t" type: operation type; submit (>0), response
(<0) */

union {

struct {

const char* from;
const char* to;
const char* notif_addr;
int notif_type; /* not_t */
const char* validity; /* DDMMYYHHmm */
const char* timestamp; /* DDMMYYHHmmss */
dst_t deliv_status;
int deliv_reasoncode;
const char* deliv_timestamp; /* DDMMYYHHmmss */
const char* text;
int priority;
int bit8; /* data coding scheme */
int numbits; /* number of bits in TD (text) if bit8=1 */
int msg_class;
int rpid; /* rpid == -1 =use RPID value "0000" */
/* rpid == 0 =do not use RPID value */
const char* xservices; /* extra services */} submit;

struct {
int ack;
int errcode;
const char* sysmsg;

} response;
} uu;
} msg_t;

How can I use this struct in C#?
How can I make in C# the same code as is in C:

msg_t* msg = 0;
msg = emi_msg( emi); //emi_msg is a function whitch return a pointer
optyp = msg->op_type;

Thanks a lot

Pavel Spaleny

Mar 27 '07 #2

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

Similar topics

1
by: Steven Spear | last post by:
Hi. I am trying to perform backtracking with this search function. Something is going wrong when I enter 2 at the command line. Entering 1 at the command line seems to work fine. I notice that...
8
by: Eric A. Johnson | last post by:
I am using scanf (from stdio.h) and having trouble inputting a date. I want the user to be able to input a date in the format mm/dd/yyyy. I use the following: printf ("Please enter your first...
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
5
by: bg_ie | last post by:
Hi all, I'm trying to use the following kit - http://ccrma.stanford.edu/software/stk/ but I'm having trouble building the code. I've tried isolating the problem by deleting code and...
3
by: John Sasso | last post by:
In my Yacc .y file I defined: %union { int value; struct Symbol Sym; } The Symbol struct I defined in a header file I #included in the Prologue section of the .y file as:
3
by: Michael | last post by:
Hi all, I'm having trouble PInvoking a TCHAR within a struct. I'll paste the specific struct's API definition below. I've tried so many numerous variations. The main Win32 error I get is...
1
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
4
by: Alan | last post by:
I`m having trouble figuring out the correct syntax for accessing an element of a struct variable (t_list) passed by reference to a function. The compiler does not like the code below when I added...
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.