本机C ++类上的C ++/CLI混合模式和__cdecl调用约定 [英] C++/CLI mixed mode and __cdecl calling convention on native C++ class

查看:121
本文介绍了本机C ++类上的C ++/CLI混合模式和__cdecl调用约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++/CLI中的CLR应用程序中使用DLL中的本机C ++类.我想为该类的所有函数显式设置__cdecl调用约定,以便CLR应用程序不使用CLR的默认__clr调用约定来调用成员函数.通过将__cdecl放在头文件中的成员函数名称之前,将类转换为__cdecl调用约定.但是我看不到用类的构造函数/析构函数来做到这一点.如果我尝试将__cdecl放在构造函数或析构函数名称之前,则会出现编译器错误.如果不告诉CLR应用程序实例化该类的对象,并使用__cdecl调用约定将参数传递给该实例化对象,则该应用程序似乎无法正常工作.如何告诉CLR应用程序该类的构造函数/析构函数也使用__cdecl调用约定?

I have a native C++ class in a DLL which I want to use form a CLR application in C++/CLI. I want to explicitly set the __cdecl calling convention for all functions of the class so that the CLR application does not call the member functions using the default __clr calling convention for CLR.

I can explicitly set all the functions of the class to the __cdecl calling convention by putting __cdecl before the member function name in the header file. But I see no way to do this with the constructor/destructor of the class. If I attempt to put __cdecl before the constructor or destructor name, i get a compiler error. Without telling the CLR application to instantiate an object of the class and pass parameters to that instantiated object using the __cdecl calling convention, it would seem that the application would not work correctly. How can I tell the CLR application that the constructor/destructor of the class also uses the __cdecl calling convention ?

推荐答案

您使用的是错误的方法.
您必须记住的第一件事是,将使用thiscall编译c ++中的本机类.不管如何都调用约定.
如果要确保将其编译为本机,则应在类之前以及要切换时使用pragma指令

#pragma unmanaged

返回到托管/混合

#pragma托管

下面是一个简单的示例.

You are going about this the wrong way.
The first thing you must remember is that native classes in c++ will be compiled with the thiscall calling convention regardless.
If you want to guarentee that it will be compiled as native then you should use the pragma directive

#pragma unmanaged

before the class and when you want to switch back to managed/mixed

#pragma managed

A simple example to show this follows.

<身体>
使用命名空间系统;
#pragma非托管
class \ NativeClass
{
私人:
int a;
char b;
public:
NativeClass():a(0),b(0)
.{
〜NativeClass()
.{
int geta()
.{
返回a;
}
voidseta(int na)
{
a == na ;
char getb()
.{
返回b;
}
void setb(char nb)
{
b == nb ;
};
#pragma管理的
int main(array < System :: String ^ > ^ args)
{
NativeClass myclass;
myclass.seta(19);
控制台:: WriteLine("{0}",myclass.geta());
返回0;
}
using namespace System;  
 
#pragma unmanaged  
 
class NativeClass  
{  
private:  
    int a;  
    char b;  
 
public:  
    NativeClass() : a(0), b(0)  
    {  
    }  
 
    ~NativeClass()  
    {  
    }  
 
    int geta()  
    {  
        return a;  
    }  
 
    void seta(int na)  
    {  
        a = na;  
    }  
 
    char getb()  
    {  
        return b;  
    }  
 
    void setb(char nb)  
    {  
        b = nb;  
    }  
};  
 
#pragma managed  
 
int main(array<System::String ^> ^args)  
{  
    NativeClass myclass;  
 
    myclass.seta(19);  
    Console::WriteLine("{0}", myclass.geta());  
    return 0;  


这篇关于本机C ++类上的C ++/CLI混合模式和__cdecl调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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