From 7948b7bd37af601f579a5dfd97da24c6e2656bca Mon Sep 17 00:00:00 2001 From: Maksym Ochenashko Date: Wed, 28 Aug 2024 16:33:20 +0300 Subject: [PATCH] feature [windowslib]: add `GetProcessTimes` to the `ProcessThreadsApi` (#4035) --- .../windowslib/ProcessThreadsApiTest.scala | 41 +++++++++++++++++++ .../windows/ProcessThreadsApi.scala | 10 +++++ 2 files changed, 51 insertions(+) create mode 100644 unit-tests/native/src/test/scala/org/scalanative/testsuite/windowslib/ProcessThreadsApiTest.scala diff --git a/unit-tests/native/src/test/scala/org/scalanative/testsuite/windowslib/ProcessThreadsApiTest.scala b/unit-tests/native/src/test/scala/org/scalanative/testsuite/windowslib/ProcessThreadsApiTest.scala new file mode 100644 index 0000000000..4eeff69843 --- /dev/null +++ b/unit-tests/native/src/test/scala/org/scalanative/testsuite/windowslib/ProcessThreadsApiTest.scala @@ -0,0 +1,41 @@ +package org.scalanative.testsuite.windowslib + +import org.junit.Test +import org.junit.Assert._ + +import scala.scalanative.meta.LinktimeInfo.isWindows +import scala.scalanative.unsafe._ +import scala.scalanative.windows.MinWinBaseApi.FileTimeStruct +import scala.scalanative.windows.MinWinBaseApiOps._ +import scala.scalanative.windows.ProcessThreadsApi + +class ProcessThreadsApiTest { + + @Test def testGetProcessTimes(): Unit = if (isWindows) { + val creationTime = stackalloc[FileTimeStruct]() + val exitTime = stackalloc[FileTimeStruct]() + val kernelTime = stackalloc[FileTimeStruct]() + val userTime = stackalloc[FileTimeStruct]() + + val result = ProcessThreadsApi.GetProcessTimes( + ProcessThreadsApi.GetCurrentProcess(), + creationTime, + exitTime, + kernelTime, + userTime + ) + + assertTrue("result is false", result) + + assertTrue( + s"kernelTime [${kernelTime.fileTime}] < 0", + kernelTime.fileTime.toLong >= 0L + ) + + assertTrue( + s"userTime [${userTime.fileTime}] < 0", + userTime.fileTime.toLong >= 0L + ) + } + +} diff --git a/windowslib/src/main/scala/scala/scalanative/windows/ProcessThreadsApi.scala b/windowslib/src/main/scala/scala/scalanative/windows/ProcessThreadsApi.scala index a9ebb55b56..f3f5439ce7 100644 --- a/windowslib/src/main/scala/scala/scalanative/windows/ProcessThreadsApi.scala +++ b/windowslib/src/main/scala/scala/scalanative/windows/ProcessThreadsApi.scala @@ -3,6 +3,7 @@ package scala.scalanative.windows import scala.scalanative.unsafe.{Word => _, _} import scalanative.unsigned._ import HandleApi.Handle +import MinWinBaseApi.FileTimeStruct import WinBaseApi.SecurityAttributes @link("advapi32") @@ -59,6 +60,15 @@ object ProcessThreadsApi { extern def GetProcessId(handle: Handle): DWord = extern + + def GetProcessTimes( + handle: Handle, + creationTime: Ptr[FileTimeStruct], + exitTime: Ptr[FileTimeStruct], + kernelTime: Ptr[FileTimeStruct], + userTime: Ptr[FileTimeStruct] + ): Boolean = extern + def OpenThreadToken( thread: Handle, desiredAccess: DWord,