1

I'm trying to call some Windows basic functions from C#, in particular this one. Since the moment I want to learn the C++/CLI Language too, I've written down this code:


#pragma once

#include <string>
#include <Windows.h>

using namespace System;

namespace InformazioniSchermo {

    public class Native_InformazioniDaSistema
    {
    public:
        int m_nAltezzaPannello;
        int m_nLarghezzaPannello;

        Native_InformazioniDaSistema(void)
        {
            DISPLAY_DEVICE dd;
            DWORD dev = 0;

            dd.cb = sizeof(dd);
            EnumDisplayDevices(0, dev, &dd, 0);
            m_nAltezzaPannello = 100;
            m_nLarghezzaPannello = 100;
        }
    };

    public ref class InformazioniDaSistema
    {
    public:
        InformazioniDaSistema();
        ~InformazioniDaSistema();

    public:
        int m_nHeight;
        int m_nWidth;
    };

    InformazioniDaSistema::InformazioniDaSistema()
    {
        Native_InformazioniDaSistema foo;

        m_nHeight = foo.m_nAltezzaPannello;
        m_nWidth = foo.m_nLarghezzaPannello;
    }

    InformazioniDaSistema::~InformazioniDaSistema()
    {
    }
}

but when I compile, I get this error:

Error   3   error LNK2028: at unresolved token (0A0003B4) "extern "C" int __stdcall EnumDisplayDevicesW(wchar_t const *,unsigned long,struct _DISPLAY_DEVICEW *,unsigned long)" (?EnumDisplayDevicesW@@$$J216YGHPB_WKPAU_DISPLAY_DEVICEW@@K@Z) referencing in function "public: __thiscall InformazioniSchermo::Native_InformazioniDaSistema::Native_InformazioniDaSistema(void)" (??0Native_InformazioniDaSistema@InformazioniSchermo@@$$FQAE@XZ)  c:\Users\massimiliano\documents\visual studio 2013\Projects\InformazioniSchermo\InformazioniSchermo\InformazioniSchermo.obj InformazioniSchermo

Where am I doing wrong?

2 Answers 2

4

You need to link against user32.lib (the library for the EnumDisplayDevices function, as you'll see in the MSDN page you linked to).

You can do this by going to project properties->Linker->Input and adding user32.lib to the "Additional Dependencies" list.

I notice that the default Visual Studio project settings for C++/CLI don't include the common Windows API libraries by default (regular C++ projects have kernel32.lib, user32.lib, shell32.lib and others added to the project's library dependencies in new projects) so you have to add these libraries yourself if you're using them.

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

Comments

2
 error LNK2028: ... (?EnumDisplayDevicesW@@$$J216YGHPB_WKPAU_DISPLAY_DEVICEW@@K@Z) ...

That is the name the linker is looking for. That is not it's name, it is a C function and does not have the C++ name mangling. Pretty unclear how you did that, especially since you obfuscated your #includes. But the only reasonable guess is that you declared this function yourself instead of using its declaration in the SDK header.

Never do that. Instead use:

  #include <Windows.h>
  #pragma comment(lib, "user32.lib")

With the #pragma helpful so you can't forget to link to user32

Comments

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.