Monday, December 14, 2009

Windows Win32 Timer or Time Counting

If you want to make a timer that only depends on the kernel, GetTickCount64 is an easy way to do it.


http://msdn.microsoft.com/en-us/library/ms724411%28VS.85%29.aspx


The code is simple and would look like this:


ULONGLONG Ticks = 0;

Ticks = GetTickCount64();

//
// some code to time
//

Ticks = GetTickCount64() - Ticks;


Note: GetTickCount64 returns the number of ms since the system was last booted. Theoretically you might have check for wrapping of the clock; however, 2^64 ms or ~49.7 days is a long time. If you are worried about that, you might consider other APIs that keep track of the full time with a ms resolution.

Friday, December 11, 2009

Running cmd.exe as SYSTEM on Win 7 and Vista

In Windows, being Administrator has its limitations. Running as SYSTEM is all powerful. It is kind of like being root in the UNIX world except you can just su or sudo to SYSTEM.

I have this lib that can run in kernel mode and in user mode. I would like to simulate a UM process using the lib doing the same things I would have it do in KM tp measure the performance difference. To solve this issue, I need to run the process as SYSTEM.

A while back, I remember doing exactly that in a sysintenrals class. I ran across this blog post that reminded me how to do it.

1. Download the sysinternals PSTools
2. Copy the files to somewhere in your path
3. In an admin cmd.exe run > psexec -i -s cmd.exe

That is it. Anything you run in the shell will be run as LOCAL SYSTEM.