1

我在Turbo C中编写了一个小程序,我想获取或创建该程序的 DLL,以便在我的 C# 应用程序中使用它。

那么如何使用 Turbo C 创建 C 程序的 DLL?
我想在 DLL 引用中将它与 C# 或 VB 程序一起使用。

如果找到此链接,但我无法理解。

4

2 回答 2

3

Turbo C(最后一次发布于 1989 年)是一个基于 DOS 的程序。它不能创建 Win32 DLL。

由于您已经在使用 Visual Studio for C#,我强烈建议您将 Visual C++ 用于您的 DLL。Visual C++ 是不言自明的(提示:Win32 DLL 是您想要的项目类型)。

于 2010-07-27T04:21:47.423 回答
3

不要使用 Turbo C 并使用 Visual C++ 编译,因为我们必须使用 Win32 调用约定。假设math.h是你的库。

#include <math.h>

extern "C"
{
    __declspec(dllexport) double __stdcall MyPow(double, double);
}

extern double __stdcall MyPow(double x, double y)
{
    return pow(x, y);
}

然后将其导入您的 C# 应用程序,使用DllImport.

class Program
{
    [DllImport("MyLibrary.dll")]
    extern static double MyPow(double x, double y);

    static void Main(string[] args)
    {
        Console.WriteLine(MyPow(2.0, 5.0));

        Console.ReadKey();
    }
}

这使您的代码极度不受管理。更好的方法是创建托管 C++ 包装器。为此,创建一个新的 Visual C++ 动态库项目,在 Project Properties > Configuration Properties > C/C++ > General 下启用Common Language RunTime Support (OldSyntax)C++ ExceptionsProject Properties > Configuration Properties > C/C++ > Code Generation中禁用。为发布目标构建。

extern "C"
{
    #include <math.h>
}

namespace Wrapper
{
    public __gc class MyClass
    {
        public:
            static double MyPow(double x, double y)
            {
                return pow(x, y);
            }
    };
};

然后创建一个新的 Visual C# 项目,引用我们刚刚创建的 .DLL 文件并在Project Properties > Build中,检查Allow unsafe code您是否在原始库中使用了指针,并且需要在 C# 应用程序中修改它们。

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Wrapper.MyClass.MyPow(2.0, 5.0));

        Console.ReadKey();
    }
}
于 2010-07-27T05:01:51.420 回答