2013-10-06 64 views
-1

下面是一個簡單機DLL:C++/CLI,DLLEXPORT和/ CLR:純

Native.h

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

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

Native.cpp

#include "Native.h" 

void Native::f() 
{ 
} 

Build:

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模式 「純」

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)" ([email protected]@@$$FQAMXXZ) referenced in function "int __clrcall m 
ain(void)" ([email protected]@$$HYMHXZ) 
Managed.obj : error LNK2019: unresolved external symbol "public: void __clrcall 
Native::f(void)" ([email protected]@@$$FQAMXXZ) referenced in function "int __clrcall ma 
in(void)" ([email protected]@$$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模式「純」

謝謝。

回答

1

嘗試包圍的原生頭文件的#include

#pragma managed(push, off) 
#include "Native.h" 
#pragma managed(pop) 

顯然,一個/clr:pure編譯單元不能有非託管定義的功能,但這些進口的只有聲明 - 它應該工作。

但是,總體而言,不推薦導出整個類。導出工廠函數並將它們用於類構造會更安全,然後返回一個指向接口(帶有純虛擬成員的基類)的指針並將其用於成員訪問。這就是COM所做的,而且這種技術在語言和編譯器版本之間是非常兼容的。

+0

謝謝Ben,很好的嘗試,但**#pragmas **不會改變任何東西。工廠的好處,我也喜歡COM方法,儘管我不一定使用它。 – Pragmateek