473,386 Members | 1,812 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,386 software developers and data experts.

C struct in dll -- > equivalent C# struct to send to dll?

17
Hi. I've managed to get the C code I need into a dll, and my C# program can access some functions. There are other functions though, and I don't know how to access them from C# because they involve a struct declared in the C code. The example I have been working from is the following:

C-struct:
[PHP]struct STRUCT_DLL
{
int count_int;
int* ints;
};[/PHP]

C# code:
[PHP]class call_dll
{

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct STRUCT_DLL
{
public Int32 count_int;
public IntPtr ints;
}

[DllImport("mingw.dll")]
private static extern int func_dll(int an_int, [MarshalAs(UnmanagedType.LPArray)] byte[] string_filled_in_dll, ref STRUCT_DLL s);

[DllImport("mingw.dll")]
private static extern int func2(int a, int b);

public static void Main()
{
...
}
}[/PHP]

That works fine. Now, if the C-struct is this:
[PHP]struct vector
{
uint elem_count; /* number of items in the vector */
uint size; /* size of the vector */
uint elem_size; /* element size */
int (*cmp)(const void *, const void *);
void *table;
};[/PHP]

I change my C# code to the following:
[PHP] [StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe private struct STRUCT_DLL
{
public uint elem_count;
public uint size;
public uint elem_size;
//int (*cmp)(const void *, const void *);
public void* table;
}[/PHP]

It compiles, but not when the commented line is...not commented. I have no idea how to proceed. I know you can put methods in structs in C#, but when it comes to function pointers, I am lost. Also, how would I DllImport a function from the C dll that is for example "vector *sdMake(int a, char b, ...)" ? I guess any function pointers give me problems.
I would appreciate any help.
Nov 15 '07 #1
0 2686

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
7
by: Dirk Reske | last post by:
Hello, if I have a P/Invoke method: open(STRUCT1 str, STRUCT2 str2); is there a way to pass <null> for a struct in C#? How can I solve this? thx
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
7
by: Alex | last post by:
If I have two struct. See below: struct s1 { int type; int (*destroy)(struct s1* p); } struct s2 { struct s1 base;
9
by: werasm | last post by:
Hi all, What is the difference between: typedef struct { ... } MyS1; ....and...
10
by: Tammy | last post by:
Hello all, I am wondering what is the best way to declare a struct to be used in other c and c++ files. Such as for a C API that will be used by others. 1. Declaring the typedef and the...
1
by: MomoCPP | last post by:
Hello all, I'm attempting to serialize the following: typedef struct { const char* name; const char* layer1; const char* layer2;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.