PsyWhat
PsyWhat

Reputation: 55

Combining C and C#. Structure's

I need a help, I have a C structures:

struct Connection 
{
    int From;
    int To;
    double Weight;
};

struct Node
{
    int ID;
    double Value;
};

struct ListElement
{
    ListElement *next;
    void *element;
};

struct NeuroNet
{
    ListElement *nodes;
    ListElement *connections;
    int inputs;
    int outputs;
};

with some functional(like a passes and that kind of things). (the whole ide of that thing is to improve a single thread performance)

And I need to use this neuro net functional with C# code but here come the problem: Ill teach this neuro net(change the weight's) via C#, and i would like to directly change the field's of Connection structure, and use a foreach kind of passage, sample:

foreach(var c in WrapperClassName.GetConnections()) {
... 
    c.Weight = c.Weight + 0.3 / someCSvariable;
...}

or

WrapperClassName.ForEachConnection(x => { 
...
    x.Weight = x.Weight + 0.3 / someCSvariable;
... });

so, how do i do that?

And Is there any way to cast C pointer on a struct to C# class (since it is ref var)?

Upvotes: 1

Views: 212

Answers (2)

AQuirky
AQuirky

Reputation: 5236

It is not possible to cast a C pointer to a C# reference since the C object will be in unmanaged memory and the C# object will be in managed memory.

I think the best way to achieve your goal is to use VC++ which has the unique ability to mix unmanaged and managed code in the same assembly. So you can pass your unmanaged C struct to a managed C++ method and use C#-like syntax. Managed VC++ methods can call or be called by C# so if you really want to do things in C# this might be a way.

There are other ways to connect C and C# of course, for example you can use DllImport to connect C# to a C function. When you call the function however the arguments must be transferred to/from unmanaged memory using a facility called marshalling. Marshalling is straightforward for common types such as int, double, and even struct, but is much more complicated for structs where you need to consider alignment...the trouble you need to go through to make this work is simply not worth it in my view.

So my proposal is to give VC++ a shot.

Upvotes: 1

WellerEE
WellerEE

Reputation: 338

It seems like you are trying to pass a Linked list from C# into C. This means that you need to use the Interop services. Take a look at the StructLayoutAttribute class. Also you will need to define the pointers using IntPtr.

Here is an example of using it for one of your structures:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Connection 
{
   public Int32 From;
   public Int32 To;
   public Double Weight;
};

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct ListElement{ 
  public IntPtr next;
  public IntPtr element; 
};

The way I like to find answers to my interop questions is to look up similar structures that have already been defined. I typically use pinvoke.net to find them. Here is an example for IP_ADDR_STRING which is a linked list of IP addresses.

Upvotes: 1

Related Questions