-1

I want to change FileTime to SystemTime, but the program always collapsed, WHY?

Thanks in advance.

FILETIME *Kernel_Time;
HANDLE Process = OpenProcess ( PROCESS_ALL_ACCESS, FALSE, 0);
GetProcessTimes (Process, NULL, NULL, Kernel_Time, NULL);
SYSTEMTIME  *Sys_Time;
FileTimeToSystemTime (Kernel_Time, Sys_Time);
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • 1
    Your `Kernel_Time` pointer needs to point to a valid `FILETIME` structure. It's just a pointer at the moment. – David Nov 17 '13 at 19:03
  • 1
    By the way, what is "collapsed"? An access violation? Please be as specific as possible when describing a problem. – David Nov 17 '13 at 19:03
  • @David M It shows up *.exe has stopped working (zh-tw:已經停止運作) – Kevin Dong Nov 17 '13 at 19:09
  • 1
    Are you running under a debugger? If you are using an IDE, try finding a "Run > Run with Debugging" or "Debug > Run" menu item, or something similar. Its name may change depending on what program you're using. The IDE will then pause your program when an exception occurs. – David Nov 17 '13 at 19:12
  • @David M I didn't use any IDE, This is the command to compile this program mingw32-gcc.exe -O3 -c "WINAPI.c" -o "WINAPI.o" mingw32-gcc.exe -o "WINAPI.exe" "WINAPI.o" – Kevin Dong Nov 17 '13 at 19:15
  • 1
    Then you'll have to use [the command-line debugger.](http://stackoverflow.com/questions/4671900/how-do-i-use-the-mingw-gdb-debugger-to-debug-a-c-program-in-windows) (Erk. You're doing this the hard way!) Try downloading a good C++ IDE for Windows, such as [Visual C++](http://www.visualstudio.com/downloads/download-visual-studio-vs) (Express is free, you probably want Express 2013 for Windows *Desktop*) or [C++ Builder](http://www.embarcadero.com/products/cbuilder/downloads) (no free version, unfortunately, but much nicer than VC++ for building user interfaces.) – David Nov 17 '13 at 19:20
  • 1
    @David M Appreciate the advice; I think I will try some IDE for C/C++. :-) – Kevin Dong Nov 17 '13 at 19:23
  • @DavidM *"Doing this the hard way"* is the prescribed way to learn how this all works. Read Charles Petzold's excellent article [Does Visual Studio Rot the Mind?](http://www.charlespetzold.com/etc/doesvisualstudiorotthemind.html) for a very thorough analysis and rationale. – IInspectable Nov 17 '13 at 20:04
  • Interesting article... and I admit to liking his thesis, "is Visual Studio sapping our programming intelligence rather than augmenting it?" Despite that I feel suspicious, since when I've seen these articles before they are easily rebuttable. I'll read it in detail later. To me, here and now, the key step isn't GUI vs CMD - *it's using a debugger at all.* I'd prefer to introduce someone to an easy to use one. – David Nov 17 '13 at 20:19

2 Answers2

3

You don't allocate memory for the result. It is probably easier to allocate it on the stack, and use the address in the function call like this:

FILETIME Kernel_Time;
HANDLE Process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 0);
GetProcessTimes(Process, NULL, NULL, &Kernel_Time, NULL);
SYSTEMTIME Sys_Time;
FileTimeToSystemTime(&Kernel_Time, &Sys_Time);
David
  • 13,360
  • 7
  • 66
  • 130
wimh
  • 15,072
  • 6
  • 47
  • 98
1

Your call to OpenProcess fails:

If the specified process is the System Process (0x00000000), the function fails and the last error code is ERROR_INVALID_PARAMETER.

You should consider checking return values. Your application crashes, since you are passing a random pointer (Sys_Time) to FileTimeToSystemTime and Kernel_Time to GetProcessTimes.

For each invocation you need to allocate memory. Automatic variables suffice in this case:

FILETIME Kernel_Time = { 0 };
...
GetProcessTimes( Process, NULL, NULL, &Kernel_Time, NULL );
IInspectable
  • 46,945
  • 8
  • 85
  • 181