怎样设置C ++函数,以便它可以被P使用/调用? [英] How to set up a C++ function so that it can be used by p/invoke?
问题描述
希望这是一个brainlessly简单的问题,但它表明我缺乏与C ++的专业知识。我是一个C#程序员,我已经做了的P / Invoke大量的工作在过去与其他人的C ++ / C的DLL。不过,这一次我决定写一个包装C ++ DLL(非托管)我自己,我然后调用从C#我的包装的DLL。
Hopefully this is a brainlessly easy question, but it shows my lack of expertise with C++. I'm a C# programmer, and I've done extensive work with P/Invoke in the past with other people's C++/C dlls. However, this time I've decided to write a wrapper C++ dll (unmanaged) myself, and am then calling my wrapper dll from C#.
我立即运行问题到的是,我无法定义可以被p找到一个C ++函数/调用。我不知道这个语法是什么,但这里是我试图至今:
The problem I am immediately running into is that I am unable to define a C++ function that can be found by p/invoke. I don't know what the syntax for this is, but here's what I'm trying so far:
extern bool __cdecl TestFunc()
{
return true;
}
本来我只是有这个,但它没有工作,要么
Originally I simply had this, but it did not work either:
bool TestFunc()
{
return true;
}
,然后在C#的一面,我有:
And then on the C# side, I have:
public const string InterfaceLibrary = @"Plugins\TestDLL.dll";
[DllImport( InterfaceLibrary, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "TestFunc" ), SuppressUnmanagedCodeSecurity]
internal static extern bool TestFunc();
一切编译,但是当我执行此C#P / Invoke调用,我得到一个System.EntryPointNotFoundException:无法找到DLL'Plugins\TestDLL.dll名为'TestFunc的切入点。
Everything compiles, but when I execute this C# p/invoke call, I get a System.EntryPointNotFoundException: Unable to find an entry point named 'TestFunc' in DLL 'Plugins\TestDLL.dll'.
当然,这必须是一些非常简单的C ++结束,我只是不知道的语法。
Surely this must be something incredibly simple on the C++ end that I just don't know the syntax for.
推荐答案
您需要使用的externC
以及 __ declspec(出口)
,就像这样:
You'll want to use extern "C"
as well as __declspec(export)
, like so:
extern "C" _declspec(dllexport) bool TestFunc()
{
return true;
}
有关详情,请参阅MSDN的编组类型的。
这篇关于怎样设置C ++函数,以便它可以被P使用/调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!