DroopyLib

Developed or developing a new product in PureBasic? Tell the world about it.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Re: DroopyLib

Post by GeoTrail »

Excellent Droopy.
Thanks for your great work.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
rule
User
User
Posts: 14
Joined: Sat Sep 02, 2006 7:56 pm
Location: Oostzaan / Netherlands

Re: DroopyLib

Post by rule »

Hi Droopy,
I've found a problem in the Week (date.l) routine.
If I input the date 2013,1,1,0,0,0 then the result is week 0.
So I started looking for a solution and found it in a newly created page on the Dutch wikipedia (weeknummer).
This example, I converted to PureBasic. Note that this only applies to European calendar weeks, the Japanese and American calendars work differently.

Code: Select all

Procedure My_Week(date.l)
  var_year = Year(date)
  var_month = Month(date)
  var_day = Day(date)

UK01 = DayOfYear(Date(var_year, var_month, var_day, 0, 0, 0)) - 4

UK02.i = UK01/7

UK03 = DayOfWeek(Date(var_year, var_month, var_day, 0, 0, 0))
If UK03 = 0
  UK03 = 7
EndIf

UK04 = DayOfWeek(Date(var_year-1, 1, 4, 0, 0, 0))
If UK04 = 0
  UK04 = 7
EndIf

UK05 = DayOfYear(Date(var_year-1, 12, 31, 0, 0, 0)) - 4

UK06.i = UK05/7

UK07 = DayOfWeek(Date(var_year-1, 12, 31, 0, 0, 0))
If UK07 = 0
  UK07 = 7
EndIf

UK08 = DayOfWeek(Date(var_year, 1, 4, 0, 0, 0))
If UK08 = 0
  UK08 = 7
EndIf

UK09 = DayOfWeek(Date(var_year+1, 1, 4, 0, 0, 0))
If UK09 = 0
  UK09 = 7
EndIf

If UK07 < UK08
  UK10 = 1
ElseIf UK07 < UK04
  UK10 = UK06 + 2
Else
  UK10 = UK06 + 1
EndIf

If var_day < 4 And var_month = 1
  If UK03 < UK08
    UK11=1
  Else
    UK11=UK10
  EndIf
ElseIf var_day > 28 And UK03 < UK09
  UK11 = 1
ElseIf UK03 < UK08
  UK11 = UK02 + 2
Else
  UK11=UK02+1
EndIf

ProcedureReturn UK11
EndProcedure
I think this is useful for your library.

Greetings,
Roel.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: DroopyLib

Post by Bisonte »

Calendarweek ? I use some "old" code from german forum and it works perfect...

http://purebasic.fr/german/viewtopic.php?p=71675#p71675
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Sveinung
Enthusiast
Enthusiast
Posts: 142
Joined: Tue Oct 07, 2003 11:03 am
Location: Bergen, Norway

Re: DroopyLib

Post by Sveinung »

Is there any problems with the process commands and PB 5.11.
Can't get them to work :(

Regards
Sveinung
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Re: DroopyLib

Post by GeoTrail »

Why not just update PB, Sveinung?
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
Sveinung
Enthusiast
Enthusiast
Posts: 142
Joined: Tue Oct 07, 2003 11:03 am
Location: Bergen, Norway

Re: DroopyLib

Post by Sveinung »

GeoTrail wrote:Why not just update PB, Sveinung?
Pretty sure 5.11 is the lates release ??

GetPidProcess
KillProcess
SearchProcess
RunOnlyOneInstance
HandleToFileName
PidToFileName
WaitProgramInitialisation

These are the cpmmands I can't get to work.

Sveinung
User avatar
le_magn
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Aug 24, 2005 12:11 pm
Location: Italia

Re: DroopyLib

Post by le_magn »

Sveinung wrote:
GeoTrail wrote:Why not just update PB, Sveinung?
Pretty sure 5.11 is the lates release ??

GetPidProcess
KillProcess
SearchProcess
RunOnlyOneInstance
HandleToFileName
PidToFileName
WaitProgramInitialisation

These are the cpmmands I can't get to work.

Sveinung
I have tested GetPidProcess
KillProcess
SearchProcess
RunOnlyOneInstance

with pb 5.11 and working ok
Image
User avatar
Sveinung
Enthusiast
Enthusiast
Posts: 142
Joined: Tue Oct 07, 2003 11:03 am
Location: Bergen, Norway

Re: DroopyLib

Post by Sveinung »

@le_magn

I have tested on win8 x64 and win 7sp1 x64....same result, getpidprocess always returns 0.

Sveinung
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: DroopyLib

Post by Bisonte »

Maybe you try to get a PID of a x64 program within the PBx86 ?
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Sveinung
Enthusiast
Enthusiast
Posts: 142
Joined: Tue Oct 07, 2003 11:03 am
Location: Bergen, Norway

Re: DroopyLib

Post by Sveinung »

Bisonte wrote:Maybe you try to get a PID of a x64 program within the PBx86 ?
Stupid me! Forgot to mention that it was compiling to X64. SORRY!! :oops: :oops: :oops:
*Bow head in shame*

I can get the pid from any process compiling to x86 but not x64. In x86 mode I can get the pid of both 32 and 64 bits Applications.
So it seems to be the x64 mode that is the problem.

Sveinung
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Re: DroopyLib

Post by Droopy »

i'm looking for these bugs
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: DroopyLib

Post by Bisonte »

With this, you don't need OpenLibrary()... x86 and x64, proved in an tailbite-userlibrary and its case insensitive if you want ;)

Code: Select all

