citelao
citelao

Reputation: 6056

What DLLs and libs do I need so I can use `timeBeginPeriod` or other parts of `timeapi.h`?

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.

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.

Upvotes: 0

Views: 2954

Answers (1)

citelao
citelao

Reputation: 6056

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!

Upvotes: 4

Related Questions