Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add code for automatic restart as administrator
- Loading branch information
Showing
2 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
#!perl | ||
use Mjollnir; | ||
use Getopt::Long qw(:config gnu_getopt no_auto_abbrev); | ||
|
||
if ($^O eq 'MSWin32') { | ||
require Win32; | ||
if (! Win32::IsAdminUser()) { | ||
Getopt::Long::GetOptions( | ||
'respawn' => \(my $respawn), | ||
); | ||
require Win32::API; | ||
|
||
my @parameters = ($0, @ARGV); | ||
for my $param (@parameters) { | ||
if ( $param =~ s/(["\\])/\\$1/g || $param =~ / / ) { | ||
$param = qq{"$param"}; | ||
} | ||
} | ||
my $parameters = join ' ', @parameters; | ||
|
||
my $ShellExecuteEx = Win32::API->new('shell32.dll', 'ShellExecuteEx', ['P'], 'N') | ||
or die 'Get ShellExecuteEx: ' . Win32::FormatMessage(Win32::GetLastError()); | ||
|
||
my $lpVerb = pack 'A*', 'runas'; | ||
my $lpFile = pack 'A*', $^X; | ||
my $lpParameters = pack 'A*', $parameters; | ||
|
||
my $args = pack 'LLLPPPPLLLPLLLL'; | ||
$args = pack 'LLLPPPPLLLPLLLL', length $args, ($respawn ? 0x8000 : 0), 0, $lpVerb, $lpFile, $lpParameters, 0, 1, 0, 0, 0, 0, 0, 0, 0; | ||
|
||
my $ret = $ShellExecuteEx->Call($args) | ||
or die 'Call ShellExecuteEx: ' . Win32::FormatMessage(Win32::GetLastError()); | ||
exit; | ||
} | ||
} | ||
|
||
require Mjollnir; | ||
my $options = {}; | ||
Getopt::Long::GetOptions( $options, qw( | ||
listen|l=s@ | ||
db_file|db|d=s | ||
device|dev|D=s | ||
respawn | ||
)) || die "usage: $0 [ -l<host>:<port> ] [ -D<device> ] [ -d<database> ]\n"; | ||
Mjollnir->run($options); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters