문제

I am writing a Qt application that needs to call system programs (netsh) and run them as administrator.

However, QProcess, QDesktopServices and system() don't allow me to run the application as administrator (not even with runas).

The only solution that I found is to use ShellExecute, but it does not even open the program.

My code is:

#ifdef Q_OS_WIN { ShellExecute(0, LPCWSTR("runas"), LPCWSTR("netsh wlan start hostednetwork"), 0, 0, SW_SHOWNORMAL); }

I have also tried to use other options, such as open and tried to run other programs, such as Notepad (notepad.exe) and Control Panel (control.exe), nothing worked.

I have also tried to add an manifest file and nothing was solved.

Do I miss something in my code? (examples are welcome).

도움이 되었습니까?

해결책

LPCWSTR("runas") - this is incorrect, you typecast string to widestring, and probably ShellExecute will return error and does not start an application. Specify "L" prefix instead. Also, you need to split command and parameters, "netsh wlan start hostednetwork" will not work as command name. Use it like this:

ShellExecute(0, L"runas", L"netsh", L"wlan start hostednetwork", 0, SW_SHOWNORMAL);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top