0

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.

  • Do I link to winmm.lib and winmm.dll?
  • Do I include 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.

citelao
  • 4,898
  • 2
  • 22
  • 36
  • 7
    The documentation you link to says "include Windows.h" and "Library Winmm.lib". – Some programmer dude Apr 13 '21 at 15:13
  • 2
    Windowsapp.lib, RoXXX are WinRT. timeBeginPeriod is Win32. – Simon Mourier Apr 13 '21 at 15:16
  • Looks like you mis-tagged the question, this doesn't seem to be a winapi question – David Heffernan Apr 14 '21 at 06:38
  • 1
    I'm confused; https://learn.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timebeginperiod does not explain how to use it, it just mentions headers and libraries. I get that they're relevant, but I was looking for an example of consuming this information. Also, is this not a WinAPI question? It's about a function that is part of the Windows API. – citelao Apr 14 '21 at 18:51

1 Answers1

3

I figured I'd look into this myself. I wrote a little test program and used the VS compiler to test.

  1. Via Start, I launched a VS developer prompt (among other ways of doing that like in the answer to Run cl.exe from cmd ).
  2. I wrote up some simple programs to test the various cases.

The answer

My experimentation showed that the answer to this question is not necessarily straightforward:

  • Do I link to winmm.lib and winmm.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 or timeapi.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:

Compiling a simple program

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!

Consume functions known to exist in 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

Consuming functions time API functions (this question)

#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!
citelao
  • 4,898
  • 2
  • 22
  • 36