是否可以从 C#.Net 调用 C 函数 [英] Is it possible to call a C function from C#.Net
问题描述
我有一个 C 库,想从 C# 应用程序调用这个库中的函数.我尝试通过将 C lib 文件添加为链接器输入并将源文件添加为附加依赖项,在 C lib 上创建 C++/CLI 包装器.
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.
有没有更好的方法来实现这一点,因为我不确定如何将 C 输出添加到 C# 应用程序中.
Is there any better way to achieve this as am not sure how to add C output to c# application.
我的 C 代码 -
__declspec(dllexport) unsigned long ConnectSession(unsigned long handle,
unsigned char * publicKey,
unsigned char publicKeyLen);
我的 CPP 包装器 -
My CPP Wrapper -
long MyClass::ConnectSessionWrapper(unsigned long handle,
unsigned char * publicKey,
unsigned char publicKeyLen)
{
return ConnectSession(handle, publicKey, publicKeyLen);
}
推荐答案
示例如下,对于 Linux:
1) 使用以下内容创建一个 C
文件,libtest.c
:
1) Create a C
file, libtest.c
with this content:
#include <stdio.h>
void print(const char *message)
{
printf("%s\n", message);
}
这是一个简单的 printf 伪包装器.但代表您要调用的库中的任何 C
函数.如果您有 C++
函数,请不要忘记放置 extern C
以避免修改名称.
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) 创建C#
文件
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) 除非你在像/usr/lib"这样的标准库路径中有库 libtest.so,否则你很可能会看到 System.DllNotFoundException,要解决这个问题,你可以将 libtest.so 移动到/usr/lib,或者更好的是,只需将您的 CWD 添加到库路径:export LD_LIBRARY_PATH=pwd
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
学分来自这里
编辑
对于Windows,没有太大的不同.以此处为例,您只需把你的方法用 extern "C"
包含在你的 *.cpp
文件中类似的东西
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
", ExampleParam, AnotherExampleParam);
}
}//End 'extern "C"' to prevent name mangling
然后,编译,并在你的 C# 文件中做
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);
然后使用它:
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);
}
}
这篇关于是否可以从 C#.Net 调用 C 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!