CallingConvention理解


CallingConvention理解

有以下几个值可以使用:Cdecl, FastCall, StdCall, ThisCall, Winapi.

Cdecl:由调用者清理栈资源。非常适合用在可变参数的函数调用上,例如printf.

FastCall: Calling convention不支持。

StdCall:由被调用者清理栈资源。这是调用native函数时默认的方式。

ThisCall:第一个参数是this指针,会被存储在ECX寄存器里,而其它的参数会被压栈。这种方式通常用在调用未托管的DLL的方法或类。

Winapi:实际上并不是一个calling convention,实际上会被默认的平台的calling convention替代。例如window上调用,会替换成StdCall,Windows CE.NET上则被替换成Cdecl.

小例子:

using namespace System;
using namespace System::Runtime::InteropServices;
public ref class LibWrap
{
public:

   // CallingConvention.Cdecl must be used since the stack is 
   // cleaned up by the caller.
   // int printf( const char *format [, argument]... )

   [DllImport("msvcrt.dll",CharSet=CharSet::Unicode, CallingConvention=CallingConvention::Cdecl)]
   static int printf( String^ format, int i, double d );

   [DllImport("msvcrt.dll",CharSet=CharSet::Unicode, CallingConvention=CallingConvention::Cdecl)]
   static int printf( String^ format, int i, String^ s );
};

int main()
{
   LibWrap::printf( "\nPrint params: %i %f", 99, 99.99 );
   LibWrap::printf( "\nPrint params: %i %s", 99, "abcd" );
}

链接来源这里


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM