Launching Win 10 apps from command-line

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Launching Win 10 apps from command-line

Post by BarryG »

Anyone had experience with launching Win 10 apps from the command-line? My problem is this: An example app is "Samsung Flow" for transferring files via wi-fi from Win 10 to my Samsung phone. If I look at the exe name from the app's window, I get this:

Code: Select all

C:\Program Files\WindowsApps\SAMSUNGELECTRONICSCoLtd.SamsungFlux_4.7.2.0_x64__wyx1vj98g3asy\SamsungFlow.exe
You'd think that running that would start the Samsung app, but it doesn't because the OS denies access to the "WindowsApps" folder (and gives an error of "Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item.").

However, I have since found out I can run the Samsung app with this command-line:

Code: Select all

start shell:AppsFolder\SAMSUNGELECTRONICSCoLtd.SamsungFlux_wyx1vj98g3asy!App
So what I need to work out is how to marry the launched window's exe (at top), with the correct "Shell" command-line per the bottom. I see there's a common part of "_wyx1vj98g3asy" but not sure if that's applicable. I've Googled but can't find a way. Anyone done this sort of thing before?
Last edited by BarryG on Fri Jan 01, 2021 2:53 pm, edited 3 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Launching Win 10 apps from command-line

Post by RASHAD »

Hi BarryG
Try :
# 1:

Code: Select all

  AppVerb$ = "runas"
  AppName$ = "SamsungFlow.exe"
  AppDir$  = "C:\Program Files\WindowsApps\SAMSUNGELECTRONICSCoLtd.SamsungFlux_4.7.2.0_x64__wyx1vj98g3asy"
     
  shExecInfo.SHELLEXECUTEINFO
  shExecInfo\cbSize=SizeOf(SHELLEXECUTEINFO)
  shExecInfo\fMask=#Null
  shExecInfo\hwnd=#Null;
  shExecInfo\lpVerb=@AppVerb$
  shExecInfo\lpFile=@AppName$
  shExecInfo\lpDirectory=@AppDir$
  shExecInfo\nShow=#SW_NORMAL
 
  ShellExecuteEx_(shExecInfo)
# 2:

Code: Select all

ShellExecute_(#Null, @"runas", @"SamsungFlow.exe", @"", @"C:\Program Files\WindowsApps\SAMSUNGELECTRONICSCoLtd.SamsungFlux_4.7.2.0_x64__wyx1vj98g3asy", #SW_NORMAL)
Egypt my love
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Launching Win 10 apps from command-line

Post by BarryG »

Both fail, Rashad. I thought I had a solution a moment ago but it also failed. I'll keep researching.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Launching Win 10 apps from command-line

Post by breeze4me »

A simple way.

Code: Select all

RunProgram("shell:AppsFolder\Microsoft.WindowsCalculator_8wekyb3d8bbwe!App")
A complicated way. :mrgreen:

Code: Select all

; Run Windows 8 Metro Application from Desktop Application 
; https://auahdark687291.blogspot.com/2015/01/

; Automate launching Windows 10 UWP apps
; https://docs.microsoft.com/ko-kr/windows/uwp/xbox-apps/automate-launching-uwp-apps

; How do I programmatically launch a Windows Store App from my Desktop App?
; https://www.xspdf.com/questions/40562.shtml



#AO_NONE = 0
#AO_DESIGNMODE = 1
#AO_NOERRORUI = 2
#AO_NOSPLASHSCREEN = 4

Interface IApplicationActivationManager Extends IUnknown
  ActivateApplication(*appUserModelId, *arguments, ActivateOptions, *processId)
  ActivateForFile(*appUserModelId, *itemArray, *verb, *processId)
  ActivateForProtocol(*appUserModelId, *itemArray, *processId)
EndInterface


Prototype ptCoAllowSetForegroundWindow(*pUnk, lpvReserved)

Procedure AllowSetForegroundApp(*IAAM)
  Protected Result = #S_FALSE
  Protected CoAllowSetForegroundWindow_.ptCoAllowSetForegroundWindow
  Protected Lib = OpenLibrary(#PB_Any, "Ole32.dll")
  
  If Lib
    
    CoAllowSetForegroundWindow_ = GetFunction(Lib, "CoAllowSetForegroundWindow")
    
    If CoAllowSetForegroundWindow_
      Result = CoAllowSetForegroundWindow_(*IAAM, 0)
    EndIf
    
    CloseLibrary(Lib)
  EndIf
  
  ProcedureReturn Result
EndProcedure




Define AAM.IApplicationActivationManager
Define PID


If CoInitializeEx_(0, #COINIT_APARTMENTTHREADED) = #S_OK
  
  If CoCreateInstance_(?CLSID_ApplicationActivationManager, 0, #CLSCTX_LOCAL_SERVER, ?IID_IApplicationActivationManager, @AAM) = #S_OK
    
    AllowSetForegroundApp(AAM)
    
    ; Find the Application User Model ID of an installed app
    ; https://docs.microsoft.com/ko-kr/windows/configuration/find-the-application-user-model-id-of-an-installed-app
    
    If AAM\ActivateApplication(@"Microsoft.WindowsCalculator_8wekyb3d8bbwe!App", @"", #AO_NONE, @PID) = #S_OK
      
      Debug PID
      
    EndIf
    
    AAM\Release()
    
  EndIf
  
  CoUninitialize_()
  
EndIf




DataSection
  ; ShObjIdl.h
  IID_IApplicationActivationManager:    ;"2e941141-7f97-4756-ba1d-9decde894a3d"
  Data.l $2e941141
  Data.w $7f97, $4756
  Data.b $ba, $1d, $9d, $ec, $de, $89, $4a, $3d
  
  CLSID_ApplicationActivationManager:   ;"45ba127d-10a8-46ea-8ab7-56ea9078943c"
  Data.l $45ba127d
  Data.w $10a8, $46ea
  Data.b $8a, $b7, $56, $ea, $90, $78, $94, $3c
EndDataSection
Last edited by breeze4me on Fri Jan 01, 2021 1:58 pm, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Launching Win 10 apps from command-line

Post by RASHAD »

Try the next
And breeze4me snippet
And fryquez snippet (OS Specific » Windows)
Then report

Code: Select all

#admin_CommandFlag = "/admin"
#SEE_MASK_UNICODE = $00004000
Global  pipe_hProcess

Procedure RunProgramAdminHandle(windowid, exe.s, params.s, workingdir.s)
  Protected shellex.SHELLEXECUTEINFO
  shellex\cbSize = SizeOf(SHELLEXECUTEINFO)
  shellex\fMask = #SEE_MASK_NOCLOSEPROCESS
  shellex\hwnd = windowid
  shellex\lpVerb = @"runas"
  shellex\lpFile = @exe
  shellex\lpParameters = @params
  shellex\lpDirectory = @workingdir
  shellex\nShow = #SW_HIDE
  ShellExecuteEx_(shellex)
  ProcedureReturn shellex\hProcess
EndProcedure

Procedure ProcessRunning(hProcess)
  Protected exitcode.l
  GetExitCodeProcess_(hProcess, @exitcode)
  If exitcode = #STILL_ACTIVE
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf 
EndProcedure

If ProgramParameter(0) = #admin_CommandFlag  
  Import ""
    AttachConsole(dwProcessId)
  EndImport
  
  FreeConsole_()
  AttachConsole(Val(ProgramParameter(1)))
  
  RunProgram("cmd", "", GetCurrentDirectory(), #PB_Program_Wait)
  End 
EndIf 

pipe_hProcess = RunProgramAdminHandle(GetForegroundWindow_(), "C:\Program Files\WindowsApps\SAMSUNGELECTRONICSCoLtd.SamsungFlux_4.7.2.0_x64__wyx1vj98g3asy\SamsungFlow.exe", #admin_CommandFlag+" "+Str(GetCurrentProcessId_()), "C:\Program Files\WindowsApps")

If pipe_hProcess
  While ProcessRunning(pipe_hProcess)
    Delay(20)
  Wend
Else
  Debug "pipe_hprocess empty"
EndIf

CloseHandle_(pipe_hProcess)
Egypt my love
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Launching Win 10 apps from command-line

Post by BarryG »

@breeze4me: Your longer code isn't relevant to me, because I need it to run non-Win10 apps, not native Win 10 apps.

@Rashad: Your last example with "pipe_hProcess" didn't work for me either - no app was launched. Sorry to report that!

But, I worked it out! I just had to massage the returned exe path to match the relevant app's folder name, and then used breeze4me's single RunProgram() line to launch it. Done and dusted.

The only thing I couldn't work out is if "C:\Program Files\WindowsApps" can be returned programmatically, rather than hard-coded. Can someone with a non-English Windows PC confirm if this path name is the same on there? Thanks. Here's how I've coded it for now:

Code: Select all

Global win10appdir$=GetUserDirectory(#PB_Directory_Programs) ; C:\Program Files (x86)\
a=FindString(win10appdir$," (")
If a
  win10appdir$=Left(win10appdir$,a-1)
EndIf
win10appdir$+"\WindowsApps\" ; C:\Program Files\WindowsApps\
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Launching Win 10 apps from command-line

Post by breeze4me »

BarryG wrote:Can someone with a non-English Windows PC confirm if this path name is the same on there?
Yes, it is the same in Korean Windows 10.

Code: Select all

Debug GetEnvironmentVariable("ProgramW6432") + "\WindowsApps\"


Edit:
proper way :?:

Code: Select all

#RRF_RT_REG_SZ = $00000002
#RRF_SUBKEY_WOW6464KEY = $00010000
;#RRF_SUBKEY_WOW6432KEY = $00020000

Prototype ptRegGetValueW(hkey, *lpSubKey, *lpValue, dwFlags.l, *pdwType, *pvData, *pcbData)

Define buffer.s{128}, ByteSize

If OpenLibrary(0, "Advapi32.dll")
  
  ; Windows Vista, Windows XP Professional x64+
  RegGetValueW_.ptRegGetValueW = GetFunction(0, "RegGetValueW")
  
  If RegGetValueW_
    
    ByteSize = 254
    
    If RegGetValueW_(#HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Appx", @"PackageRoot", #RRF_RT_REG_SZ | #RRF_SUBKEY_WOW6464KEY, 0, @buffer, @ByteSize) = #ERROR_SUCCESS
      Debug ByteSize
      Debug buffer
    EndIf
    
  EndIf
  
  CloseLibrary(0)
EndIf
Last edited by breeze4me on Fri Jan 01, 2021 7:25 pm, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Launching Win 10 apps from command-line

Post by BarryG »

Thanks for the "ProgramW6432" env var! That's just what I was after. Out of curiosity, what does that show as the path on your Korean PC?
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Launching Win 10 apps from command-line

Post by breeze4me »

It's the same as English Windows.
C:\Program Files
See this link for other countries.
https://en.wikipedia.org/wiki/Program_Files
Last edited by breeze4me on Fri Jan 01, 2021 4:50 pm, edited 1 time in total.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Launching Win 10 apps from command-line

Post by BarryG »

Thanks. And happy new year to you!
Post Reply