【问题标题】:What DLLs and libs do I need so I can use `timeBeginPeriod` or other parts of `timeapi.h`?我需要哪些 DLL 和库才能使用“timeBeginPeriod”或“timeapi.h”的其他部分?
【发布时间】:2021-07-08 15:58:53
【问题描述】:

尽管Windowsapp.lib 或 API 集中存在许多 Windows API 函数(请参阅 this answer to How to declare and link to RoInitialize,RoUninitialize,RoGetActivationFactory and HSTRING Functions in Mingw Gcc),但许多函数却不在 listed as included in WindowsApp.libthe extension APIs 中。

例如timeBeginPeriod,我想用它来设置Sleep的分辨率。

它是Timeapi 的一部分,在 WindowsApp.lib 或扩展 API 中可用的函数列表中的任何地方都没有提到它。该文档也没有提及任何 API 集。

  • 我是否链接到winmm.libwinmm.dll
  • 我是否包括Windows.htimeapi.h

我怎么知道? RoInitialize 没有提到 DLL 或 API 集,但它有几个可用。

【问题讨论】:

  • 您链接到的文档显示“包含 Windows.h”和“库 Winmm.lib”。
  • Windowsapp.lib、RoXXX 是 WinRT。 timeBeginPeriod 是 Win32。
  • 看起来您错误标记了问题,这似乎不是 winapi 问题
  • 我很困惑; docs.microsoft.com/en-us/windows/win32/api/timeapi/… 没有解释如何使用它,它只是提到了头文件和库。我知道它们是相关的,但我正在寻找使用此信息的示例。另外,这不是 WinAPI 问题吗?它是关于作为 Windows API 一部分的函数。

标签: c++ windows winapi winmm


【解决方案1】:

我想我会自己调查一下。我写了一个小测试程序,用VS编译器测试。

  1. 通过“开始”,我启动了一个 VS 开发人员提示(among other ways of doing that 就像对Run cl.exe from cmd 的回答一样)。
  2. 我编写了一些简单的程序来测试各种情况。

答案

我的实验表明,这个问题的答案并不一定简单:

  • 我是否链接到winmm.libwinmm.dll

您可以链接/使用 winmm,但 windowsapp.lib 也足够了,尽管没有记录时间 API 函数是其中的一部分。

  • 我是否包括Windows.htimeapi.h

至少在我的实验中,Windows.h 实际上需要使用timeBeginPeriodtimeapi.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!

【讨论】:

    猜你喜欢
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2017-07-06
    • 2010-11-05
    • 2017-11-04
    • 2013-11-13
    相关资源
    最近更新 更多