简体   繁体   English

如何在ac#程序中使用C dll

[英]How to use a C dll within a c# program

I have a .c and a .h file modified to be used within a cpp application, in fact they have the 我有一个.c和.h文件被修改为在cpp应用程序中使用,实际上他们有

#ifdef __cplusplus
       extern "C"{
#endif 

prepocessor lines. 预处理线。 I was wondering if and how I can use the functions defined there within ac# program. 我想知道是否以及如何使用ac#program中定义的函数。 Maybe I have to create a dll for that piece of code? 也许我必须为这段代码创建一个DLL?

Here's a simple example taken from MSDN : 这是一个从MSDN获取的简单示例:

using System;
using System.Runtime.InteropServices;

class PlatformInvokeTest
{
    [DllImport("msvcrt.dll")]
    public static extern int puts(string c);
    [DllImport("msvcrt.dll")]
    internal static extern int _flushall();

    public static void Main() 
    {
        puts("Test");
        _flushall();
    }
}

This assumes you are willing to compile your existing code into a .DLL 假设您愿意将现有代码编译为.DLL

You guys don't seem to get it - he doesn't have a dll, he has c++ source files: 你们似乎没有得到它 - 他没有dll,他有c ++源文件:

You have two options 你有两个选择

  1. Translate the c++ code to C# and incorporate it directly into your application 将c ++代码转换为C#并将其直接合并到您的应用程序中

  2. Use a c++ compiler to create a dll from the source files and use PInvoke to access it 使用c ++编译器从源文件创建一个dll,并使用PInvoke访问它

As far as if you need to modify the c++ code to create the dll or make it accessable from C#, we have no way of knowing without a posting of the full code. 如果您需要修改c ++代码来创建dll或使其可以从C#访问,我们无法在不发布完整代码的情况下知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM