我對C#完全陌生。我試圖運行一個C#代碼,該代碼使用了一個用C 撰寫的DLL。該DLL檔案是,
#include "pch.h"
#include <vector>/span>
#include <iostream>
extern "C"/span> __declspec(dllexport) void __cdecl analysis(double* values, int len)
{
使用 namespace std;
vector<int> ind(values, values len)。
int n = ind.size()。
for (int i = 0; i < n; i )
{
cout << ind[i] << endl;
}
}
而C#的代碼,
using System.Runtime.InteropServices;
using System.Collections.Generic.Basic.Basic.Basic;
namespaceTest_run
{
class Program {
{
[DllImport("Test.dll")]
public static extern void analysis([MarshalAs(UnmanagedType. LPArray] double[] values, int len)。
static void Main(string[] args)?
{
List<double> lst = new List<double> ()。
lst.Add(1.2)。
lst.Add(2.3);
lst.Add(3.4)。
分析(lst.ToArray(), lst.Count)。
}
}
}
兩個檔案都是單獨編譯的。但是當我運行這個程式時,它出現了例外 "無法加載DLL "Test.dll "或它的一個依賴項。無法找到指定的模塊。(0x8007007E)"。它們都在同一個解決方案中,C#專案中添加了DLL。
uj5u.com熱心網友回復:
僅僅將DLL添加到專案中是不夠的,你需要確保DLL檔案確實被復制到了Executable的輸出目錄中。
如果你在你的C#專案中添加了DLL檔案,你可以設定 "Build Action" => "None",以及 "Copy to Output Directory" => "Copy if newer"。 這應該可以確保你的dll檔案在正確的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/309162.html
標籤:

