簡體   English   中英

創建C ++ DLL然后在C#中使用它

[英]Creating a C++ DLL and then using it in C#

好的我正在嘗試創建一個C ++ DLL,然后我可以在ac#App中調用和引用它。 我已經使用了很多指南制作了一個簡單的dll,但是當我嘗試在C#應用程序中引用它時,我得到了錯誤

無法加載DLL“SDES.dll”:找不到指定的模塊。

該程序的代碼如下(跟我一起,我將包含所有文件)

//These are the DLL Files.

#ifndef TestDLL_H
#define TestDLL_H

    extern "C"
    {
        // Returns a + b
        __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        __declspec(dllexport) double Divide(double a, double b);
    }

#endif

//.cpp
#include "test.h"

#include <stdexcept>

using namespace std;

    extern double __cdecl Add(double a, double b)
    {
        return a + b;
    }

    extern double __cdecl Subtract(double a, double b)
    {
        return a - b;
    }

    extern double __cdecl Multiply(double a, double b)
    {
        return a * b;
    }

    extern double __cdecl Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }

//C# Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("SDES.dll")]
        public static extern double Add(double a, double b);
        static void Main(string[] args)
        {
            Add(1, 2); //Error here...
        }
    }
}

任何人都知道我的程序中可能缺少什么? 如果我錯過了一些代碼或者您有任何問題,請告訴我。

下載Dependecy Walker並打開您的SDES.dll 檢查是否可以加載所有相關DLL。 如果您發現缺少依賴性,也將該dll放在目標目錄中。

您使用的是64位系統嗎? 如果是,則應將C#和C ++定位到相同的體系結構(32位或64位)。

我剛剛測試了你的功能,結果很好。

    [DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)]
    public static extern double Add(double a, double b); 

    static void Main(string[] args)
    {
        Console.WriteLine(Add(1.0, 3.0));

        Console.ReadLine();
    }

輸出:

4

這就是我在Visual Studio 2010中所做的:

  • 創建一個新的解決方案
  • 創建一個新的C#項目
  • 創建一個新的C ++ - Dll項目(沒有MFC和其他東西)
  • 復制粘貼標題和cpp文件
  • 構建C ++ - Dll
  • 將DLL復制到C#項目的Debug / Release(取決於您使用的是什么)目錄(通常分別為“Solution / CSharpProjectName / bin / Debug /”或“Solution / CSharpProjectName / bin / Release /”)
  • 將此P / Invoke簽名添加到C#文件:

    [DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)] public static extern double Add(double a, double b);

    注意:我必須傳遞參數CallingConvention.Cdecl ,否則我得到一個異常。

  • 運行C#-Project,如上所示

PS:我沒有必要設置架構。 它剛剛起作用。 (我正在使用帶有64位操作系統的x64機器。)

這意味着當它試圖加載DLL時,它找不到它!

http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx

很可能它不在你的C#應用​​程序的同一目錄中。

還有,好時機提一下

http://www.dependencywalker.com/

有助於發現DLL依賴問題。

SDES.dll放到放置C#.exe的文件夾中。

SimulateGameDLL在哪里定義? 我沒有在C / C ++代碼中看到它。

我只編譯了代碼,沒有錯誤。 這是我的命令行。

> cl /LD test.cpp -o SDES.dll
> csc test.cs
> test.exe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM