DLLImport中的C#错误查找方法
问题描述:
我有一个使用DLLImport导入的C++程序集。DLLImport中的C#错误查找方法
我试图调用它的方法:
namespace Testing
{
class Test{
int Run(char* filePath, bool bEntry, double duration){//code}
};
}
通过
[DllImport(dllName, CharSet = CharSet.Auto)]
public static extern int Run(string filePath, bool bEntry, double duration)
);
当我调用它的方法,我得到错误信息:
无法找到切入点命名为Run in dll
答
“运行”看起来是一种非静态的类方法。尽管可以从C#中调用这些方法,但这不是主要的用例。这将是比较容易的方式,从.NET使用它,如果你通过COM揭露它,或者,至少作为一个普通的C接口:
extern "C" __declspec(dllexport) void* Testing_Test_Create();
extern "C" __declspec(dllexport) void Testing_Test_Destroy(void* self);
extern "C" __declspec(dllexport) int Testing_Test_Run(void* self, char* filePath, bool bEntry, double duration);
这里是一个示例如何调用C++从C#类方法:
// Test.cpp in NativeLib.dll
namespace Testing
{
class __declspec(dllexport) Test
{
public:
explicit Test(int data)
: data(data)
{
}
int Run(char const * path)
{
return this->data + strlen(path);
}
private:
int data;
};
}
// Program.cs in CSharpClient.exe
class Program
{
[DllImport(
"NativeLib.dll",
EntryPoint = "[email protected]@@[email protected]@Z",
CallingConvention = CallingConvention.ThisCall,
CharSet = CharSet.Ansi)]
public static extern void TestingTestCtor(IntPtr self, int data);
[DllImport(
"NativeLib.dll",
EntryPoint = "[email protected]@[email protected]@[email protected]",
CallingConvention = CallingConvention.ThisCall,
CharSet = CharSet.Ansi)]
public static extern int TestingTestRun(IntPtr self, string path);
static void Main(string[] args)
{
var test = Marshal.AllocCoTaskMem(4);
TestingTestCtor(test, 10);
var result = TestingTestRun(test, "path");
Console.WriteLine(result);
Marshal.FreeCoTaskMem(test);
}
}
入口点的名称可能是您的构建配置/编译器不同,所以使用DUMPBIN实用程序来获取它们。再次,这只是一个概念证明,在真实代码中使用COM会更好。
答
我不知道这会帮助,如果该函数是一个类的成员,而是要找到名字的切入点,没有顺序,你需要在你的DLL .def文件..
LIBRARY mylib
Run @1
什么样的dll?用C写的? – 2011-02-03 10:41:36
我猜这不是一个COM DLL。你确定DLL中的Run函数被导出吗? – 2011-02-03 10:43:50