Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

c++ - Requesting administrator privileges at run time

Is it possible to get a C++ application running in Windows to request administrator privileges from the operating system at run time?

I know it can be done at compile time, but can't seem to find anywhere whether it can be done at run time.

Thanks for your help!

EDIT: What if I want the current instance to have elevated privileges? For example, I might have data stored in memory which I want to keep.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want the application to always elevate, you can give it a manifest, either by building one in (not compiling technically) or by putting an external manifest in the same folder as the exe. If you want to decide, as a person, to run it elevated, you right click the exe or short cut and choose Run As Administrator. If you are launching it from code, then as @vcsjones comments, you use the runas verb when you launch that process. For example:

ShellExecute( NULL, 
    "runas",  
    "c:\windows\notepad.exe",  
    " c:\temp\report.txt",     
    NULL,                        // default dir 
    SW_SHOWNORMAL  
); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...