无法将__delegate指针传递给C ++函数? [英] Cannot pass a __delegate pointer to C++ function?

查看:53
本文介绍了无法将__delegate指针传递给C ++函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将__delegate ptrs传递给DLL中的本机C函数没有问题,

但是当试图将__delegate ptr传递给本机C ++函数时

在DLL中我得到以下运行时异常:未处理的异常

类型''System.EntryPointNotFoundException''。


如果入口点是没找到,我想我会在链接期间得到一个未解决的符号错误。此外,我可以

成功地将void,int *和函数指针等传递给C ++ DLL

函数,但是当尝试传递__delegate时,这个错误仍然存​​在>
指针,用于回调托管C ++函数。


这是相关的代码片段 - DLL代码只包含
$ b $的存根b这里调用的函数:


#include" stdafx.h"

#using< mscorlib.dll>


using namespace System;

using namespace System :: Runtime :: InteropServices;


__delegate void testCB();

[DllImport(" unmdll.dll")]

void fnunmdll_2(testCB * UpdateProgMeter);


int fnunmdll_0(void);

void fnunmdll_1(int * x); // testCB * UpdateProgMeter);


__gc class C1

{

public:

static void handleCB()

{

return;

}

};


in t _tmain()

{

int k = 9;


//总是有效(与C / C ++一样)< br $>
fnunmdll_0();


//总是有效(和C / C ++一样)

fnunmdll_1(& k);


testCB * pCB = new testCB(0,& C1 :: handleCB);


//如果这是一个C ++函数,期待一个函数指针,

出现上述错误。如果这是一个外部的C。功能,一切都好!

fnunmdll_2(pCB);


返回0;

}

听起来很疯狂,我开始认为这不可能用原生C ++函数来实现




谢谢提前,乔治

I have no trouble passing __delegate ptrs to native C functions in DLLs,
however when attempting to pass the __delegate ptr to a native C++ function
in a DLL I get the following runtime exception: An unhandled exception of
type ''System.EntryPointNotFoundException''.

This is confusing b/c if the entry point was not found, I would think I''d
get an unresolved symbol error during linking. Furthermore, I can
successfully pass void, int *, and function pointers, etc to C++ DLL
functions, but this error persists when attempting to pass a __delegate
pointer, which is used for callbacks to managed C++ functions.

Here is the relevant code snippet - the DLL code simply contains stubs of
the functions called here:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate void testCB();
[DllImport("unmdll.dll")]
void fnunmdll_2(testCB *UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int *x);//testCB *UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
return;
}
};

int _tmain()
{
int k = 9;

//always works (as both C/C++)
fnunmdll_0();

//always works (as both C/C++)
fnunmdll_1(&k);

testCB *pCB = new testCB(0, &C1::handleCB);

//if this is a C++ function expecting a function pointer, the
aforementioned error occurs. If this is an extern "C" function, all is ok!
fnunmdll_2(pCB);

return 0;
}

As crazy as it sounds, I''m beginning to think this is not possible to do
with native C++ functions.

Thanks in advance, George

推荐答案

这与代表无关,这是一个C ++名称重整问题,

你的DllImport函数(fnunmdll_2)必须使用C链接导出,或者一个

相应的.DEF文件。

externC

{

....


}


威利。


" ; glutz7878" < GL ******* @ discussions.microsoft.com>在消息中写道

news:42 ********************************** @ microsof t.com ...
This has nothing to do with delegates, this is a C++ name mangling issue,
your DllImport functions (fnunmdll_2) must use C linkage exports, or an
appropriate .DEF file.
extern "C"
{
....

}

Willy.

"glutz7878" <gl*******@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
我将__delegate ptrs传递给DLL中的本机C函数没有问题,但是当试图将__delegate ptr传递给本机C ++
函数时在DLL中我得到以下运行时异常:
类型''System.EntryPointNotFoundException''的未处理异常。

如果入口点不是b / c这会令人困惑发现,我认为在链接过程中我会得到一个未解决的符号错误。此外,我可以成功地将void,int *和函数指针等传递给C ++ DLL
函数,但是当尝试传递__delegate
指针时,此错误仍然存​​在,该指针用于回调管理C ++函数。

这是相关的代码片段 - DLL代码只包含这里调用函数的存根:

#include" stdafx。 h"
#using< mscorlib.dll>

使用命名空间系统;
使用命名空间System :: Runtime :: InteropServices;

__delegate void testCB();
[DllImport(" unmdll.dll")]
void fnunmdll_2(testCB * UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int * x); // testCB * UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
返回;
}
};

int _tmain()
{
int k = 9;
//总是有效(和C / C ++一样)
fnunmdll_0();
//总是有效(和C / C ++一样)
fnunmdll_1(& k);

testCB * pCB = new testCB(0,& C1 :: handleCB);

//如果这是一个期望函数指针的C ++函数,则会发生上述错误。如果这是一个外部的C。功能,一切都好了!
fnunmdll_2(pCB);

返回0;
}

听起来很疯狂,我我开始认为用原生C ++函数做这个是不可能的。

