C++: Wrapping unmanaged dll using dll export
I am using Visual Studio 2010 targeting .Net 4.0
I am working with an unmanaged C++ dll using a managed C++ wrapper. I am using _declspec(dllexport) to export the unmanaged .dll below is the header file for the unmanaged dll:
class DllExport KeyManager
{
public:
KeyManager(const char *pszKeyFileName, int thisProduct);
~KeyManager();
...
I am then making a call to the unmanaged dll from the managed wrapper here:
#include "stdafx.h"
#include "KeyModCLR.h"
#using <mscorlib.dll>
#include <msclr/marshal.h>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr::interop;
MCKeyManager::MCKeyManager(String ^fileName, int thisProduct)
{
pszFileName = (char*)Marshal::StringToHGlobalAnsi(fileName).ToPointer();
m_pC = new KeyManager(pszFileName, thisProduct);
}
This all works great when the project targets Win32, however when I change the target platform to x64 as described here I get the following error:
Error 17 error LNK2019: unresolved external symbol "public: __cdecl
KeyManager::KeyManager (char const *,int)" (??0KeyManager@@$$FQEAA@PEBDH@Z) referenced in
function "public: __clrcall MCKeyManager::MCKeyManager(class System::String ^,int)" (?? 0MCKeyManager@@$$FQE$AAM@PE$AAVString@System@@H@Z)
I am 开发者_开发问答not terribly familiar with C++ as I did not write this code so I don't know if I am missing something obvious. I have read that decoration of the imported functions are different for 64 bit and 32 bit but I am not sure how that necessarily affects me.
I have been looking for an answer all day and have come up short so any suggestions or advice would be greatly appreciated.
Thanks in advance
This type of error is probably attributable to the Configuration Manager setup. When you set up the x64 configuration, configuration manager will often set dependant .dll's to be compiled as Win32 instead of x64. Open Configuration Manager for the solution, review the settings for each project. Verify that they will build an x64 configuration and that they are checked on to build.
Another useful check is on calling convention. For instance, if one project uses _cdecl
and another uses _stdcall
, then calls between the projects will fail to link. The error message will show the calling convention that is being used by the calling project. You can use DUMPBIN / EXPORTS on the target .dll or .lib file to find out the calling convention that is being exported. (DUMPBIN is in the VC/bin folder).
精彩评论