本文介绍了本机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 ?

推荐答案

使用命名空间系统;
#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调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:02