提前谢谢,George
I have no trouble passing __delegate ptrs to native C functions in DLLs,
however when attempting to pass the __delegate ptr to a native C++
function
in a DLL I get the following runtime exception: An unhandled exception of
type ''System.EntryPointNotFoundException''.

This is confusing b/c if the entry point was not found, I would think I''d
get an unresolved symbol error during linking. Furthermore, I can
successfully pass void, int *, and function pointers, etc to C++ DLL
functions, but this error persists when attempting to pass a __delegate
pointer, which is used for callbacks to managed C++ functions.

Here is the relevant code snippet - the DLL code simply contains stubs of
the functions called here:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate void testCB();
[DllImport("unmdll.dll")]
void fnunmdll_2(testCB *UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int *x);//testCB *UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
return;
}
};

int _tmain()
{
int k = 9;

//always works (as both C/C++)
fnunmdll_0();

//always works (as both C/C++)
fnunmdll_1(&k);

testCB *pCB = new testCB(0, &C1::handleCB);

//if this is a C++ function expecting a function pointer, the
aforementioned error occurs. If this is an extern "C" function, all is
ok!
fnunmdll_2(pCB);

return 0;
}

As crazy as it sounds, I''m beginning to think this is not possible to do
with native C++ functions.

Thanks in advance, George



这与代理无关,这是一个C ++名称修改问题,

您的DllImport函数(fnunmdll_2)必须使用C链接导出,或者

适当.DEF文件。

extern" C"

{

....


}


威利。


" glutz7878" < GL ******* @ discussions.microsoft.com>在消息中写道

news:42 ********************************** @ microsof t.com ...
This has nothing to do with delegates, this is a C++ name mangling issue,
your DllImport functions (fnunmdll_2) must use C linkage exports, or an
appropriate .DEF file.
extern "C"
{
....

}

Willy.

"glutz7878" <gl*******@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
我将__delegate ptrs传递给DLL中的本机C函数没有问题,但是当试图将__delegate ptr传递给本机C ++
函数时在DLL中我得到以下运行时异常:
类型''System.EntryPointNotFoundException''的未处理异常。

如果入口点不是b / c这会令人困惑发现,我认为在链接过程中我会得到一个未解决的符号错误。此外,我可以成功地将void,int *和函数指针等传递给C ++ DLL
函数,但是当尝试传递__delegate
指针时,此错误仍然存​​在,该指针用于回调管理C ++函数。

这是相关的代码片段 - DLL代码只包含这里调用函数的存根:

#include" stdafx。 h"
#using< mscorlib.dll>

使用命名空间系统;
使用命名空间System :: Runtime :: InteropServices;

__delegate void testCB();
[DllImport(" unmdll.dll")]
void fnunmdll_2(testCB * UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int * x); // testCB * UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
返回;
}
};

int _tmain()
{
int k = 9;
//总是有效(和C / C ++一样)
fnunmdll_0();
//总是有效(和C / C ++一样)
fnunmdll_1(& k);

testCB * pCB = new testCB(0,& C1 :: handleCB);

//如果这是一个期望函数指针的C ++函数,则会发生上述错误。如果这是一个外部的C。功能,一切都好了!
fnunmdll_2(pCB);

返回0;
}

听起来很疯狂,我我开始认为用原生C ++函数做这个是不可能的。

提前谢谢,George
I have no trouble passing __delegate ptrs to native C functions in DLLs,
however when attempting to pass the __delegate ptr to a native C++
function
in a DLL I get the following runtime exception: An unhandled exception of
type ''System.EntryPointNotFoundException''.

This is confusing b/c if the entry point was not found, I would think I''d
get an unresolved symbol error during linking. Furthermore, I can
successfully pass void, int *, and function pointers, etc to C++ DLL
functions, but this error persists when attempting to pass a __delegate
pointer, which is used for callbacks to managed C++ functions.

Here is the relevant code snippet - the DLL code simply contains stubs of
the functions called here:

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;

__delegate void testCB();
[DllImport("unmdll.dll")]
void fnunmdll_2(testCB *UpdateProgMeter);

int fnunmdll_0(void);
void fnunmdll_1(int *x);//testCB *UpdateProgMeter);

__gc class C1
{
public:
static void handleCB()
{
return;
}
};

int _tmain()
{
int k = 9;

//always works (as both C/C++)
fnunmdll_0();

//always works (as both C/C++)
fnunmdll_1(&k);

testCB *pCB = new testCB(0, &C1::handleCB);

//if this is a C++ function expecting a function pointer, the
aforementioned error occurs. If this is an extern "C" function, all is
ok!
fnunmdll_2(pCB);

return 0;
}

As crazy as it sounds, I''m beginning to think this is not possible to do
with native C++ functions.

Thanks in advance, George



感谢帖子,但是你知道为什么传递

void或int *,甚至函数指针都没有问题?难道他们也没有名字错误

问题?奇怪的是,唯一失败的是传递

__delegates。


再次感谢,George
Thanks for the post but do you know why there are no problems when passing
void or int *, even function pointers? Wouldn''t they also have name mangling
issues? It seems strange that the only thing that fails is passing
__delegates.

Thanks again, George


这篇关于无法将__delegate指针传递给C ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