Page 1 of 1

How to convert .bat code to .pb GetCommandLine_() ?

Posted: Fri Jan 01, 2021 5:05 am
by vmars316
Hello,
I have a .bat that I use to Hibernate win10 (one-click) .

Code: Select all

cd c:\
shutdown /h
But I would like to turn it into a *.pb , so that it can have an icon to pin to the Taskbar .

I found this code

Code: Select all

; German forum: http://www.purebasic.fr/german/archive/viewtopic.php?t=1128&highlight=
; Author: Danilo
; Date: 25. May 2003
; OS: Windows
; Demo: No

lpCmdLine = GetCommandLine_() 
If lpCmdLine 
  A$ = PeekS(lpCmdLine) 
  MessageRequester("INFO","Commandline: "+A$,0) 
EndIf 
There is nothing in Help about "GetCommandLine" .
So I need some help to figure out how t run .bat code as commandLine_() .

Thanks for your Help...

Re: How to convert .bat code to .pb GetCommandLine_() ?

Posted: Fri Jan 01, 2021 10:17 am
by jacky
GetCommandLine_ is not a PB command, but a Windows API command

But what does that have to do with command line parameters? You have just posted the content of a bat file...

Code: Select all

RunProgram("cmd", "/c cd C:\ && shutdown /h", GetTemporaryDirectory(), #PB_Program_Hide)

Re: How to convert .bat code to .pb GetCommandLine_() ?

Posted: Fri Jan 01, 2021 6:39 pm
by vmars316
Oh that's right cmd is just a program after all .
Thanks

Re: How to convert .bat code to .pb GetCommandLine_() ?

Posted: Fri Jan 01, 2021 9:22 pm
by Marc56us
Hi,

Shutdown is an exe and is in system folder, so no need to use batch file

Code: Select all

RunProgram("shutdown", "/h", "")

Re: How to convert .bat code to .pb GetCommandLine_() ?

Posted: Sat Jan 02, 2021 5:22 pm
by cas
If you do not want to run any external program (shutdown.exe) then you can do it directly with SetSystemPowerState_():

Code: Select all

EnableExplicit

Procedure _SetShutdownPrivilege()
  ;returns: 0=success; else error code
  Protected error=0
  Protected hToken
  Protected p.TOKEN_PRIVILEGES
  If OpenProcessToken_(GetCurrentProcess_(), #TOKEN_QUERY|#TOKEN_ADJUST_PRIVILEGES, @hToken) = 0
    ProcedureReturn GetLastError_()
  EndIf
  p\PrivilegeCount = 1
  p\Privileges[0]\Attributes = #SE_PRIVILEGE_ENABLED
  If LookupPrivilegeValue_(0, "SeShutdownPrivilege", @p\Privileges[0]\Luid)=0
    error=GetLastError_()
    CloseHandle_(hToken)
    ProcedureReturn error
  EndIf
  If AdjustTokenPrivileges_(hToken, 0, @p, 0, 0, 0)<>#ERROR_SUCCESS
    error=GetLastError_()
  EndIf
  CloseHandle_(hToken)
  ProcedureReturn error
EndProcedure

Procedure HibernatePC()
  ;returns: 0=success; else error code
  _SetShutdownPrivilege()
  SetSystemPowerState_(#False,0)
  ProcedureReturn GetLastError_()
EndProcedure

HibernatePC()

Re: How to convert .bat code to .pb GetCommandLine_() ?

Posted: Sat Jan 02, 2021 5:56 pm
by RASHAD
Hi cas
Thanks for sharing

Code: Select all

SetSystemPowerState_(0,0) ;For hibernate

SetSystemPowerState_(1,0) ;For suspend

Re: How to convert .bat code to .pb GetCommandLine_() ?

Posted: Sat Jan 02, 2021 8:26 pm
by cas
Yeah, i should have put #False instead of #True. Thanks for a correction.