0

I need following function (from C++ dll) available in C++/CLI

extern "C" _declspec(dllexport) void __stdcall DestroyInstance(CKeyManagerServerApp *ptr);

My try:

[DllImport("KeyManagerServer.dll", CallingConvention=CallingConvention::StdCall)]
void DestroyInstance(CKeyManagerServerApp IntPtr);

The C++/CLI wrapper is compiled with /clr and stdcall (C++ dll also with stdcall)!

I got following errors:

MKeyManagerInterface.obj : error LNK2028: unresolved token (0A000585) "extern "C" void __stdcall DestroyInstance(class CKeyManagerServerApp *)" (?DestroyInstance@@$$J14YGXPAVCKeyManagerServerApp@@@Z) referenced in function "private: __clrcall MKeyManagerInterface::ManagedKeyInterface::~ManagedKeyInterface(void)" (??1ManagedKeyInterface@MKeyManagerInterface@@$$FA$AAM@XZ)
1>MKeyManagerInterface.obj : error LNK2019: unresolved external symbol "extern "C" void __stdcall DestroyInstance(class CKeyManagerServerApp *)" (?DestroyInstance@@$$J14YGXPAVCKeyManagerServerApp@@@Z) referenced in function "private: __clrcall MKeyManagerInterface::ManagedKeyInterface::~ManagedKeyInterface(void)" (??1ManagedKeyInterface@MKeyManagerInterface@@$$FA$AAM@XZ)
1>..\Debug\Bin\KeyManagerInterfaceD.dll : fatal error LNK1120: 2 unresolved externals

How can I solve this linker errors?

Thx

1 Answer 1

2

You shouldn't need to use P/Invoke from C++/CLI. You should be able to include the usual C/C++ header files that declare the imported function. Just make sure to link your C++/CLI assembly against the export library from the native-code DLL that exports the function in question.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but if I want to load the library at runtime linking against lib is not suitable!
@leon22 But you never stated that as a requirement in your post :) Can you just use LoadLibrary + GetProcAddress like unmanaged Windows C/C++ code does?
Yes, this was my first idea! But I'm always getting strange memory errors! see: stackoverflow.com/questions/11306602/heap-error-in-c-sharp
If you've got heap corruption issues, you should work on resolving them regardless of this loading problem. (I also don't understand how LoadLibrary relates to heap corruption.) Perhaps you could try using tools like Application Verifier to help catch heap issues?
Thx! It was only an idea! I will try with Application Verifier!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.