Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use C lib in .NET Core 2.0? #756

Closed
LGinC opened this issue Jul 18, 2017 · 6 comments
Closed

How to use C lib in .NET Core 2.0? #756

LGinC opened this issue Jul 18, 2017 · 6 comments

Comments

@LGinC
Copy link

LGinC commented Jul 18, 2017

system: Ubuntu 14.04 x64
I have a Header file(.h) and a share object(.so), how can I use it in my .net core app.

@LGinC
Copy link
Author

LGinC commented Jul 18, 2017

or How to complete Inter-Process Communication

@Petermarcu
Copy link
Member

@yizhang82 to point to docs on interop with native binaries and IPC.

@mellinoe
Copy link

mellinoe commented Jul 18, 2017

I have a Header file(.h) and a share object(.so), how can I use it in my .net core app.

.NET Core supports PInvoke, just like the .NET Framework. Define "static extern" functions with a compatible signature in your C# code.

Native function:

extern "C" int32_t MyNativeFunction(int32_t x, int32_t y, int32_t z)
{
    return x + y + z;
}

C#:

[DllImport("mysharedobject")]
public static extern int MyNativeFunction(int x, int y, int z);

(for example)

or How to complete Inter-Process Communication

There are a variety of ways to do IPC; the same options exist in .NET as in other programming languages. Pipes or Sockets are two main options.

@LGinC
Copy link
Author

LGinC commented Jul 26, 2017

@mellinoe oh....struct, for example, I define a struct named "struct CameraInfo" in server.h, how can I use it,
and I communicate with another process by shared memory....how to do it

@LGinC
Copy link
Author

LGinC commented Aug 1, 2017

Only serialize to JSON or XML?

@mellinoe
Copy link

mellinoe commented Aug 1, 2017

If you want to directly share objects in memory, you need to be very careful about the physical layout of your structures. This means that you should use the appropriate fixed-size types, packing directives, etc. on the native side to ensure that their shape is well-defined and predictable. On the C# side, you need to match the structure shape exactly, using the same-sized shapes, as well as LayoutKind.Sequential or LayoutKind.Explicit in a StructLayoutAttribute.

Sharing memory like this is pretty fragile, though. Usually you would want to create a well-defined interface between your managed and native layers, and perform explicit calls between them:

C#:

[DllImport("mysharedobject")]
public static extern void GetCameraInfo(out CameraInfo cameraInfo);

Native:

extern "C" void GetCameraInfo(CameraInfo* pCameraInfo) { return whatever; }

With the above, you still need to match the shape of the CameraInfo structure.

This is all very hand-wavy. This is a broad topic, and there are many approaches that can be taken. Given that you are talking about a "CameraInfo" structure, I'm going to say that you should avoid serializing and deserializing large amounts of state through JSON or XML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants