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

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

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

Post 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...
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
jacky
User
User
Posts: 66
Joined: Mon Jan 21, 2019 1:41 pm

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

Post 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)
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

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

Post by vmars316 »

Oh that's right cmd is just a program after all .
Thanks
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

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

Post 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", "")
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

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

Post 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()
Last edited by cas on Sat Jan 02, 2021 8:27 pm, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

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

Post by RASHAD »

Hi cas
Thanks for sharing

Code: Select all

SetSystemPowerState_(0,0) ;For hibernate

SetSystemPowerState_(1,0) ;For suspend
Egypt my love
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

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

Post by cas »

Yeah, i should have put #False instead of #True. Thanks for a correction.
Post Reply