簡體   English   中英

包裝C#的C ++ CLI類

[英]Wrapping C++ CLI Class For C#

我試圖從C#中調用某些Windows基本功能,尤其是這一功能 自從我也想學習C ++ / CLI語言的那一刻起,我就寫下了這段代碼:


#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()
    {
    }
}

但是當我編譯時,出現以下錯誤:

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

我在哪里做錯了?

您需要鏈接到user32.libEnumDisplayDevices函數的庫,如您所鏈接的MSDN頁面中所示)。

您可以通過以下方法執行此操作:轉到項目屬性->鏈接器->輸入,然后將user32.lib添加到“其他依賴項”列表中。

我注意到默認情況下,C ++ / CLI的默認Visual Studio項目設置不包括常見的Windows API庫(常規C ++項目在新版本中將kernel32.libuser32.libshell32.lib以及其他添加到項目的庫依賴項中項目),因此如果使用它們,則必須自己添加這些庫。

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

這是鏈接程序正在尋找的名稱。 不是它的名字,它是C函數,沒有C ++名稱修飾。 尚不清楚您是如何做到的,尤其是因為您混淆了#includes。 但是唯一合理的猜測是您自己聲明了此函數,而不是在SDK標頭中使用了它的聲明。

絕對不要那樣做。 而是使用:

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

在#pragma幫助下,您可以忘記鏈接到user32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM