News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Run As Admin -- Must it be 64-bit?

Started by Dan-TheStarman, January 29, 2021, 06:47:24 AM

Previous topic - Next topic

Dan-TheStarman

I'll leave the following here, in case it may help someone else:

"AW" in this thread: http://masm32.com/board/index.php?topic=7755.0, provided some really nice code for popping-up the standard Windows 10 screen asking if his program could run as Admin. However, when looking at it (and his asadmin.exe), I found it was a 64-bit solution. Does Windows 10; latest 64-bit version, require such a program to be assembled as 64-bit? Even if it doesn't, WHERE do I get the masm32 equivalent for creating 64-bit executable programs?

If we can do this in a 32-bit program under Windows 10 64-bit, what would be the masm32 version of this code (which looks to me as if it might be something the latest Microsoft assembler could use? Obviously no "masm64rt.inc" here):


INCLUDELIB msvcrt.lib
printf PROTO :PTR, :VARARG
_getch PROTO
INCLUDELIB kernel32.lib
ExitProcess PROTO :DWORD
GetModuleFileNameA PROTO :PTR, :PTR, :DWORD
GetModuleHandleA PROTO :PTR
GetConsoleWindow PROTO

INCLUDELIB  Advapi32.lib
OpenSCManagerA PROTO :PTR, :PTR, :DWORD
CloseServiceHandle PROTO :PTR

INCLUDELIB shell32.lib
ShellExecuteA PROTO :PTR, :PTR, :PTR, :PTR, :PTR, :DWORD

INCLUDELIB user32.lib
ShowWindow PROTO :PTR, :DWORD

GENERIC_READ EQU 80000000h
GENERIC_WRITE EQU 40000000h
GENERIC_EXECUTE EQU 20000000h
SW_SHOWNORMAL EQU 1

.data

sModuleFileName db 256 dup (0)
runas db "runas",0
asAdminMsg db "Running as Administrator!",10,0

.code

main proc
sub rsp, 38h

mov r8d, GENERIC_READ OR  GENERIC_WRITE OR GENERIC_EXECUTE
mov rdx, 0
mov rcx, 0
call OpenSCManagerA
cmp rax, 0
ja @F ; Already Administrator?

call GetConsoleWindow
mov rcx, rax
mov edx, 0 ; Hide the window
call ShowWindow
mov rcx, 0
mov rdx, offset sModuleFileName
mov r8d, 256
call GetModuleFileNameA
mov rcx, 0
mov rdx, offset runas
mov r8, offset sModuleFileName
mov r9, 0
mov rax, 0
mov [rsp+20h], rax
mov eax, SW_SHOWNORMAL
mov dword ptr [rsp+28h], eax
call ShellExecuteA
jmp short @exit
@@: ; Is Administrator
mov rcx, rax
call CloseServiceHandle
mov rcx, offset asAdminMsg
call printf
call _getch
@exit:
mov ecx, 0
call ExitProcess
main endp

end


morgot

You must check Integrity level , no "open SCM"
and you have errors in parametres function.

You can use 32bit. It works in Win10x64

.686                   
.model flat, stdcall
option casemap :none

include \masm32\include\windows.inc
include \masm32\macros\macros.asm
uselib kernel32,user32,shell32,advapi32

.data

sModuleFileName db 256 dup (0)
runas db "runas",0
asAdminMsg db "Running as Administrator!",10,0

.code

main proc

push GENERIC_READ OR  GENERIC_WRITE OR GENERIC_EXECUTE
push 0
push 0
call OpenSCManagerA
cmp eax,0
jnz @f

push 256
push offset sModuleFileName
push 0
call GetModuleFileNameA


push SW_SHOWNORMAL
push 0
push 0
push offset sModuleFileName
push offset runas
push 0
call ShellExecuteA
jmp short @exit

@@: ; Is Administrator


call CloseServiceHandle
push offset asAdminMsg
call OutputDebugStringA

@exit:
push 0
call ExitProcess
main endp

end main
Sorry for the bad English

Dan-TheStarman

#2
Thank you very much "morgot" for the 32-bit code,

   I was able to assemble and link that code just fine, however, I have no idea where to place my code after or wherever in here! The original program left the Console window open with a message (because it had that "_getch" function in it) which is where I was going to add some snippets to see if they function as expected... But your code immediately closes the window, even though I added 10 of these "wait_key" MACROs at various locations in the code!):

inkey MACRO
      call wait_key
      print chr$(13,10)
      ENDM


It always closes the window and I can't see anything... as soon as I press the "Yes" or "No" button to the "Run As Admin" question it closes the windows!

I had intended to print a message to my users, informing them that a copy process was successful and, of course, keep the console window open until they read it and then pressed a key.

TIA,
Dan-TheStarman.

jj2007

Dan,
Open a DOS prompt and launch it from there, so that you can see the output.

Dan-TheStarman

