johnscillieri / psutil-nim

Port of python psutil to nim
MIT License
62 stars 21 forks source link

Type Issues #13

Open SynSnipe opened 3 years ago

SynSnipe commented 3 years ago

Type Issues in Windows side of psutil. I assume something changed in winim but I am uncertain. Linux compiles and runs without issues.

Nim Version

nim -v
Nim Compiler Version 1.4.4 [Windows: amd64]
Compiled at 2021-02-23
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release

winim version

nimble install winim
Downloading https://github.com/khchen/winim using git
  Verifying dependencies for winim@3.6.0
 Installing winim@3.6.0
   Building winim/winim/winimx.exe using c backend
    Prompt: winim@3.6.0 already exists. Overwrite? [y/N]
    Answer: N

psutil version

nimble install psutil
Downloading https://github.com/juancarlospaco/psutil-nim using git
  Verifying dependencies for psutil@0.6.0
 Installing psutil@0.6.0
    Prompt: psutil@0.6.0 already exists. Overwrite? [y/N]
    Answer: N

compilation output

C:\Users\user\processlist>nim c mytest.nim
Hint: used config file 'C:\nim-1.4.4\config\nim.cfg' [Conf]
Hint: used config file 'C:\nim-1.4.4\config\config.nims' [Conf]
...................................................................................................
C:\Users\user\.nimble\pkgs\psutil-0.6.0\psutil\psutil_windows.nim(151, 30) Error: type mismatch: got <HANDLE, HMODULE, array[0..259, TCHAR], DWORD>
but expected one of:
proc GetModuleBaseName(hProcess: HANDLE; hModule: HMODULE; lpBaseName: LPWSTR;
                       nSize: DWORD): DWORD
  first type mismatch at position: 3
  required type for lpBaseName: LPWSTR
  but expression 'szProcessName' is of type: array[0..259, TCHAR]

expression: GetModuleBaseName(hProcess, hMod, szProcessName, cast[DWORD](len(szProcessName)))

The issues are not limited to this proc. I tried to run down my list of issues that I had as a result of trying to create a windows process list. Unfortunately the type issues go a bit deeper than I understand at this point.

SynSnipe commented 3 years ago

Simple Code that I was trying to compile...

import psutil

proc main : void =
    echo "psutil test"
    let pidList = pids()
    echo $pidList

when isMainModule:
    main()

Again works on Linux but not on Windows.

johnscillieri commented 3 years ago

Sorry for the issue, i'll take a look soon-ish and provide an update.

IvanScheers commented 3 years ago

Hi, I'm having the same issue.

IvanScheers commented 3 years ago

I know how to solve the issue, and did it in the code on my PC, but don't know how to edit the code here, otherwise I would.

Anyway:

In psutil_windows.nim, everwhere you get a LPWSTR error the var should be declared as wstring, for a LPSTR error, declare the var as cstring. These variables were declared as TCHAR or char arrays Example code:

##### var szProcessName: array[MAX_PATH, TCHAR]
    var szProcessName: wstring

##### var filename: array[MAX_PATH, char]
    var filename: cstring

##### var wcUser: array[512, TCHAR]
##### var wcDomain: array[512, TCHAR]
    var wcUser: wstring
    var wcDomain: wstring

It is in several places in the code that the declarations should be changed. It got broken because of a change in winim.

BTW pid_names() doesn't work, it returns a seq with PIDs and a seq with empty strings. I don't need that, so did not check why.

Mind that processexists() is case sensitive_ ! If you want it case insensitive find the code and change to

if name.toLowerAscii() == processName.toLowerAscii():
            exists = true