Procedure GetPID(Name$, Mode = 0) ; Returns the ProcessID the tasks Name$ or 0 ( Mode = #True : Case Insensitiv)
  Protected Process.PROCESSENTRY32
  Protected ProcSnap
  
  ProcSnap = CreateToolhelp32Snapshot_(#TH32CS_SNAPPROCESS, 0)
  If ProcSnap <> 0
    Process\dwsize=SizeOf(Process)
    Process32First_(ProcSnap, Process)
    While Process32Next_(ProcSnap, Process) > 0
      If Mode <> 0
        If UCase(PeekS(@Process\szExeFile)) = UCase(Name$)
          ProcedureReturn Process\th32ProcessID
          Break
        EndIf
      Else
        If PeekS(@Process\szExeFile) = Name$
          ProcedureReturn Process\th32ProcessID
          Break
        EndIf      
      EndIf
      
    Wend
  EndIf  
EndProcedure
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: DroopyLib

Post by jpd »

Hi,

oops Bisonte was faster... :-)

but this example here works a little bit different.

Droopy can change this as necessary on the DroopyLib.

Here some Process Procedure that works on Win2k and Higher x86/x64 Ascii and Unicode

the only different is on the GetPIDProcess Procedure
the orig droopy this found only the first entry, this one is more similar as the Searchprocess but return the pid (list) of all found matches

Code: Select all

Prototype ProcessFirst(Snapshot, Process)
Prototype ProcessNext(Snapshot, Process)
Global ProcessFirst.ProcessFirst
Global ProcessNext.ProcessNext
Global NewList PID()




Prototype GetModuleFileNameEx(hProcess,dwFlags, lpExeName, lpdwSize) 
Global  GetModuleFileNameEx.GetModuleFileNameEx

Prototype  GetProcessImageFileName(hProcess, lpImageFileName, nSize)
Global GetProcessImageFileName.GetProcessImageFileName

Prototype QueryFullProcessImageName(hProcess, dwFlags, lpExeName, lpdwSize) 
Global QueryFullProcessImageName.QueryFullProcessImageName 


Declare.s PidToFileName(PID)

ProcedureDLL.s HandleToFileName(hWnd.l) ; Returns the file Path and Name of the exe that owns the specified Window Handle
  
  Protected Name.s=""
  
  ;// Handle can be found with WindowsEnum()
  
  Pid.l=0 
  GetWindowThreadProcessId_( hWnd, @Pid ) 
  Name.s=PidToFileName(Pid)
  
  
  ProcedureReturn Name 
EndProcedure 


Procedure.s JunctiontoPathname(junction.s)
  
  Protected *Buffer,i,C,nr
  Protected Drives.s,Drive.s,DosDeviceName.s,LogicalName.s
  If FindString(junction,"Device\LanmanRedirector",1)
    LogicalName=RemoveString(junction,"Device\LanmanRedirector")
    ProcedureReturn LogicalName  
  EndIf
  *Buffer = AllocateMemory(#MAX_PATH*SizeOf(character))
  For i = 0 To GetLogicalDriveStrings_(#MAX_PATH*SizeOf(character), *Buffer)
    C = PeekB(*Buffer +i)
    If C <> 0
      drives.s = UCase(drives) + Chr(C)
    EndIf
  Next
  DosDeviceName = Space(#MAX_PATH*SizeOf(character))
  nr=CountString(drives,"\")
  For x=1 To nr 
    drive.s=StringField(drives,x,"\")
    QueryDosDevice_(drive, @DosDeviceName, #MAX_PATH*SizeOf(character))  
    If FindString(junction,DosDeviceName,1)
      LogicalName=ReplaceString(junction, DosDeviceName, Drive)
      Break
    EndIf
  Next    
  ProcedureReturn LogicalName
  
EndProcedure

Procedure.s PidToFileName(PID)
  Protected hProcess
  Protected libID
  Protected lpdwSize
  Protected PathFileName.s = ""
  Protected DosDeviceName.s
  
  If OSVersion() => #PB_OS_Windows_Vista
    libID = OpenLibrary(#PB_Any, "kernel32.dll") 
  ElseIf     OSVersion() => #PB_OS_Windows_2000 Or OSVersion() < #PB_OS_Windows_Vista
    libID = OpenLibrary(#PB_Any, "psapi.dll")
  EndIf
  
  CompilerIf #PB_Compiler_Unicode
    
    If OSVersion() = #PB_OS_Windows_2000
      GetModuleFileNameEx=GetFunction(libID,"GetModuleFileNameExW") 
    ElseIf OSVersion() = #PB_OS_Windows_XP Or OSVersion() = #PB_OS_Windows_Server_2003
      GetProcessImageFileName = GetFunction(libID, "GetProcessImageFileNameW")
    ElseIf OSVersion() => #PB_OS_Windows_Vista
      QueryFullProcessImageName = GetFunction(libID, "QueryFullProcessImageNameW") 
    EndIf
    
  CompilerElse
    
    If OSVersion() = #PB_OS_Windows_2000
      GetModuleFileNameEx=GetFunction(libID,"GetModuleFileNameExA") 
    ElseIf OSVersion() = #PB_OS_Windows_XP Or OSVersion() = #PB_OS_Windows_Server_2003
      GetProcessImageFileName = GetFunction(libID, "GetProcessImageFileNameA")
    ElseIf OSVersion() => #PB_OS_Windows_Vista
      QueryFullProcessImageName = GetFunction(libID, "QueryFullProcessImageNameA") 
    EndIf
    
  CompilerEndIf 
  
  If GetModuleFileNameEx <> 0 Or GetProcessImageFileName <> 0 Or QueryFullProcessImageName <>0
    
    If OSVersion() = #PB_OS_Windows_2000
      hProcess = OpenProcess_( #PROCESS_QUERY_INFORMATION | #PROCESS_VM_READ, 0, PID ); 
    ElseIf OSVersion() = > #PB_OS_Windows_XP 
      hProcess = OpenProcess_(#PROCESS_QUERY_INFORMATION, 0, PID) 
    EndIf
    
    If hProcess
      PathFileName = Space(#MAX_PATH*4)
      lpdwSize=#MAX_PATH*4
      
      If OSVersion() = #PB_OS_Windows_2000
        
        GetModuleFileNameEx(hProcess,0,@PathFileName,@lpdwSize ) 
        
      ElseIf OSVersion() = #PB_OS_Windows_XP Or OSVersion() = #PB_OS_Windows_Server_2003
        
        GetProcessImageFileName(hProcess, @PathFileName, #MAX_PATH)
        PathFileName=JunctiontoPathname(PathFileName)
        
      ElseIf OSVersion() => #PB_OS_Windows_Vista
        
        QueryFullProcessImageName(hProcess, DosDevice, @PathFileName, @lpdwSize)
        
      EndIf
      
      CloseHandle_(hProcess)
      
    EndIf
    
    CloseLibrary(libID)
    
  EndIf
  
  ProcedureReturn PathFileName
  
EndProcedure 



;{/*
; MessageRequester("PID Process to Filename","Explorer.exe"+" --> "+PidToFileName(GetPidProcess("Explorer.exe")))
;}

           
ProcedureDLL GetHandle(WindowsName.s)
  retour= FindWindow_(0, WindowsName)
  ProcedureReturn retour
EndProcedure


Procedure.b GetPIDProcess(Name.s)  
  
  
  Protected ProcLib
  Protected ProcName.s
  Protected Process.PROCESSENTRY32
  Protected x
  Protected RetVal=#False
  Name=UCase(Name.s)
  ProcLib= OpenLibrary(#PB_Any, "Kernel32.dll") 
  If ProcLib
    CompilerIf #PB_Compiler_Unicode
      ProcessFirst           = GetFunction(ProcLib, "Process32FirstW") 
      ProcessNext            = GetFunction(ProcLib, "Process32NextW") 
    CompilerElse
      ProcessFirst           = GetFunction(ProcLib, "Process32First") 
      ProcessNext            = GetFunction(ProcLib, "Process32Next") 
    CompilerEndIf
    If  ProcessFirst And ProcessNext 
      Process\dwSize = SizeOf(PROCESSENTRY32) 
      Snapshot =CreateToolhelp32Snapshot_(#TH32CS_SNAPALL,0)
      If Snapshot 
        ProcessFound = ProcessFirst(Snapshot, Process) 
        x=1
        While ProcessFound 
          ProcName=PeekS(@Process\szExeFile)
          ProcName=GetFilePart(ProcName)
          If UCase(ProcName)=UCase(Name) 
            AddElement(PID())
            PID()=Process\th32ProcessID
            RetVal=#True
          EndIf
          
          ProcessFound = ProcessNext(Snapshot, Process) 
        x=x+1  
        Wend 
      EndIf 
      CloseHandle_(Snapshot) 
    EndIf 
    CloseLibrary(ProcLib) 
  EndIf 
  ProcedureReturn RetVal
  
EndProcedure

ret=GetPidProcess("purebasic.exe")
If ret  
  ForEach PID()
    Debug PID()
  Next
EndIf


hWin=GetHandle("Unbenannt - Editor")
If hWin
  Debug HandleToFileName(hWin)
EndIf


PB 5.10 Windows 7 x64 SP1
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Re: DroopyLib

Post by Droopy »

Hello,

New version avalaible :
23/05/13 : Library 5.11.001
Compiled with PB 5.11 / TB 1.4.9
WaitProgramInitialisation() Bug fixed
SearchProcess() Function rewritten
PidToFileName() Function rewritten
HandleToFileName() Function rewritten
GetPidProcess() Function rewritten
GetPidProcessEx() Function added
@Sveinung : RunOnlyOneInstance/KillProcess : no bug found
@Bisonte and jpd : Thanks a lot wink:

Week() bug will be fixed in the next release.

Regards
User avatar
Sveinung
Enthusiast
Enthusiast
Posts: 142
Joined: Tue Oct 07, 2003 11:03 am
Location: Bergen, Norway

Re: DroopyLib

Post by Sveinung »

Thank you Droppy! :D

Sveinung
Post Reply