Quote from: jj2007 on January 29, 2021, 07:27:50 PM
Dan,
Open a DOS prompt and launch it from there, so that you can see the output.
That does not work... the code opens and closes its own console window apart from wherever you run the program; whether in a regular (not Admin) Command Prompt (CMD) or PowerShell (not Admin) Prompt. Did you actually assemble, link and try it?
(And even if it did work there, that would defeat the purpose of it requiring a message be shown to my users later on.)

Vortex

Hi Dan,

Assuming that your main application has a graphical interface :


.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc
include     \masm32\macros\macros.asm

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib
includelib  \masm32\lib\msvcrt.lib

.data

.code

start:

    invoke  MessageBox,0,\
            CTXT("Click OK to display the console window."),\
            CTXT("Hello"),0

    invoke  AllocConsole
    invoke  StdOut,CTXT("Hello from the console.",13,10)
    inkey
    invoke  ExitProcess,0

END start


Building the application :

\masm32\bin\ml /c /coff TestCons.asm
\masm32\bin\polink /SUBSYSTEM:WINDOWS TestCons.obj


AllocConsole opens a new console window and the output of StdOut is directed to this window.

jj2007

Quote from: Dan-TheStarman on January 29, 2021, 08:42:55 PMDid you actually assemble, link and try it?

No, it's 64-bit code that works only with a non-standard installation that I don't have, sorry.

If you really want to see the messages, launch a debugger.

hutch--

Dan,

You cannot mix 32 and 64 bit MASM code, win32 and win64 are different animals. Write the code as 32 bit OR 64 bit but do not try to mix them.

morgot

#8
Dan-TheStarman,
you can use kip irvine library , that have simple procedures for Masm (simplier that in C).

Or standart Masm32 (see help dir in masm32 directory)
Sorry for the bad English

mineiro

hello sir Dan-TheStarman;
I forgot command line parameters to console window remain in screen, maybe /c or /k. Can you try? The last windows that I really play seriouos was XP, don't know if this continue working.
Copy and paste link below because last ")" is being ignored
https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490880(v=technet.10)

Batch files have PAUSE command, so console window can stay at screen, maybe a try.

I remember now that you can redirect output of a program to a text file. Like:

@echo off
myprogram > myfile.txt
notepad myfile.txt
PAUSE

I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Dan-TheStarman

Quote from: jj2007 on January 29, 2021, 10:23:37 PM
Quote from: Dan-TheStarman on January 29, 2021, 08:42:55 PMDid you actually assemble, link and try it?
No, it's 64-bit code that works only with a non-standard installation that I don't have, sorry.

jj, you really need to start reading the replies! "morgot" provided all the 32-bit code which assembled and linked just fine using masm32 from hutch... my last question to him was how to keep the window open... which it certainly appears you replied to, so then I wrote that no matter what I'd tried, it would always close! Go assemble and link "morgot"'s code... it is 32-bit masm32 code. Dan-TheStarman

Dan-TheStarman

Quote from: hutch-- on January 29, 2021, 10:26:31 PM
Dan,
You cannot mix 32 and 64 bit MASM code, win32 and win64 are different animals. Write the code as 32 bit OR 64 bit but do not try to mix them.
hutch, "morgot" already provided the 32-bit code above in his first reply to me which works just fine in your IDE, etc. Dan.

hutch--

 :biggrin:

Don't worry Dan, we are just trying to keep you safe, sound and coding at the speed of light.  :winking:

jj2007

Quote from: Dan-TheStarman on January 30, 2021, 12:02:45 PM
Quote from: jj2007 on January 29, 2021, 10:23:37 PM
Quote from: Dan-TheStarman on January 29, 2021, 08:42:55 PMDid you actually assemble, link and try it?
No, it's 64-bit code that works only with a non-standard installation that I don't have, sorry.

jj, you really need to start reading the replies! "morgot" provided all the 32-bit code

Sorry, I thought you were referring to your own code.

Dan-TheStarman

#14
Quote from: Vortex on January 29, 2021, 10:17:39 PM
Hi Dan,
. . .

include     \masm32\macros\macros.asm
. . .
    inkey
    invoke  ExitProcess,0

Hello Vortex,

   Thank you for your example.... works fine here. I may eventually place everything that needs to be said to users in graphical windows; forget the console, however my PROBLEM above is very specific:

   I do not understand why placing "inkey" in the code from "morgot" (such as right after "call OutputDebugStringA" or just before the "call ExitProcess") does absolutely nothing to keep the console window open that is created by that "call"?! His code has the same "include \masm32\macros\macros.asm" line yours does... all I can guess is: That "inkey" needs to be inside the (or any?) function you "call"; not outside of it?  I also saw your code uses "invoke" for almost everything, whereas morgot's code is apparently more granular? (using a number of individual Assembly instructions apart from the "call"s).

Dan.