How to check if the notepad.exe is running?

Just starting out? Need help? Post your questions and find answers here.
stmdbe2019
User
User
Posts: 89
Joined: Mon Aug 31, 2009 2:11 pm

How to check if the notepad.exe is running?

Post by stmdbe2019 »

How to check if the notepad.exe is running? Following code always return 0 instead of telling me if its running or not?

Code: Select all




Procedure GetPid(FiletoFind$)
  kernel32 = OpenLibrary(0, "Kernel32.dll")
  OpenLibrary(1, "psapi.dll")
  If kernel32
    CreateToolhelpSnapshot = GetFunction(0, "CreateToolhelp32Snapshot")
    ProcessFirst           = GetFunction(0, "Process32First")
    ProcessNext            = GetFunction(0, "Process32Next")
    If CreateToolhelpSnapshot And ProcessFirst And ProcessNext
      Process.PROCESSENTRY32\dwSize = SizeOf(PROCESSENTRY32)
      Snapshot = CallFunctionFast(CreateToolhelpSnapshot, #TH32CS_SNAPPROCESS, 0)
      If Snapshot
        ProcessFound = CallFunctionFast(ProcessFirst, Snapshot, Process)
        While ProcessFound
          PID = Process\th32ProcessID
          hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, 0, PID)
          If hProcess
            Name$ = Space(1024)
            CallFunction(1, "GetModuleFileNameExA", hProcess, 0, @Name$, Len(Name$))
            CloseHandle_(hProcess)
          EndIf
          Temp.s = PeekS(@Process\szExeFile)
          If Temp <> "[System Process]"
              ;AddGadgetItem(GD, - 1, Str(PID) + Chr(10) + Temp + Chr(10) + Name$)
              If UCase(Temp)=UCase(FiletoFind$)
              Debug Temp
              PIDFound=PID              
              EndIf

          EndIf
          ProcessFound = CallFunctionFast(ProcessNext, Snapshot, Process)
        Wend
      EndIf
      CloseHandle_(Snapshot)
    EndIf
  EndIf
  ProcedureReturn PIDFound
EndProcedure


Pid = GetPid("notepad.exe")
Debug (Pid)

-----
Registered PureBasic Coder.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: How to check if the notepad.exe is running?

Post by cas »

Replace this line:

Code: Select all

Temp.s = PeekS(@Process\szExeFile)
with:

Code: Select all

