问题描述
这一个是基本的,我该如何调用该函数SubscribeNewsFeed从一个C#的DllImport以下?
类LogAppender:公共L_Append
{
上市:
LogAppender()
:向outfile(TestLog.txt,标准::内部监督办公室:: TRUNC |性病::内部监督办公室::出来)
,feedSubscribed(假)
{
outfile.setf(0,标准::内部监督办公室:: floatfield);
。OUTFILE precision(4);
}
无效SubscribeNewsFeed()
{
someOtherCalls();
}
};
我无法弄清楚如何在这里我的C#程序使用的DllImport时,包括类名:
类节目
{
[的DllImport(LogAppender.dll)]
公共静态外部无效SubscribeNewsFeed();
静态无效的主要(字串[] args)
{
SubscribeNewsFeed();
}
}
的PInvoke不能用于直接调用这样一个C ++函数。相反,你需要定义一个的externC
函数调用的PInvoke功能的PInvoke进入该功能。另外,你不能进入的PInvoke类的实例方法。
C / C ++ code
的externC无效SubscribeNewsFeedHelper(){
LogAppender附加器;
appender.SubscribeNewsFeed();
}
C#
[的DllImport(LogAppender.dll)
公共静态外部无效SubscribeNewsFeedHelper();
This one is basic, how do I call the function SubscribeNewsFeed in the following from a C# DllImport?
class LogAppender : public L_Append
{
public:
LogAppender()
: outfile("TestLog.txt", std::ios::trunc | std::ios::out)
, feedSubscribed(false)
{
outfile.setf(0, std::ios::floatfield);
outfile.precision(4);
}
void SubscribeNewsFeed()
{
someOtherCalls();
}
};
I'm unable to figure out how to include the class name when using the DllImport in my C# program here:
class Program
{
[DllImport("LogAppender.dll")]
public static extern void SubscribeNewsFeed();
static void Main(string[] args)
{
SubscribeNewsFeed();
}
}
PInvoke cannot be used to call directly into a C++ function in this way. Instead you need to define an extern "C"
function which calls the PInvoke function and PInvoke into that function. Additionally you cannot PInvoke into a class instance method.
C / C++ Code
extern "C" void SubscribeNewsFeedHelper() {
LogAppender appender;
appender.SubscribeNewsFeed();
}
C#
[DllImport("LogAppender.dll")]
public static extern void SubscribeNewsFeedHelper();
这篇关于调用使用的DllImport C ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!