C++/CLI: linker gives "unresolved token" for win32 function

12,609

Solution 1

Additional dependancies was "NoInherit", when I looked "under" the setting, there was a list of libs, gdi32.lib was in the list. I checked "Inherit from parent project" and it now works. Dirk, if you add all that as an answer I'll select it and give you the rep.

Check if Gdi32.lib is there in the linker commandline(Properties > Linker > CommandLine).

(There you go -- you have successfully appealed to the my selfish, rep seeking part of soul ;) )

Solution 2

You should link your dll with Gdi32.lib.

You can either do it with a #pragma comment(lib, "gdi32.lib") or in your project's settings under Linker.

Solution 3

The clue is in the __clrcall modifier on the function declarations. Your Windows Forms app uses pure Common Language Runtime code and calling conventions by default. Your external library, which you're linking to, does not. You need to change the Common Language Runtime support setting in your Project Defaults area of the project properties from /clr:pure to /clr. That worked for me.

Share:
12,609
Rabeel
Author by

Rabeel

I'm Irish (and have been all my life) I've been writing code since I was 12 years old (professionally since 22 or so), and some day – hopefully soon - I'll figure out how to do it properly. I'm married (sorry ladies), and she's gorgeous, she's so far out of my league we're playing different sports. I swear there are days I marvel that she doesn't snap out of it and decide she can do far better :) . . . but I’m very glad she doesn’t. My second love for many years would have been C++, which has been pushed out by C#, very powerful, very concise. I often work with Vb, but I dislike it, being much more enamoured of the curly brace than I am of hugely verbose syntax. Although it’s many years since we last parted, I still hold a grudge against Vb3, 4, 5 & 6, oh, and all flavours of VBA. Languages that are, IMHO, as wrong and unholy as . . . well . . . something that’s really wrong and unholy (pick your own desecration and go with that imagery, I’m not going to do all the work for you). . . there are whole years I spent bleeding and blistered on the Pre Vb.Net cross . . . and I’m glad I can say “never again”. SOreadytohelp

Updated on June 05, 2022

Comments

  • Rabeel
    Rabeel almost 2 years

    Folks,

    I just created my first C++/CLI project (Visual Studio 2008), it's a Library to allow my C# app access an point of sale tally printer.

    My library builds well and trivial functions work when called from a C# exe.

    However as soon as I include a WinGDI call (DeleteObject in this case), the linker complains with “unresolved token” errors.

    Error 2 error LNK2028: unresolved token (0A000088) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject@@$$J14YGHPAX@Z) referenced in function "private: __clrcall ReceiptPrinter::Epson::~Epson(void)" (??1Epson@ReceiptPrinter@@$$FA$AAM@XZ) ReceiptPrinter.obj ReceiptPrinter

    I haven't done any serious C++ in the last 4 years, and I have precious little experience of MS C++ compilers, as such I don’t know what I’m looking for in the linker settings.

    Any help will be greatfully received.

    Thanks