如何解决我需要什么 DLL 和库才能使用 `timeBeginPeriod` 或 `timeapi.h` 的其他部分?
尽管许多 Windows API 函数存在于 Windowsapp.lib
或 API 集中(请参阅 this answer to How to declare and link to RoInitialize,RoUninitialize,RoGetActivationFactory and HSTRING Functions in Mingw Gcc),但许多函数不在 listed as included in WindowsApp.lib 或 the extension APIs 中。
例如,timeBeginPeriod
,我想用它来设置 Sleep
的分辨率。
它是 Timeapi
的一部分,在 WindowsApp.lib 或扩展 API 中可用的函数列表中的任何地方都没有提及。该文档也没有提到任何 API 集。
- 我是否链接到
winmm.lib
和winmm.dll
? - 我是否包含
Windows.h
或timeapi.h
?
我怎么知道? RoInitialize 没有提到 DLL 或 API 集,但有几个可用。
解决方法
我想我会自己调查一下。自己写了一个小测试程序,用VS编译器测试。
- 通过 Start,我启动了 VS 开发人员提示(among other ways of doing that 就像对 Run cl.exe from cmd 的回答一样)。
- 我编写了一些简单的程序来测试各种情况。
答案
我的实验表明,这个问题的答案并不一定是直截了当的:
- 我是否链接到
winmm.lib
和winmm.dll
?
您可以链接/使用 winmm
,但 windowsapp.lib
也足够了,即使没有记录时间 API 函数是其中的一部分。
- 我是否包含
Windows.h
或timeapi.h
?
至少在我的实验中,Windows.h
实际上需要使用 timeBeginPeriod
。 timeapi.h
是不够的或必要的。我不清楚为什么会这样。
这是我得到这个答案的方式:
编译一个简单的程序
只是为了证明事情会编译:
#include <iostream>
int main()
{
std::cout << "Hello!" << std::endl;
}
>cl /EHsc src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
app.obj
> .\app.exe
Hello!
使用已知存在于 Windowsapp.lib 中的函数
#include <iostream>
#include <winstring.h>
int main()
{
std::cout << "Hello!" << std::endl;
// Ignore the poor error handling
HSTRING string;
WindowsCreateString(L"Test",4,&string);
const auto len = WindowsGetStringLen(string);
std::cout << len << std::endl;
WindowsDeleteString(string);
}
> cl /EHsc windowsapp.lib src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
windowsapp.lib
app.obj
> .\app.exe
Hello!
4
消耗函数时间API函数(本题)
#include <iostream>
// Interestingly,timeapi.h does not work:
// #include <timeapi.h>
#include <Windows.h>
int main()
{
std::cout << "Hello!" << std::endl;
timeBeginPeriod(500);
}
链接 winmm.lib:
> cl /EHsc winmm.lib src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
winmm.lib
app.obj
> .\app.exe
Hello!
有趣的是,您也可以只链接 Windowsapp.lib:
> cl /EHsc windowsapp.lib src\app.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29336 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
app.cpp
Microsoft (R) Incremental Linker Version 14.28.29336.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:app.exe
windowsapp.lib
app.obj
> .\app.exe
Hello!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。