News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

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

Vortex

#15
Hi Dan,

The console in my example is created by the function AllocConsole. Regarding morgot's code, you need to run his code under a debugger to view the output of OutputDebugString :

QuoteIf the application has no debugger and the system debugger is not active, OutputDebugString does nothing

https://docs.microsoft.com/en-us/windows/win32/api/debugapi/nf-debugapi-outputdebugstringa

http://masm32.com/board/index.php?topic=6968.0

The inkey macro's job in my code is to keep the console open so the user can read the output and press a key to resume the execution of the application :

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


Removing inkey here is inconvenient as the user will see only a flashing window.

Regarding the statement invoke :

invoke is one of the post powerfull directives of Masm and compatible assemblers. The job of this macro is to eliminate the unncessary typing of the "push" sequences. Comparing the two approaches :

    invoke  MessageBox,0,\
            ADDR message,\
            ADDR caption,0


    push    0
    push    OFFSET caption
    push    OFFSET message
    push    0
    call    MessageBox


https://docs.microsoft.com/en-us/cpp/assembler/masm/invoke?view=msvc-160

Typing a lof of pushes can be annoying. Consider this example, CreateWindowEx taking 12 parameters :

    invoke  CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
            WS_OVERLAPPEDWINDOW or WS_VISIBLE,100,\
            100,500,400,NULL,NULL,\
            hInst,NULL


Why morgot prefered push to invoke ? That's a matter of personel preference and there is nothing wrong. In some coding practices, pushing can be more effective than invoking but for the moment those exceptions are not important.

Here is another version of my example :

.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

message     db "Click OK to display the console window.",0
message2    db "Hello from the console.",13,10
            db "Please hit a key to continue.",0
message3    db "Click OK to terminate the application.",0
caption     db "Hello",0

.code

start:

    invoke  MessageBox,0,\
            ADDR message,\
            ADDR caption,0

    invoke  AllocConsole
    invoke  StdOut,ADDR message2

    invoke  wait_key ; \masm32\m32lib\wait_key.asm

;   we could also type call wait_key

    push    MB_OK           ; MB_OK = 0
    push    OFFSET caption
    push    OFFSET message3
    push    0
    call    MessageBox

    invoke  ExitProcess,0

END start


EDIT : Reading Timo's message, the introduction of the invoke statement above is corrected. Macro changed to directive.

TimoVJL

For OutputDebugString()
https://docs.microsoft.com/en-us/sysinternals/downloads/debugview
May the source be with you

jj2007

Quote from: Vortex on January 30, 2021, 09:03:21 PMinvoke is one of the post powerfull buit-in macros of Masm and compatible assemblers. The job of this macro is to eliminate the unncessary typing of the "push" sequences

The invoke macro does also type checking, at least for 32-bit code.

TimoVJL

Quote from: jj2007 on January 30, 2021, 11:24:39 PM
The invoke macro does also type checking, at least for 32-bit code.
In ml.exe not a macro, a directive
https://docs.microsoft.com/en-us/cpp/assembler/masm/invoke?view=msvc-160
May the source be with you

morgot

#19
Dan, see it

https://www.masm32.com/board/index.php?topic=5354.0
* removed non working link

this is very simple and usefull library/

Also read the book
https://codernet.ru/books/assembler/assembly_language_for_x86_processors/
http://asmirvine.com/

this is the best book for Masm

or tell in topic, what you need, I can write simple program..
Sorry for the bad English

Dan-TheStarman

#20
There was no point in what I'd asked here...

Dan, TheStarman.

Dan-TheStarman

#21
Program I had started here was based on a false assumption of how "ShellExecuteA" with "runas" was supposed to be used... It's NOT for use inside the program you want to run with elevated privileges!

Dan.

jj2007

include \masm32\include\masm32rt.inc ; plain Masm32 for the fans of pure assembler

.code
AppName db "Masm32:", 0

start: MsgBox 0, "Hello World, how are you?", addr AppName, MB_OK
exit

end start


If you don't like macros: invoke MessageBox, 0, addr message, addr title, MB_OK

There is a reason why Microsoft created the invoke macro in the 20th Century :cool:

Dan-TheStarman

#23
Quote from: jj2007 on February 03, 2021, 10:45:12 PM
If you don't like macros: invoke MessageBox, 0, addr message, addr title, MB_OK
There is a reason why Microsoft created the invoke macro in the 20th Century :cool:

jj, HSE answered the question! I did not realize "addr" was an actual required part of the command... still learning!

But now there's a much bigger issue I've run into both here and under VS2019:
Is there some kind of code we must use to tell the program to ignore all previous references to any windows??

Dan.

HSE

Line is:

invoke  MessageBoxA, 0, addr winText, addr winCapt, YESNO
Equations in Assembly: SmplMath

Dan-TheStarman

#25
Quote from: HSE on February 04, 2021, 08:42:47 AM
Line is:
invoke  MessageBoxA, 0, addr winText, addr winCapt, YESNO

Thank  you very much HSE! 

Dan.

HSE

Equations in Assembly: SmplMath

TouEnMasm

I have rewrite the first sample for 32 bits,and ... it work in 32 bits
Quote
.data
sModuleFileName db MAX_PATH dup (0)
runas db "runas",0
asAdminMsg db "Running as Administrator!",10,0
.code
manager proc      ;
   Local hconsole:DWORD,hservice:DWORD,retour:DWORD
   mov retour,0
   invoke OpenSCManager,0,0,GENERIC_READ OR  GENERIC_WRITE OR GENERIC_EXECUTE ;need to be admin to work
   mov hservice,eax
   .if eax == 0   ;not admin,do it
      
      ;invoke GetConsoleWindow
      ;mov hconsole, eax
      ;invoke ShowWindow,hconsole,SW_SHOWNORMAL
      invoke GetModuleFileName,NULL,addr sModuleFileName,MAX_PATH
      invoke ShellExecute,NULL,TXT("runas"),addr sModuleFileName,NULL,NULL,SW_SHOWNORMAL
      .if eax <= 32
         invoke RetrouveMessageErreur,addr sModuleFileName
      .endif   
      mov retour,1
   .else
      ; Is Administrator
      invoke CloseServiceHandle,hservice      
      invoke printf,addr asAdminMsg   
   .endif
   mov eax,retour
   ret
manager endp

return 0 ,it is admin user,just continue
return 1,quit the program

Fa is a musical note to play with CL

Dan-TheStarman

#28
Quote from: HSE on February 05, 2021, 12:37:47 AM
invoke ExitProcess, 0

Wishing I could just delete this!

Dan.

HSE

Quote from: Dan-TheStarman on February 05, 2021, 06:37:25 AM
What about the frustrating issue of: Why does program display Info window when answering "Yes" to the UAC runas Admin?
It's not the program.
Equations in Assembly: SmplMath