c++ - #在 DLL 文件中包含 Windows.h

标签 c++ windows winapi

我正在尝试使用 ChooseColor 在 C++ 中创建颜色对话框 DLL。尽管每次构建时都会遇到问题

1>ChooseColorDLL.obj : error LNK2019: unresolved external symbol __imp_ChooseColorA referenced in function "void __cdecl ShowMyDialog(char *)" (?ShowMyDialog@@YAXPEAD@Z)

这是我的 DLL 中的 .cpp 文件:

#include "stdafx.h"
#include "ChooseColorDLL.h"
#include <commdlg.h>
#include <fstream>
#include <iostream>

CHOOSECOLORDLL_API void ShowDialog(char* i)
{

    static COLORREF  colorrefCustomColours[16] = {0} ;
    CHOOSECOLOR cc;

    cc.hwndOwner = NULL;    //No Owner
    cc.hInstance = NULL;
    cc.rgbResult =    RGB(0,0,0);
    cc.lpfnHook =NULL;
    cc.Flags = CC_SOLIDCOLOR | CC_PREVENTFULLOPEN;
    cc.lpCustColors = colorrefCustomColours;
    cc.lpTemplateName = NULL;
    cc.lCustData = NULL;
    cc.lStructSize = sizeof(cc);
    // Seperate Colors
    ChooseColor(&cc);
}

选择ColorDLL.h:

#ifdef CHOOSECOLORDLL_EXPORTS
#define CHOOSECOLORDLL_API __declspec(dllexport)
#else
#define CHOOSECOLORDLL_API __declspec(dllimport)
#endif

CHOOSECOLORDLL_API void ShowDialog(char* i);

最佳答案

我相当确定问题是您的版本未设置 WINVER,并且 _WIN32_WINNT 版本要么未设置,要么设置不正确 - 请参阅 this

换句话说,在 #include <windows.h> 之前,你应该有:

#define WINVER 0x0500
#define _WIN32_WINNT 0x0500

默认情况下,为了让应用程序在“任何”版本的 Windows 上运行,windows.h 只提供从 WinNT4 及更高版本的每个版本的 Windows 中可用的函数。由于这是在 Win2K 中引入的,因此它不是默认的。上面的定义为您提供了 Win2K 变体。其他值记录在 here

关于c++ - #在 DLL 文件中包含 Windows.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16950628/

相关文章:

c++ - pcap_next 调用填充 pcap_pkthdr,其中 len 等于零

c++ - std::thread 重载未解决(使用正确的参数)

c++ - 检查 "Whether for a given directed graph there is only one way to sort the graph using topological sort or not"的算法优化

windows - GetFinalPathNameByHandle 对于设备句柄失败

windows - Windows中的jq命令无法按预期工作

c++ - WIN32 API 项目中的 ReadDirectoryChangesW 错误?

c++ - boost::spirit 算术公式解析器编译失败

COMDAT 与 BSS 定义

c# - 可以使用 Winkey+L 进行低级键盘 Hook /发送输入吗? (工作站锁定在 Vista 及更高版本中被拦截)

windows - Win32_PhysicalMemory.Capactiy——它在 32 位应用程序中的表现如何?