如何解决GetSystemMetrics()的链接错误?

时间:2009-12-10 17:14:46

标签: c windows visual-c++ getsystemmetrics

我正在尝试使用以下内容来获得高度和高度主显示屏的宽度:

#include <winuser.h>
size_t width = (size_t)GetSystemMetrics(SM_CXBORDER);
size_t height = (size_t)GetSystemMetrics(SM_CYBORDER);

但是,未解决的外部链接错误(LNK1120)失败了。我已经尝试链接到user32.lib(记录为here),并收到相同的错误,以及链接到wmbase.lib(如文档here所示),并收到了wmbase的错误。 lib不存在!我做错了什么?

请注意,我只使用普通的'C - 而不是C ++。这是用于获取主显示屏的屏幕分辨率(以像素为单位)的正确功能吗?

我现在正试图在MSVC上编译它,但更喜欢可移植到其他编译器的解决方案。

感谢。

修改

所以看起来我要查找的参数是SM_CXSCREENSM_CYSCREEN,而不是SM_CXBORDERSM_CYBORDER。但是,我仍然无法设法将其编译。

如果它澄清了任何内容(当链接到user32.lib时),这是实际错误:

screen.obj : error LNK2019: unresolved external symbol __imp__GetSystemMetrics@4 referenced in function _getMainDisplaySize
build\lib.win32-2.6\foomodule\bitmap.pyd : fatal error LNK1120: 1 unresolved externals
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\link.exe"' failed with exit status 1120

(我正在尝试编译Python / C模块,这就是为什么你会看到奇怪的目录)

3 个答案:

答案 0 :(得分:2)

您希望#include <windows.h>代替<winuser.h>。原样,它可能会使原型上的一些修饰符错误。

编辑:既然你还有问题,也许我们可以从简化测试开始,看看你得到了什么。幸运的是,GetSystemMetrics()不需要窗口句柄或任何东西,所以e可以从一个简单的控制台应用程序调用它:

#include <windows.h>
#include <iostream>

int main() { 
    size_t width = (size_t)GetSystemMetrics(SM_CXSCREEN);
    size_t height = (size_t)GetSystemMetrics(SM_CYSCREEN);

    std::cout << "width = " << width << "\n";
    std::cout << "height = " << height << std::endl; 
    return 0;
}

这是一个编译并运行它的屏幕转储:

D:\C\source>cl screen_res.cpp user32.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

screen_res.cpp
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:screen_res.exe
screen_res.obj
user32.lib

D:\C\source>screen_res
width = 1600
height = 1200

如果这不能编译并运行,则可能是您的安装有问题。如果确实如此,则问题可能出在您正在进行的项目中。

答案 1 :(得分:0)

这对您的链接错误没有帮助,但您也质疑这是否是获取显示大小的正确方法。您传递给GetSystemMetrics的参数不会返回您想要的内容。检查documentation处的可用标记。另请考虑使用SystemParametersInfo。使用哪种调用取决于您要查找的内容的背景 - 监视器大小与工作区等。

答案 2 :(得分:0)

解决方案是在你的一个模块中定义COMPILE_MULTIMON_STUBS。

#define COMPILE_MULTIMON_STUBS
#include <multimon.h>