Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call a C function from C#.Net

Tags:

c

interop

c#-4.0

I have a C lib and want to call function in this library from C# application. I tried creating a C++/CLI wrapper on the C lib by adding the C lib file as linker input and adding the source files as additional dependencies.

Is there any better way to achieve this as am not sure how to add C output to c# application.

My C Code -

__declspec(dllexport) unsigned long ConnectSession(unsigned long handle,                             unsigned char * publicKey,                             unsigned char   publicKeyLen); 

My CPP Wrapper -

long MyClass::ConnectSessionWrapper(unsigned long handle,                                 unsigned char * publicKey,                                 unsigned char   publicKeyLen)     {         return ConnectSession(handle, publicKey, publicKeyLen);     } 
like image 229
Chinjoo Avatar asked Jul 11 '12 03:07

Chinjoo


People also ask

Can a function call a function in C?

The main function always acts as a driver function and calls other functions. We can also write function call as a parameter to function. In the below code, first add(num1, num2) is evaluated, let the result of this be r1. The add(r1, num3) is evaluated.

Can I use C in C#?

You can directly call C functions from C# by using P/Invoke. Here's a short how-to on creating a C# lbrary that wraps around a C dll. Now in the Linker branch, go to General, and change Output File to: "$(SolutionDir)Wrapper\$(ProjectName). dll", this will copy the built C DLL to the C# project root.

Can you call a function from a different file in C?

You can include the . c files, no problem with it logically, but according to the standard to hide the implementation of the function but to provide the binaries, headers and source files techniques are used, where the headers are used to define the function signatures where as the source files have the implementation.

Can C functions call themselves?

In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. The C programming language supports recursion, i.e., a function to call itself.


1 Answers

The example will be, for Linux:

1) Create a C file, libtest.c with this content:

#include <stdio.h>  void print(const char *message) {   printf("%s\\n", message); } 

That’s a simple pseudo-wrapper for printf. But represents any C function in the library you want to call. If you have a C++ function don’t forget to put extern C to avoid mangling the name.

2) create the C# file

using System;  using System.Runtime.InteropServices;  public class Tester {         [DllImport("libtest.so", EntryPoint="print")]          static extern void print(string message);          public static void Main(string[] args)         {                  print("Hello World C# => C++");         } } 

3) Unless you have the library libtest.so in a standard library path like “/usr/lib”, you are likely to see a System.DllNotFoundException, to fix this you can move your libtest.so to /usr/lib, or better yet, just add your CWD to the library path: export LD_LIBRARY_PATH=pwd

credits from here

EDIT

For Windows, it's not much different. Taking an example from here, you only have yo enclose in your *.cpp file your method with extern "C" Something like

extern "C" { //Note: must use __declspec(dllexport) to make (export) methods as 'public'       __declspec(dllexport) void DoSomethingInC(unsigned short int ExampleParam, unsigned char AnotherExampleParam)       {             printf("You called method DoSomethingInC(), You passed in %d and %c\n\r", ExampleParam, AnotherExampleParam);       } }//End 'extern "C"' to prevent name mangling 

then, compile, and in your C# file do

[DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]  public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam); 

and then just use it:

using System;      using System.Runtime.InteropServices;      public class Tester     {             [DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")]      public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam);              public static void Main(string[] args)             {                     ushort var1 = 2;                     char var2 = '';                       DoSomethingInC(var1, var2);             }     } 
like image 152
Gonzalo.- Avatar answered Nov 16 '22 02:11

Gonzalo.-