简体   繁体   中英

Unresolved Externals in C++: Visual C++ mangles method signature differently from mangled method in dll

I'm using Visual Studio 2012, managed C++, to make a bridge between a third party SDK and our system which is written in C#. I have succesfully wrapped and consumed several functions from said SDK. Except one, which only result in a Unresolved External Error.

The SDK's header file defines the function's signature:

#if defined WIN32
    #if defined BUILD_ADS_SHARED_LIB
        #define ADS_LINK_SPEC __declspec (dllexport)
        #define ADS_CALLING_CONVENTION __stdcall
    #elif defined USE_ADS_SHARED_LIB
        #define ADS_LINK_SPEC __declspec (dllimport)
        #define ADS_CALLING_CONVENTION __stdcall
    #else
        #define ADS_LINK_SPEC
        #define ADS_CALLING_CONVENTION
    #endif
#else
    #define ADS_LINK_SPEC
    #define ADS_CALLING_CONVENTION
#endif

DatabaseResult ADS_LINK_SPEC ADS_CALLING_CONVENTION
createDatabase(
    const Settings& settings, Artec::SdkDatabase::iDatabase *& instance);

The error says:

Error   10  error LNK2028: unresolved token (0A000089) "enum Artec::SdkDatabase::DatabaseResult __cdecl Artec::SdkDatabase::createDatabase(class Artec::SdkDatabase::Settings const &,class Artec::SdkDatabase::iDatabase * &)" (?createDatabase@SdkDatabase@Artec@@$$FYA?AW4DatabaseResult@12@ABVSettings@12@AAPAViDatabase@12@@Z) referenced in function "private: static enum Artec::SdkDatabase::DatabaseResult __clrcall Broadway3dWrapper::Broadway3dWrapper::GetConn(wchar_t const *,wchar_t const *,wchar_t const *,wchar_t const *,char const *,class Artec::SdkDatabase::iDatabase * &)" (?GetConn@Broadway3dWrapper@1@$$FCM?AW4DatabaseResult@SdkDatabase@Artec@@PB_W000PBDAAPAViDatabase@34@@Z) C:\bioap\tfs\Identitum\Dev\src\BA.Identitum.Devices.Broadway3d\Broadway3dWrapper.obj    BA.Identitum.Devices.Brodway3D

So it's looking for the mangled name:

?createDatabase@SdkDatabase@Artec@@$$FYA?AW4DatabaseResult@12@ABVSettings@12@AAPAViDatabase@12@@Z

Making a little dumpbin on the referenced dll, I found there is in fact a function called like that exported, thing is the name is mangled slightly different:

?createDatabase@SdkDatabase@Artec@@YG?AW4DatabaseResult@12@ABVSettings@12@AAPAViDatabase@12@@Z

Can anyone help me here? I cannot contact the SDK vendor, and I'm completely lost here.

The difference between those two lies in the calling convention section.

createDatabase@SdkDatabase@Artec@@YG?AW4DatabaseResult@12@ABVSettings@12@AAPAViDatabase@12@@Z is stdcall: enum Artec::SdkDatabase::DatabaseResult __stdcall Artec::SdkDatabase::createDatabase(class Artec::SdkDatabase::Settings const &,class Artec::SdkDatabase::iDatabase * &)

The demangler I used does not understand ?createDatabase@SdkDatabase@Artec@@$$FYA?AW4DatabaseResult@12@ABVSettings@12@AAPAViDatabase@12@@Z , but the part where they differ ( @@$$FYA? vs @@YG? ) is the calling convention (if I change YG to YF , the calling convention changes and nothing else does).

Change your declaration of the function to return-type __stdcall function-name[(argument-list)] .

When you included the header file, did you #define USE_ADS_SHARED_LIB explicitly or on the compiler command line? Are you targeting 32 bit windows?

Use the undname.exe utility to undecorate names. It is looking for:

enum Artec::SdkDatabase::DatabaseResult 
__cdecl 
Artec::SdkDatabase::createDatabase(
    class Artec::SdkDatabase::Settings const &,
    class Artec::SdkDatabase::iDatabase * &
)

The one you found is:

enum Artec::SdkDatabase::DatabaseResult 
__stdcall 
Artec::SdkDatabase::createDatabase(
    class Artec::SdkDatabase::Settings const &,
    class Artec::SdkDatabase::iDatabase * &
)

Everything matches, except the calling convention, __cdecl vs __stdcall. Note how the SDK header allows this to happen, it doesn't raise a stink when neither BUILD_ADS_SHARED_LIB nor USE_ADS_SHARED_LIB is #defined. And that will produce a __cdecl function. Bad idea btw.

So very high odds that you simply forgot to define USE_ADS_SHARED_LIB. Project + Properties, C/C++, Preprocessor, Preprocessor Definitions setting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM