Problem linking managed DLL to native DLL

D

Dave Burns

I have the following situation:

There are two dlls in question: a c++ dll (native) and a c++/cli dll
(managed) with /clr option enabled.

Managed dll links with native dll.

Native dll exports a class ClientSession with a method GetJob.

Managed dll references this method and compiles just fine.

Managed dll does NOT link however and gives this error:

error LNK2028: unresolved token (0A000168) "public: class
Rimage::Simple::Api::Job * __thiscall
Rimage::Simple::Api::ClientSession::GetJobW(wchar_t const *)"
(?GetJobW@ClientSession@Api@Simple@Rimage@@$$FQAEPAVJob@234@PB_W@Z)
referenced in function "public: class Rimage::Simple::Dotnet::Api::Job ^
__clrcall Rimage::Simple::Dotnet::Api::ClientSession::GetJobW(class
System::String ^)"
(?GetJobW@ClientSession@Api@Dotnet@Simple@Rimage@@$$FQ$AAMP$AAVJob@2345@P$AAVString@System@@@Z)

Notice that the linker is complaining about GetJobW, NOT GetJob.

When I right click on GetJob and select "Go to Definition" it takes me to
winspool.h where the following block exists:

#ifdef UNICODE
#define GetJob GetJobW
#else
#define GetJob GetJobA
#endif // !UNICODE

Just for kicks I deleted this block and all of a sudden the linker worked!

BTW, when I link a native application (exe) with the same native dll and
call GetJob on my class it compiles and links just fine. So it seems to have
something to do with the fact that I am linking a managed dll with a native
dll.

Any help would be greatly appreciated

Thanks, Dave
 
W

William DePalo [MVP VC++]

Dave Burns said:
I have the following situation:
When I right click on GetJob and select "Go to Definition" it takes me to
winspool.h where the following block exists:

#ifdef UNICODE
#define GetJob GetJobW
#else
#define GetJob GetJobA
#endif // !UNICODE

Just for kicks I deleted this block and all of a sudden the linker worked!

That's a drastic solution. :)

Immediately after including the Windows header files try adding an

#undef GetJob

line.

In case you don't know, to support both ASCII and UNICODE projects,
virtually all of the functions of the Windows API on NT/2K/XP/2K+3/Vista
that use strings come in two versions. In one version the name of the
function ends in A and takes or returns ANSI characters. The other ends in W
and is for UNICODE.

Macro trickery is used to hide the fact that there are two ostensibly
identical functions with the same name. That trickery, and the fact that one
of your member functions has the same name is biting you.

Regards,
Will
 
G

Guest

Native dll exports a class ClientSession with a method GetJob.
Managed dll references this method and compiles just fine.

Managed dll does NOT link however and gives this error:

error LNK2028: unresolved token (0A000168) "public: class
Rimage::Simple::Api::Job * __thiscall
Rimage::Simple::Api::ClientSession::GetJobW(wchar_t const *)"
(?GetJobW@ClientSession@Api@Simple@Rimage@@$$FQAEPAVJob@234@PB_W@Z)
referenced in function "public: class Rimage::Simple::Dotnet::Api::Job ^
__clrcall Rimage::Simple::Dotnet::Api::ClientSession::GetJobW(class
System::String ^)"
(?GetJobW@ClientSession@Api@Dotnet@Simple@Rimage@@$$FQ$AAMP$AAVJob@2345@P$AAVString@System@@@Z)

Notice that the linker is complaining about GetJobW, NOT GetJob.

When I right click on GetJob and select "Go to Definition" it takes me to
winspool.h where the following block exists:

#ifdef UNICODE
#define GetJob GetJobW
#else
#define GetJob GetJobA
#endif // !UNICODE

Just for kicks I deleted this block and all of a sudden the linker worked!

BTW, when I link a native application (exe) with the same native dll and
call GetJob on my class it compiles and links just fine. So it seems to have
something to do with the fact that I am linking a managed dll with a native
dll.

I have had the same problem before with other function names.
basically, the problem is that GetJob is a macro like most win32 functions.

during preprocessing, the preprocessor will bluntly replace all instances of
'GetJob' with another
function name, depending on whether your build is for unicode or ascii.

if you wouldn't include windows.h somewhere, you would not have this problem.
this has nothing to do with managed or unmanaged.

there are 3 workarounds that I know of.
- use a different method name instead of GetJob
- bluntly #undef GetJob in the header file of your unmanaged class.
this could have the side effect for anyone using your header that they have
to explicitly use GetJobW or GetJobA because the GetJob macro is not
working anymore.
- everywhere you call GetJob, use pragma push_macro, undef and pragma
pop_macro to locally
change the meaning of GetJob. this is tedious, and all users of the GetJob
function have to do it.

maybe there is a smarter workaround, but I just used a different name and
moved on.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top