B4J Code Snippet How to add an "Elevated" Priveleges Application to Start-UP at Windows (add an app at windows tasks)

Hi there...

Don't Forget... This is a Windows Solution

Well I am sure for those need such a solution, this will help... until now I was using the Registry lib of hatzisn (simple and solid)... but when the .lnk or the standalone exe has Administrator Rights or RunAS Administrator - it will just pass our registry trick without running...

So the only solution (simple and solid) found is this and I am sharing you with you gus:
B4X:
Sub AddElevatedAppToTasks(TaskFolderAndNameofApp As String,apppath As String)
    Dim t1 As String=sys32run("schtasks.exe",Array("/create","/SC","ONLOGON","/TN",QUOTE & TaskFolderAndNameofApp & QUOTE,"/TR",QUOTE & apppath & QUOTE,"/RL","HIGHEST"))
    Log(t1)
End Sub

Sub DeleteElevatedAppFromTasks(TaskFolderAndNameofApp As String)
    Dim t1 As String=sys32run("schtasks.exe",Array("/delete","/TN",QUOTE & TaskFolderAndNameofApp & QUOTE,"/f"))
    Log(t1)
End Sub

Sub sys32run(app As String,params As List) As String
    Dim js As Shell
    js.Initialize("js", app, params)
    js.WorkingDirectory="c:\windows\system32"
    'If Main.ShellEncoding.Length>0 Then js.Encoding=Main.ShellEncoding
    Dim string1 As ShellSyncResult
    Dim string2 As String
    string1=js.RunSynchronous(-1)
    string2=string1.StdOut
    Return string2
End Sub

You can use it like this:
' This will add the app at Tasks with the name of exe produced by b4jpackager !
AddElevatedAppToTasks("Magma\mSupport",File.GetFileParent(File.DirApp) & "\" & "msupport.exe")

'This will remove it from Tasks...
DeleteElevatedAppFromTasks("Magma\mSupport")
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
"c:\windows" is also variable. How to get ?
 

MrKim

Well-Known Member
Licensed User
Longtime User
I am looking at this code with great interest as I have a dilemma it might solve, but I am not clear when and how to use this code.

I have a standalone executable program that needs to shell out and run an msaccess program. On some computers the rights are restricted and prevent this from happening. I need to fix this issue at program install wehn admin rights are available. Can this code be used for that?

Thanks
 

Magma

Expert
Licensed User
Longtime User
I am looking at this code with great interest as I have a dilemma it might solve, but I am not clear when and how to use this code.

I have a standalone executable program that needs to shell out and run an msaccess program. On some computers the rights are restricted and prevent this from happening. I need to fix this issue at program install wehn admin rights are available. Can this code be used for that?

Thanks
Hi there, this code is to add it at Schedule Tasks... but to do that you must have admin rights.... it is actually for running something automatically at startup...

My experience.... says that none code can be safe for that work!

You must make a "combination" of trick solutions....

1) You must make sure that your installation/setup of exe will run only with Admin Rights... So you must check Inno Setup and configuration of it!
2) You must run the app will execute AddElevatedAppToTasks("YourCompany\YourAPPName",File.GetFileParent(File.DirApp) & "\" & "yourapp.exe")
where Magma\msupport ... you can have your company-or-softwarefolder (it hasn't anything to do with directories-files) YourCompanyMrKim\YourAPPName\subapp....
3) Also you must make sure that yourapp.exe will run with admin rights, make it that with Inno Setup / elevating... or by right click and properties... run as Admin!
4) Sometimes need to add Security Properties and add the Admin user to it... have to with policy of system... that is another level...

5) I suggest to do until step 3... and inform end-user that will try to run it as Admin... if not succeded... user must find the way...

If you want i can share a configuration of Inno Setup using at my setup of apps... i want to have elevated priv.

ps: As long tried at Windows 10 and in 7 works...
 

Magma

Expert
Licensed User
Longtime User
For example mSupport (my Remote Administrator utility using MQTT) Inno Setup configuration:

B4X:
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "mSupport"
#define MyAppVersion "0.088"
#define MyAppPublisher "MAGMA Multimedia Productions"
#define MyAppURL "https://www.magma.gr"
#define MyAppExeName "msupport.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{xxxxxxxxxxxxxxxxxxxxxxxxx-yourappid}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
VersionInfoProductName="mSupport"
VersionInfoDescription="mSupport is a Remote Administration Tool"
AppCopyright="Magma Multimedia Productions"
DefaultDirName={autopf64}\{#MyAppName}
DisableProgramGroupPage=yes
; Uncomment the following line to run in non administrative install mode (install for current user only.)
; PrivilegesRequired=admin
OutputBaseFilename=mysetup
SetupIconFile=D:\B4J\AutoUpdate\B4J\Files\msup.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}";

[Files]
Source: "D:\tesssst\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "D:\tesssst\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files


[Icons]
Name: "{userdesktop}\mSupport"; Filename: "{app}\mSupport.exe"; \
  AfterInstall: SetElevationBit('{userdesktop}\mSupport.lnk')


[Code]

procedure SetElevationBit(Filename: string);
var
  Buffer: string;
  Stream: TStream;
begin
  Filename := ExpandConstant(Filename);
  Log('Setting elevation bit for ' + Filename);

  Stream := TFileStream.Create(FileName, fmOpenReadWrite);
  try
    Stream.Seek(21, soFromBeginning);
    SetLength(Buffer, 1);
    Stream.ReadBuffer(Buffer, 1);
    Buffer[1] := Chr(Ord(Buffer[1]) or $20);
    Stream.Seek(-1, soFromCurrent);
    Stream.WriteBuffer(Buffer, 1);
  finally
    Stream.Free;
  end;
end;

[Registry]
Root: "HKLM"; Subkey: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"; \
    ValueType: String; ValueName: "{app}\msupport.exe"; ValueData: "RUNASADMIN"; \
    Flags: uninsdeletekeyifempty uninsdeletevalue; MinVersion: 0,6.1

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: runascurrentuser nowait postinstall skipifsilent;

hope it helps...
 

Magma

Expert
Licensed User
Longtime User
the points... you must check at inno setup... are these:

B4X:
[Icons]
Name: "{userdesktop}\mSupport"; Filename: "{app}\mSupport.exe"; \
  AfterInstall: SetElevationBit('{userdesktop}\mSupport.lnk')
here: actually i want the shortcut have elevated priv.


B4X:
[Registry]
Root: "HKLM"; Subkey: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"; \
    ValueType: String; ValueName: "{app}\msupport.exe"; ValueData: "RUNASADMIN"; \
    Flags: uninsdeletekeyifempty uninsdeletevalue; MinVersion: 0,6.1
and here: i want specific .exe to have admin rights - if the use go at folder and run it from there...


and this is the code you need to add at a default inno config:
B4X:
[Code]

procedure SetElevationBit(Filename: string);
var
  Buffer: string;
  Stream: TStream;
begin
  Filename := ExpandConstant(Filename);
  Log('Setting elevation bit for ' + Filename);

  Stream := TFileStream.Create(FileName, fmOpenReadWrite);
  try
    Stream.Seek(21, soFromBeginning);
    SetLength(Buffer, 1);
    Stream.ReadBuffer(Buffer, 1);
    Buffer[1] := Chr(Ord(Buffer[1]) or $20);
    Stream.Seek(-1, soFromCurrent);
    Stream.WriteBuffer(Buffer, 1);
  finally
    Stream.Free;
  end;
end;

may be help you... that too:
B4X:
; Uncomment the following line to run in non administrative install mode (install for current user only.)
; PrivilegesRequired=admin
 
Top