Whereas many Windows API functions exist in Windowsapp.lib
or in API sets (see this answer to How to declare and link to RoInitialize,RoUninitialize,RoGetActivationFactory and HSTRING Functions in Mingw Gcc), many functions are not listed as included in WindowsApp.lib or in the extension APIs.
For example, timeBeginPeriod
, which I want to use to set the resolution for Sleep
.
It is part of Timeapi
, which is not mentioned anywhere in the list of functions available in WindowsApp.lib or extension APIs. The documentation also does not mention any API set.
winmm.lib
and winmm.dll
?Windows.h
or timeapi.h
?How would I know? RoInitialize does not mention a DLL or an API set, but it is available in several.
Disclaimer: I work for Microsoft.
I figured I'd look into this myself. I wrote a little test program and used the VS compiler to test.
My experimentation showed that the answer to this question is not necessarily straightforward:
- Do I link to
winmm.lib
andwinmm.dll
?
You may link/consume winmm
, but windowsapp.lib
is also sufficient, even though it is not documented that the time API functions are part of it.
- Do I include
Windows.h
ortimeapi.h
?
At least in my experimentation, Windows.h
was actually required to use timeBeginPeriod
. timeapi.h
was not sufficient or necessary. It is unclear to me why that is the case.
Here's how I got this answer:
Just to prove things will compile:
#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!
#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
#include <iostream>
// Interestingly, timeapi.h does not work:
// #include <timeapi.h>
#include <Windows.h>
int main()
{
std::cout << "Hello!" << std::endl;
timeBeginPeriod(500);
}
Linking 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!
Interestingly, you can also link just Windowsapp.lib instead:
> 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!