-1

以下は単純なネイティブ DLL です。

ネイティブ.h :

#ifdef BUILDING_NATIVE_DLL
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif

class DLLAPI Native
{
    public: void f();
};

ネイティブ.cpp :

#include "Native.h"

void Native::f()
{
}

建てる:

cl /DBUILDING_NATIVE_DLL /LD Native.cpp
...
    Creating library Native.lib and object Native.exp

C++/CLIアプリケーションから使用したいと思います。

Managed.cpp :

#include "Native.h"

int main()
{
    Native* native = new Native();
    native->f();
}

CLRモード「IJW」でビルドできます:

cl /clr Managed.cpp Native.lib
...

/out:Managed.exe
Managed.obj
Native.lib

ただし、CLR モードの "pure"ではありません:

cl /clr:pure Managed.cpp Native.lib
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01
for Microsoft (R) .NET Framework version 4.00.30319.18047
Copyright (C) Microsoft Corporation.  All rights reserved.

Managed.cpp
c:\users\...\Native.h(9) : warning C42
72: 'Native::f' : is marked __declspec(dllimport); must specify native calling c
onvention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::Native' : is marked __declspec(dllimport); must specify native cal
ling convention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::~Native' : is marked __declspec(dllimport); must specify native ca
lling convention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::Native' : is marked __declspec(dllimport); must specify native cal
ling convention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::operator =' : is marked __declspec(dllimport); must specify native
 calling convention when importing a function.
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Managed.exe
/clrimagetype:pure
Managed.obj
Native.lib
Managed.obj : error LNK2028: unresolved token (0A000009) "public: void __clrcall
 Native::f(void)" (?f@Native@@$$FQAMXXZ) referenced in function "int __clrcall m
ain(void)" (?main@@$$HYMHXZ)
Managed.obj : error LNK2019: unresolved external symbol "public: void __clrcall
Native::f(void)" (?f@Native@@$$FQAMXXZ) referenced in function "int __clrcall ma
in(void)" (?main@@$$HYMHXZ)
Managed.exe : fatal error LNK1120: 2 unresolved externals

したがって、ビルドを壊しているように見えるのは、ネイティブの呼び出し規約がないことです。

実際、私がそれを指定した場合:

#ifdef BUILDING_NATIVE_DLL
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif

class DLLAPI Native
{
    public: void __thiscall f();
};

それは良いです:

cl /clr:pure Managed.cpp
Native.lib
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01
for Microsoft (R) .NET Framework version 4.00.30319.18047
Copyright (C) Microsoft Corporation.  All rights reserved.

Managed.cpp
c:\users\...\Native.h(10) : warning C4
272: 'Native::Native' : is marked __declspec(dllimport); must specify native cal
ling convention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::~Native' : is marked __declspec(dllimport); must specify native ca
lling convention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::Native' : is marked __declspec(dllimport); must specify native cal
ling convention when importing a function.
c:\users\...\Native.h(10) : warning C4
272: 'Native::operator =' : is marked __declspec(dllimport); must specify native
 calling convention when importing a function.
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:Managed.exe
/clrimagetype:pure
Managed.obj
Native.lib

ただし、生成されたメンバーにはまだ警告があります。

だからここに質問があります:

  • 生成されたメンバーが継承するクラス全体の呼び出し規約を指定することは可能ですか?
  • ヘッダー ファイルで呼び出し規約が指定されておらず、それを変更できない場合は、CLR モードでビルドする方法を "pure"にします。

ありがとう。

4

1 に答える 1