是否有可能调用从C#.NET的C函数 [英] Is it possible to call a C function from C#.Net

查看:81
本文介绍了是否有可能调用从C#.NET的C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C lib和想调用函数从C#应用程序这个库。我试图加入的C库文件作为连接器的输入,并添加源文件作为额外的依赖建立在C lib目录C ++ / CLI包装。

有没有什么更好的方法来实现这一目标是不知道如何到C输出添加到C#应用程序。

我的C code -

  __ declspec(dllexport)的无符号长ConnectSession(无符号长手柄,
                            无符号字符*公钥,
                            unsigned char型publicKeyLen);

我的CPP包装 -

 长MyClass的:: ConnectSessionWrapper(无符号长手柄,
                                无符号字符*公钥,
                                unsigned char型publicKeyLen)
    {
        返回ConnectSession(手柄,公钥,publicKeyLen);
    }


解决方案

的例子将是,的Linux 的:

1)创建, libtest.c A C 文件与此内容:

 的#include<&stdio.h中GT;无效打印(为const char *消息)
{
  的printf(%s的\\\\ N,消息);
}

这是一个简单的伪包装器的printf。但再presents你要调用的库中的任何 C 功能。如果你有一个 C ++ 函数不要忘了把的extern C 来避免重整的名称。

2)创建 C#文件

 使用系统;使用System.Runtime.InteropServices;公共类测试仪
{
        函数[DllImport(libtest.so,入口点=打印)        静态外部无效打印(字符串消息);        公共静态无效的主要(字串[] args)
        {                打印(的Hello World C#=> C ++);
        }
}

3)除非你有像/ usr / lib目录,你可能会看到一个System.DllNotFoundException,要解决这个问题,您可以将您的libtest.so到/ usr /标准库路径库libtest.so lib下,或者更好,只需添加您的CWD到库路径:出口LD_LIBRARY_PATH = PWD

这里

修改

有关的窗口的,这没有太大的不同。
以从一个例子<一个href=\"http://www.$c$cproject.com/Articles/6912/Calling-methods-from-Dll-compiled-in-C-from-C\">here,你只需要在哟你的 *。cpp的包围的externC文件的方法
类似

 的externC
{
//注:必须使用__declspec(dllexport)的制作(出口)方法为公开
      __declspec(dllexport)的无效​​DoSomethingInC(无符号短整型ExampleParam,unsigned char型AnotherExampleParam)
      {
            的printf(,ExampleParam,AnotherExampleParam你叫方法DoSomethingInC(),您可以在%D和%C \\ n \\ r通过了);
      }
} //结束'的externC'以prevent名称重整

然后,编译,并在C#文件做

 函数[DllImport(C_DLL_with_Csharp.dll,入口点=DoSomethingInC)]公共静态外部无效DoSomethingInC(USHORT ExampleParam,焦炭AnotherExampleParam);

,然后只使用它:

 使用系统;    使用System.Runtime.InteropServices;    公共类测试仪
    {
            函数[DllImport(C_DLL_with_Csharp.dll,入口点=DoSomethingInC)]    公共静态外部无效DoSomethingInC(USHORT ExampleParam,焦炭AnotherExampleParam);            公共静态无效的主要(字串[] args)
            {
                    USHORT VAR1 = 2;
                    焦炭VAR2 ='';
                    DoSomethingInC(VAR1,VAR2);
            }
    }

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);
    }

解决方案

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);
            }
    }

这篇关于是否有可能调用从C#.NET的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