Main Site Documentation

Pointer to a struct and interop function


#1

I’m new to .net microframework and I’m trying to port some native code to .netMF using interop. Now my problem is that a native function take as a parameter a pointer to a struct, something like the following:

typedef struct _struct1 {
const char* path;
const char* interfaces;
uint8_t flags;
void* context;
} struct1;

void func(struct1* param1)

But interop functions can only accept parameter in a limited number of types.
So, like I asked, what is the best way to accomplish this? Do I need to serialize my struct or there are others solutions ?


#2

@ Daniele - Your best bet from what limited information I have would be to split up the struct and pass the individual parameters. Something like “void func(string path, string interfaces, byte flags, int context);” instead.

A more involved method is to turn your struct into a managed class and have that function as a member function. Then you can use the helper functions generated in the interop header file of the form T& Get_*(thisObject); You can read and write the managed members using them.

So if you have the C# class:


class struct1 {
  string path;
  string interfaces;
  byte flags;
  int context;

  [MethodImplAttribute(MethodImplOptions.InternalCall)]
  extern void func();
}

In side the native version of func, you can access the members like below:


void struct1::func(CLR_RT_HeapBlock* pMngObj) {
  UINT8& flags = Get_flags(pMngObj);

  flags = 2;
}

After calling func, flags will be set to 2 in the managed side.

You will likely not be able to make a direct port of your existing native code like this without a lot of effort and debugging. The easiest and most supported way when moving to NETMF is to port as much of your app as possible to managed code. Only when you need increased and more deterministic performance should you resort to native interops or our RLP library.


#3

@ Daniele - Welcome to the forum!