Reputation: 257
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?
Upvotes: 1
Views: 1388
Reputation: 11
#include <stdio.h>
#include <windows.h>
typedef NTSYSAPI (CALLBACK *LPNTQUERYSYSTEMTIME)(PLARGE_INTEGER);
void main(void)
{
PLARGE_INTEGER SystemTime;
SystemTime = (PLARGE_INTEGER) malloc(sizeof(LARGE_INTEGER));
HMODULE hNtDll = GetModuleHandleA("ntdll");
LPNTQUERYSYSTEMTIME fnNtQuerySystemTime = (LPNTQUERYSYSTEMTIME)GetProcAddress(hNtDll, "NtQuerySystemTime");
if(fnNtQuerySystemTime){
printf("found NtQuerySystemTime function at ntdll.dll address:%p\n",fnNtQuerySystemTime);
fnNtQuerySystemTime(SystemTime);
printf("%llx\n", SystemTime->QuadPart);
}
free(SystemTime);
}
Upvotes: 1
Reputation: 5607
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");
Upvotes: 1