using System.Runtime.InteropServices;
namespace Vanara.PInvoke
{
/// Items from the WinMm.dll
public static partial class WinMm
{
/// The timeBeginPeriod function requests a minimum resolution for periodic timers.
///
/// Minimum timer resolution, in milliseconds, for the application or device driver. A lower value specifies a higher (more
/// accurate) resolution.
///
///
/// Returns TIMERR_NOERROR if successful or TIMERR_NOCANDO if the resolution specified in uPeriod is out of range.
///
///
///
/// Call this function immediately before using timer services, and call the timeEndPeriod function immediately after you are
/// finished using the timer services.
///
///
/// You must match each call to timeBeginPeriod with a call to timeEndPeriod, specifying the same minimum resolution in both
/// calls. An application can make multiple timeBeginPeriod calls as long as each call is matched with a call to timeEndPeriod.
///
///
/// Prior to Windows 10, version 2004, this function affects a global Windows setting. For all processes Windows uses the lowest
/// value (that is, highest resolution) requested by any process. Starting with Windows 10, version 2004, this function no longer
/// affects global timer resolution. For processes which call this function, Windows uses the lowest value (that is, highest
/// resolution) requested by any process. For processes which have not called this function, Windows does not guarantee a higher
/// resolution than the default system resolution.
///
///
/// Setting a higher resolution can improve the accuracy of time-out intervals in wait functions. However, it can also reduce
/// overall system performance, because the thread scheduler switches tasks more often. High resolutions can also prevent the CPU
/// power management system from entering power-saving modes. Setting a higher resolution does not improve the accuracy of the
/// high-resolution performance counter.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timebeginperiod MMRESULT timeBeginPeriod( UINT uPeriod );
[DllImport(Lib_Winmm, SetLastError = false, ExactSpelling = true)]
[PInvokeData("timeapi.h", MSDNShortId = "NF:timeapi.timeBeginPeriod")]
public static extern MMRESULT timeBeginPeriod(uint uPeriod);
/// The timeEndPeriod function clears a previously set minimum timer resolution.
/// Minimum timer resolution specified in the previous call to the timeBeginPeriod function.
///
/// Returns TIMERR_NOERROR if successful or TIMERR_NOCANDO if the resolution specified in uPeriod is out of range.
///
///
/// Call this function immediately after you are finished using timer services.
///
/// You must match each call to timeBeginPeriod with a call to timeEndPeriod, specifying the same minimum resolution in both
/// calls. An application can make multiple timeBeginPeriod calls as long as each call is matched with a call to timeEndPeriod.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timeendperiod MMRESULT timeEndPeriod( UINT uPeriod );
[DllImport(Lib_Winmm, SetLastError = false, ExactSpelling = true)]
[PInvokeData("timeapi.h", MSDNShortId = "NF:timeapi.timeEndPeriod")]
public static extern MMRESULT timeEndPeriod(uint uPeriod);
/// The timeGetDevCaps function queries the timer device to determine its resolution.
///
/// A pointer to a TIMECAPS structure. This structure is filled with information about the resolution of the timer device.
///
/// The size, in bytes, of the TIMECAPS structure.
///
/// Returns MMSYSERR_NOERROR if successful or an error code otherwise. Possible error codes include the following.
///
///
/// Return code
/// Description
///
/// -
/// MMSYSERR_ERROR
/// General error code.
///
/// -
/// TIMERR_NOCANDO
/// The ptc parameter is NULL, or the cbtc parameter is invalid, or some other error occurred.
///
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timegetdevcaps MMRESULT timeGetDevCaps( LPTIMECAPS ptc,
// UINT cbtc );
[DllImport(Lib_Winmm, SetLastError = false, ExactSpelling = true)]
[PInvokeData("timeapi.h", MSDNShortId = "NF:timeapi.timeGetDevCaps")]
public static extern MMRESULT timeGetDevCaps(out TIMECAPS ptc, uint cbtc = 8);
///
/// The timeGetSystemTime function retrieves the system time, in milliseconds. The system time is the time elapsed since
/// Windows was started. This function works very much like the timeGetTime function. See timeGetTime for details of these
/// functions' operation.
///
/// Pointer to an MMTIME structure.
/// Size, in bytes, of the MMTIME structure.
/// If successful, returns TIMERR_NOERROR. Otherwise, returns an error code.
/// The system time is returned in the ms member of the MMTIME structure.
// https://docs.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timegetsystemtime MMRESULT timeGetSystemTime( LPMMTIME
// pmmt, UINT cbmmt );
[DllImport(Lib_Winmm, SetLastError = false, ExactSpelling = true)]
[PInvokeData("timeapi.h", MSDNShortId = "NF:timeapi.timeGetSystemTime")]
public static extern MMRESULT timeGetSystemTime(out MMTIME pmmt, uint cbmmt = 8);
///
/// The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows
/// was started.
///
/// Returns the system time, in milliseconds.
///
///
/// The only difference between this function and the timeGetSystemTime function is that timeGetSystemTime uses the MMTIME
/// structure to return the system time. The timeGetTime function has less overhead than timeGetSystemTime.
///
///
/// Note that the value returned by the timeGetTime function is a DWORD value. The return value wraps around to 0
/// every 2^32 milliseconds, which is about 49.71 days. This can cause problems in code that directly uses the timeGetTime
/// return value in computations, particularly where the value is used to control code execution. You should always use the
/// difference between two timeGetTime return values in computations.
///
///
/// The default precision of the timeGetTime function can be five milliseconds or more, depending on the machine. You can use
/// the timeBeginPeriod and timeEndPeriod functions to increase the precision of timeGetTime. If you do so, the minimum
/// difference between successive values returned by timeGetTime can be as large as the minimum period value set using
/// timeBeginPeriod and timeEndPeriod. Use the QueryPerformanceCounter and QueryPerformanceFrequency functions to
/// measure short time intervals at a high resolution.
///
///
// https://docs.microsoft.com/en-us/windows/win32/api/timeapi/nf-timeapi-timegettime DWORD timeGetTime();
[DllImport(Lib_Winmm, SetLastError = false, ExactSpelling = true)]
[PInvokeData("timeapi.h", MSDNShortId = "NF:timeapi.timeGetTime")]
public static extern uint timeGetTime();
/// The TIMECAPS structure contains information about the resolution of the timer.
// https://docs.microsoft.com/en-us/windows/win32/api/timeapi/ns-timeapi-timecaps typedef struct timecaps_tag { UINT wPeriodMin;
// UINT wPeriodMax; } TIMECAPS, *PTIMECAPS, *NPTIMECAPS, *LPTIMECAPS;
[PInvokeData("timeapi.h", MSDNShortId = "NS:timeapi.timecaps_tag")]
[StructLayout(LayoutKind.Sequential)]
public struct TIMECAPS
{
/// The minimum supported resolution, in milliseconds.
public uint wPeriodMin;
/// The maximum supported resolution, in milliseconds.
public uint wPeriodMax;
}
}
}