Temp.s = PeekS(@Process\szExeFile,-1,#PB_Ascii)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to check if the notepad.exe is running?

Post by Marc56us »

(A without API version)
How to check if the notepad.exe is running?

Code: Select all

C:\>tasklist | findstr notepad
notepad.exe                  27004 Console                    1    14 360 Ko
RunProgram("tasklist") ... (#PB_Program_Hide)

Code: Select all

...
While ProgramRunning(Compiler)
      If AvailableProgramOutput(Compiler)
      ...check for string "notepad"
... etc
:wink:
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: How to check if the notepad.exe is running?

Post by Kwai chang caine »

Marc56us wrote:RunProgram[/url]("tasklist") ... (#PB_Program_Hide)
Hello MARC56
Cool 8)

Code: Select all

Compiler = RunProgram("tasklist", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
Output$ = ""

If Compiler

 While ProgramRunning(Compiler)
 
  If AvailableProgramOutput(Compiler)
   Output$ + ReadProgramString(Compiler) + Chr(13)
  EndIf
  
 Wend
 
 Output$ + Chr(13) + Chr(13)
 Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
 
 CloseProgram(Compiler)
 
EndIf

Debug Output$
Why i have specific characters :|
I have try to add #PB_Program_Ascii or #PB_Ascii, or UNICODE or UTF8 without success :cry:
Nom de l'image PID Nom de la sessio Num�ro de s Utilisation
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 8 Ko
System 4 Services 0 456 Ko
smss.exe 344 Services 0 932 Ko
csrss.exe 540 Services 0 4�228 Ko
wininit.exe 632 Services 0 5�328 Ko
.....
.....
Exitcode: 0
ImageThe happiness is a road...
Not a destination
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to check if the notepad.exe is running?

Post by Marc56us »

Why i have specific characters :|
I have try to add #PB_Program_Ascii or #PB_Ascii, or UNICODE or UTF8 without success :cry:
Quote:
Nom de l'image PID Nom de la sessio Num�ro de s Utilisation
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 8 Ko
System 4 Services 0 456 Ko
smss.exe 344 Services 0 932 Ko
csrss.exe 540 Services 0 4�228 Ko
wininit.exe 632 Services 0 5�328 Ko
Can be solved with OemToChar_ , but this is API and I hate playing with directly API :twisted:
(Because et surtout parce que j'y pige que pouic au fouillis des API :mrgreen:)
:wink:
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: How to check if the notepad.exe is running?

Post by Mijikai »

Here's how it should look like (x64 & x86):

Code: Select all

Procedure.i GetPID(Process.s)
  Protected SnapShot.i
  Protected PE32.PROCESSENTRY32
  SnapShot = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS,#Null)
  If Not SnapShot = #INVALID_HANDLE_VALUE
    PE32\dwSize = SizeOf(PROCESSENTRY32)
    If Process32First_(SnapShot,@PE32)
      Repeat
        If PeekS(@PE32\szExeFile) = Process
          CloseHandle_(SnapShot)
          ProcedureReturn PE32\th32ProcessID
        EndIf
      Until Process32Next_(SnapShot,@PE32) = #False
    EndIf
    CloseHandle_(SnapShot)
  EndIf
EndProcedure
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: How to check if the notepad.exe is running?

Post by Kwai chang caine »

@Mijikai
Thanks, that works on W7 x86 8)
Marc56 wrote:Can be solved with OemToChar_
Thanks to your answer, i have search OEMTOCHAR and find the solution thanks to a FRYQUEZ code :wink:
The worst, it's in one of my personal thread, i have asked exactely this style of question the last year :oops:

Code: Select all

Compiler = RunProgram("tasklist", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide)
Output$ = ""

If Compiler

 While ProgramRunning(Compiler)
 
  If AvailableProgramOutput(Compiler)
     
   sOEM_in_unicode.s = ReadProgramString(Compiler, #PB_Ascii)
   iByteLength = Len(sOEM_in_unicode) + 2     
   sOem_in_Ascii.s = Space(iByteLength)
   PokeS(@sOem_in_Ascii, sOEM_in_unicode, -1, #PB_Ascii)
   sunicode.s = Space(iByteLength)
   OemToChar_(@sOem_in_Ascii, @sunicode)
   Output$ + sunicode + Chr(13)
   
  EndIf
 
 Wend
 
 Output$ + Chr(13) + Chr(13)
 Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
 
 CloseProgram(Compiler)
 
EndIf
It's when even strange, because the W7 Console not return the same numbers of specials characters than W10 :shock:
I have not tested this code on W10, but like it works on W7, i hope it's works on W10 too
ImageThe happiness is a road...
Not a destination
User avatar
mk-soft
Always Here
Always Here
Posts: 5336
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to check if the notepad.exe is running?

Post by mk-soft »

Simple way to found all running 'Notepad'

Small update with get text from notepad :mrgreen:

Code: Select all

hWnd = 0
cnt = 0
Repeat
  hWnd = FindWindowEx_(0, hWnd, @"NotePad", 0)
  If hwnd
    cnt + 1
    title.s = Space(512)
    GetWindowText_(hWnd, @title, 512)
    Debug "" + cnt + ": " + title
    hEdit = FindWindowEx_(hWnd, 0, @"Edit", 0)
    If hEdit
      len = SendMessage_(hEdit, #WM_GETTEXTLENGTH, 0, 0)
      If len
        text.s = Space(len)
        SendMessage_(hEdit, #WM_GETTEXT, len + SizeOf(character), @text)
        Debug text
      Else
        Debug "No text"
      EndIf
    Else
      Debug "Error: No hEdit"
    EndIf
  EndIf
Until hWnd = 0
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply