很抱歉,尽管我试图缩短这段内容,但它仍然很长。
我知道一些C#和C ++,但正在学习CLI。我有一个本地的C函数,执行一些统计计算。它有1或2个嵌入式汇编语言指令 - 它的优点是速度非常快(像从消防龙带喝水一样)。我不会重写它。因此,我试图将其放入dll中,并从C#调用它。因此需要使用C ++ / CLI。
按我的估算,以下代码应该接近工作[尽管可能会出现错误的封送处理]。经过广泛的Stack_Overflow研究 - 顺便说一句,我很享受阅读很多问题并且有一些很棒的答案 - 我已经到达了编译事物的地步。 C#gui完成其工作,并能够为包装器函数组合一个可能有效的参数列表。然后是运行时错误...
类型为'System.EntryPointNotFoundException'的未处理异常在gui_proj.exe中发生
附加信息:无法在DLL 'Wrapper_proj.dll'中找到名为'process_these_files'的入口点。
我已经将代码简化到必要的部分(我认为是)。该dll在gui proj中被引用。对象浏览器似乎看到了入口点。Dependency Walker全部搞砸了 -“IESHIMS.DLL”真的?Silurian Inspect_Exe说它可以加载dll并显示基本属性,但无法显示任何导入/导出。[两个项目都是64位配置]。我在构建之前清理解决方案。
如何获得一个入口点'found'?我需要封送文件名吗?还是封送(空)结果数组?
在gui_proj.cs中:
// I've also tried CallingConvention.StdCall
[DllImport("Wrapper_proj.dll", SetLastError = true, CharSet = CharSet.Ansi, EntryPoint = "process_these_files", CallingConvention = CallingConvention.Cdecl)]
static extern void process_these_files( [In] int length, [In] String[] file_names, [Out] byte[] output_calcs, [Out] UInt64[] file_sizes);
private void Calcs_on_files_ThreadStart(object oo)
{
List<int> fil_indices = new List<int>();
List<string> fil_names = new List<string>();
List<UInt64> fil_sizes = new List<UInt64>();
foreach (DataGridViewRow rr in dgvFiles.SelectedRows)
{
fil_indices.Add(rr.Index);
fil_names.Add( ((string)rr.Cells["Full_Name"].Value) );
fil_sizes.Add( ((UInt64)((long)rr.Cells["File_Size"].Value)) ); // expected size
}
int jj = fil_names.Count;
int kk = 32 * fil_names.Count;
byte[] calc_results = new byte[kk]; // "byte" is equivalent to "uint8_t"
UInt64[] siz_results = new UInt64[jj];
string[] fil_arr = fil_names.ToArray();
process_these_files( jj, fil_arr, calc_results, siz_results);
}
在Wrapper_proj.h文件中:
#include "stdafx.h"
#include <fstream> // std::ifstream
...
// compiler supposedly defines *"_EXPORTS", though just being definitive here
#define Wrapper_proj_EXPORTS
#ifdef Wrapper_proj_EXPORTS
#define Wrapper_proj_API __declspec(dllexport)
#else
#define Wrapper_proj_API __declspec(dllimport)
#endif
namespace Wrapper_proj {
// ++++ see note below
public ref class wrap_it
{
public:
// +++ see note below
static void process_these_files(
int length, // each array has same length (varying sizes though, of
course)
array<System::String^>^ files,
array<System::Byte>^ calcs,
array<System::UInt64>^ fil_sizes);
};
}
在 Wrapper_proj.cpp 文件中
#include "stdafx.h"
#include "Wrapper_proj.h"
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices;
using std::ios;
namespace Wrapper_proj {
void wrap_it::process_these_files(
int arr_length,
array<System::String^>^ files,
array<System::Byte>^ calcs,
array<System::UInt64>^ fil_sizes)
{
...
// call native calc. fcn. (that has asy. lang. instructions)
...
}; // process_these_files
} // end namespace
如果我尝试在wrapper_proj.h中使用以下声明: "static Wrapper_proj_API void process_these_files(" 然后我会得到以下错误信息:
error C3387: 'process_these_files' : __declspec(dllexport)/__declspec(dllimport)不能应用于托管类型的成员 error C3395: 'City_Hash_Lib::City_Hash::process_these_files' : __declspec(dllexport)不能应用于具有__clrcall调用约定的函数
我想保留"public ref Class",但奇怪的是它似乎并没有阻止dllexport。
Dumpbin /EXPORTS显示如下:
Dump of file Wrapper_proj.dll File Type: DLL Summary 3000 .data 2000 .nep 1000 .pdata 27000 .rdata 1000 .reloc 3000 .rsrc A000 .text
创建一个.def文件以导出"process_these_files"会产生错误C3387。
ref class
包装器时,不要使用[DllImport]。只需添加对C++/CLI项目的项目引用,即可直接使用wrap_it类。 - Hans Passant