Reputation: 250
I have a problem with this code, writing a wrapper function from an existing .lib (CryptoLib.lib):
mycode.ccp
#include "stdafx.h"
#pragma managed(push, off)
#include "CryptoLib.h"
#pragma comment (lib, "CryptoLib.lib")
#pragma managed(pop)
using namespace System;//This is a C++-CLI project.
__declspec(dllexport) void Encrypt(unsigned char *Data, unsigned char *RandomNr)
{
CryptoLib_Encrypt(Data, RandomNr);
}
cryptolib.h
#ifndef _CRYPTOLIB_H_
#define _CRYPTOLIB_H_
#define PUBLIC
//This procedure is written in c++ code
extern void CryptoLib_Encrypt(unsigned char *Data, unsigned char *RandomNr);
#endif /* _CRYPTOLIB_H_ */
I have linked the cryptolib.h and the cryptolib, but i get still the "unresolved token Cryptolib_Encrypt" and the "unresolved external symbol Cryptolib_Encrypt" Errors.
Can anyone tell me why?
Thanks for helping
Exact Error-messages:
error LNK2028: unresolved token (0A000006) "void __cdecl CryptoLib_Encrypt(unsigned char *,unsigned char *)" (?CryptoLib_Encrypt@@$$FYAXPAE0@Z) referenced in function "void __cdecl Encrypt(unsigned char *,unsigned char *)" (?Encrypt@@$$FYAXPAE0@Z)
error LNK2019: unresolved external symbol "void __cdecl CryptoLib_Encrypt(unsigned char *,unsigned char *)" (?CryptoLib_Encrypt@@$$FYAXPAE0@Z) referenced in function "void __cdecl Encrypt(unsigned char *,unsigned char *)" (?Encrypt@@$$FYAXPAE0@Z)
error LNK1120: 2 unresolved externals
The Dumpbin.exe /exports
comand line
returns just
But i still added the C/C++ Additional Include Directory in Configuration Properties/"C/C++"/General and the Additional Dependencies (Cryptolib.lib
) in Configuation Properties/Linker/Input
Upvotes: 3
Views: 3265
Reputation: 941455
#pragma once
#pragma comment (lib, "CryptoLib.lib")
#include "stdafx.h"
This is wrong. The compiler will go looking for the stdafx.h #include directive and ignores anything it finds before that. So it will completely ignore your #pragma comment directive. The linker will therefore not link CryptoLib.lib and you'll indeed get this linker error. Using #pragma once in a .cpp file does not make sense.
Another problem is that you appear to compile this code with /clr in effect, we can tell from the using statement. The compiler cannot tell that your function is a __cdecl function, it will assume the default and that's __clrcall when managed code compilation is enabled. You have to be explicit about it, like this:
#include "stdafx.h" // First line in file!
#pragma managed(push, off)
#include "CryptoLib.h"
#pragma comment (lib, "CryptoLib.lib")
#pragma managed(pop)
There's yet another possible problem with the function declaration, it isn't clear whether that function was compiled with a C compiler or a C++ compiler. A C++ compiler will decorate the function name. If it was actually compiled with a C compiler then you have to tell the compiler about that:
#ifndef _CRYPTOLIB_H_
#define _CRYPTOLIB_H_
#ifdef __cplusplus
extern "C" {
#endif
void __cdecl CryptoLib_Encrypt(unsigned char *Data, unsigned char *RandomNr);
#ifdef __cplusplus
}
#endif
#endif /* _CRYPTOLIB_H_ */
Note the usage of extern "C"
, it disables name decoration. If you cannot or should not edit this header file then you can cram the extern "C" {} in your .cpp file, surrounding the #include.
If you still have trouble then post the exact linker error message as well what you see when you run Dumpbin.exe /exports
on the DLL from the Visual Studio Command Prompt.
Upvotes: 5