공부중
[VC++]권한이 필요한 프로세스를 생성할 경우. 본문
일단 상황은 이렇다.
ConsoleApplicationUAC는 권한상승을 필요로 하는 프로그램이고
UACProgramCall은 위의 ConsoleApplicationUAC를 CreateProcess를 통해서 실행하는 프로그램이다.
ConsoleApplicationUAC는 실행하면 권한 상승 요청을 묻고 승인한 뒤에 실행이 가능하다.
특별한것은 없고 이 출력문 하나 하고 끝난다.
이제 저 프로그램을 CreateProcess를 통해서 실행해보자. 코드는 아래와 같다.
#include <iostream>
#include <Windows.h>
int main()
{
std::cout << "UAC ProgramCall!!" << std::endl;
TCHAR commandLine[] = TEXT("ConsoleApplicationUAC.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL,
&si, // Pointer to STARTUPINFO structure.
&pi)) // Pointer to PROCESS_INFORMATION
{
DWORD ErrorCode = GetLastError();
std::cout << "Error : " << ErrorCode << std::endl;
system("pause");
return 1;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
std::cout << "Success" << std::endl;
system("pause");
return 0;
}
일단 실행하면 다음과 같이 나온다.
GetLastError()를 통해서 받아온 에러는 740번이다. MSDN을 참고해보면...
https://learn.microsoft.com/ko-kr/windows/win32/debug/system-error-codes--500-999-
시스템 오류 코드(500-999)(WinError.h) - Win32 apps
WinError.h 헤더 파일에 정의되고 개발자를 위한 오류 코드 500-999에 대해 설명합니다.
learn.microsoft.com
ERROR_ELEVATION_REQUIRED 740(0x2E4) 요청된 작업에는 권한 상승이 필요합니다. |
요청 작업에 권한 상승이 필요하다고 나온다.
이와 같은 내용은 아래의 페이지에 또 설명이 되어있다.
https://learn.microsoft.com/ko-kr/previous-versions/windows/it-pro/windows-7/cc722422(v=ws.10)
Using the ElevateCreateProcess Fix
Table of contents Using the ElevateCreateProcess Fix Article 06/02/2010 2 minutes to read In this article --> Applies To: Windows 7, Windows Vista This section includes information about using the ElevateCreateProcess compatibility fix, including the assoc
learn.microsoft.com
요약하자면 더이상 CreateProcess으로 권한이 높은 프로세스를 실행 할 수없도록 수정했으니까
ShellExecute API를 사용하도록 애플리케이션을 수정해야 한다 라는 내용이다.
https://learn.microsoft.com/ko-kr/windows/win32/api/shellapi/nf-shellapi-shellexecutew
ShellExecuteW function (shellapi.h) - Win32 apps
Performs an operation on a specified file. (ShellExecuteW)
learn.microsoft.com
ShellExecute에서 lpOperation에 runas를 넘겨주면 관리자로 실행하게 된다.
https://learn.microsoft.com/ko-kr/windows/win32/api/shellapi/nf-shellapi-shellexecuteexw
ShellExecuteExW function (shellapi.h) - Win32 apps
Performs an operation on a specified file. (ShellExecuteExW)
learn.microsoft.com
예제로 작성한 새로운 코드는 ShellExecuteEx를 사용했다
ShellExecuteEx는 SHELLEXECUTEINFO 구조체를 넘겨주는데 구조체 정보는 아래와 같이 있다.
https://learn.microsoft.com/ko-kr/windows/win32/api/shellapi/ns-shellapi-shellexecuteinfow
SHELLEXECUTEINFOW (shellapi.h) - Win32 apps
Contains information used by ShellExecuteEx. (Unicode)
learn.microsoft.com
lpVerb에 runas를 넘겨줄 경우 프로그램을 관리자로 실행하게 된다.
새로운 코드는 아래와 같다.
#include <iostream>
#include <Windows.h>
int main()
{
std::cout << "UAC ProgramCall!! With Shell" << std::endl;
TCHAR commandLine[] = TEXT("ConsoleApplicationUAC.exe");
SHELLEXECUTEINFO ShellExecuteInfo;
ZeroMemory(&ShellExecuteInfo, sizeof(ShellExecuteInfo));
ShellExecuteInfo.cbSize = sizeof(ShellExecuteInfo);
ShellExecuteInfo.fMask = SEE_MASK_UNICODE | SEE_MASK_NOCLOSEPROCESS;
ShellExecuteInfo.lpFile = commandLine;
ShellExecuteInfo.lpVerb = TEXT("runas");
ShellExecuteInfo.nShow = SW_SHOW;
ShellExecuteInfo.lpParameters = NULL;
ShellExecuteEx(&ShellExecuteInfo);
DWORD ErrorCode = GetLastError();
std::cout << "Error : " << ErrorCode << std::endl;
system("pause");
return 0;
}
실행하면 UAC 상승권한을 묻는 창이 나오게 되고 실행 결과는 아래와 같이 나온다.
https://learn.microsoft.com/ko-kr/windows/win32/shell/launch
애플리케이션 시작(ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) - Win32 apps
애플리케이션에서 파일 개체를 찾은 후 다음 단계는 종종 어떤 방식으로든 작업을 수행하는 것입니다.
learn.microsoft.com
'Programing > C, C++' 카테고리의 다른 글
[C++] 생성자 호출순서 다시 돌아보기 (0) | 2024.11.27 |
---|---|
[C++] std::sort 사용시 invalid comparator가 발생한 경우. (0) | 2024.01.21 |
[C/C++] 배열 안에 자료형 크기가 다른 값으로 채우기 (0) | 2022.04.18 |
[MSVC]public 생성자를 만들었음에도 불구하고 링크에러가 나는 경우 (0) | 2021.05.24 |
[C/C++]유니코드로 콘솔출력 또는 파일출력이 동작하지 않는 경우.. (0) | 2021.05.02 |