user1978386 user1978386 - 2 months ago 20
C++ Question

Linking failure. How to use 'NtQuerySystemTime' windows function?

I try this simple code to calculate HDD write speed in my application:

#include <winternl.h>

...
float speed;
double divident;
PLARGE_INTEGER systime0, systime1;
LONGLONG elapsed_time;
...

write_flag = true ;

NtQuerySystemTime(systime0) ;

f_out->write(out_buffer0, chunk_len0);
f_out->write(out_buffer1, chunk_len1);

NtQuerySystemTime(systime1);

elapsed_time = systime1->QuadPart - systime0->QuadPart;

write_flag = false ;

divident = static_cast<double>(chunk_len0 + chunk_len1) / 1.048576 ; // 1.024 * 1.024 = 1.048576; divident yield value 1000000 times greater then value in MB
divident *= 10 ; // I want 'speed' to be in MB/s
speed = divident / static_cast<double>(elapsed_time) ;
...


but it fails to link.

On MSDN, the
NtQuerySystemTime
documentation
says there is no associated import library and that I must use the
LoadLibrary()
and
GetProcAddress()
functions to dynamically link to
Ntdll.dll
. But I don't understand how to use those functions. Can someone please provide a code example of how to use those functions?

Answer

This is how you would be able to use this function.

HMODULE hNtDll = GetModuleHandleA("ntdll");
NTSTATUS (WINAPI *NtQuerySystemTime)(PLARGE_INTEGER) = 
    (NTSTATUS (WINAPI*)(PLARGE_INTEGER))GetProcAddress(hNtDll, "NtQuerySystemTime");