Skip to content

Commit

Permalink
添加按钮申请Administrator权限,免去用户手动申请的繁琐
Browse files Browse the repository at this point in the history
  • Loading branch information
dybb8999 committed Apr 8, 2019
1 parent 4e9b55b commit a5bb6fe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
73 changes: 67 additions & 6 deletions 驱动工具/DriverToolFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ wxBEGIN_EVENT_TABLE(CDriverToolFrame, wxFrame)
EVT_BUTTON(ID_BTN_START, CDriverToolFrame::OnStart)
EVT_BUTTON(ID_BTN_STOP, CDriverToolFrame::OnStop)
EVT_BUTTON(ID_BTN_UNINSTALL, CDriverToolFrame::OnUnInstall)
EVT_BUTTON(ID_BTN_NEED_ADMIN, CDriverToolFrame::OnStartElevatedProcess)
EVT_THREAD(THREAD_SERVER_CONTROL_COMPLETE, CDriverToolFrame::OnServiceControlComplete)
EVT_CHECKBOX(ID_CHK_WINDOWTOP, CDriverToolFrame::OnWindowTop)
EVT_DROP_FILES(CDriverToolFrame::OnDropFile)
Expand Down Expand Up @@ -161,6 +162,15 @@ std::map<wxString, wxString> g_GUIDMap;
m_pBtnUninstall->SetFont(font);
m_pMiddleStaticBoxSizer->Add(m_pBtnUninstall, 1, wxALIGN_CENTRE_VERTICAL | wxALL, 5);

if (IsUserAnAdmin() == false)
{
m_pBtnAuthNeed = new wxButton(this, ID_BTN_NEED_ADMIN, wxT("请求管理员权限"));
m_pBtnAuthNeed->SetFont(font);
m_pBtnAuthNeed->SetAuthNeeded(true);
m_pMiddleStaticBoxSizer->Add(m_pBtnAuthNeed, 1, wxALIGN_CENTRE_VERTICAL | wxALL, 5);
SetStatusText(wxT("当前程序没有管理员权限,请使用管理员权限启动"));
}

m_pBottomBoxSizer = new wxStaticBoxSizer(wxHORIZONTAL, this, wxT("状态"));
m_pStaticStatus = new wxStaticText(this, wxID_ANY, wxT("状态:"));
m_pStaticStatus->SetFont(font);
Expand Down Expand Up @@ -195,12 +205,6 @@ std::map<wxString, wxString> g_GUIDMap;
this->SetSizer(m_pMainBoxSizer);
m_pMainBoxSizer->SetMinSize(wxSize(600, 300));
m_pMainBoxSizer->SetSizeHints(this);

//检测Admin权限
if (IsUserAnAdmin() == FALSE)
{
SetStatusText(wxT("当前程序没有管理员权限,请使用管理员权限启动"));
}
}

CDriverToolFrame::~CDriverToolFrame()
Expand Down Expand Up @@ -388,6 +392,63 @@ void CDriverToolFrame::OnUnInstall(wxCommandEvent & event)
} while (0);
}

void CDriverToolFrame::OnStartElevatedProcess(wxCommandEvent & event)
{
TCHAR* pModulePath = (TCHAR*)calloc(32767, sizeof(TCHAR));
DWORD dwRet = 0;
do
{
this->Show(false);
if (pModulePath == nullptr)
{
break;
}

dwRet = GetModuleFileName(GetModuleHandle(NULL), pModulePath, 32767);
if (dwRet == 0)
{
break;
}

SHELLEXECUTEINFO sei = { sizeof(SHELLEXECUTEINFO) };

// Ask for privileges elevation.
sei.lpVerb = TEXT("runas");

// Pass the application to start with high privileges.
sei.lpFile = pModulePath;

// Pass the command line.
sei.lpParameters = nullptr;

// Don't forget this parameter otherwise the window will be hidden.
sei.nShow = SW_SHOWNORMAL;

ShellExecuteEx(&sei);
if (GetLastError() != 0)
{
break;
}

//这里内存其实可以不用释放
if (pModulePath != nullptr)
{
free(pModulePath);
pModulePath = nullptr;
}

ExitProcess(0);
} while (false);

if (pModulePath != nullptr)
{
free(pModulePath);
pModulePath = nullptr;
}

this->Show(true);
}

void CDriverToolFrame::OnWindowTop(wxCommandEvent & event)
{
if (m_pChkBoxWindowTop->IsChecked() == true)
Expand Down
3 changes: 3 additions & 0 deletions 驱动工具/DriverToolFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum
ID_BTN_START,
ID_BTN_STOP,
ID_BTN_UNINSTALL,
ID_BTN_NEED_ADMIN,
ID_EDT_SHOW,
ID_BTN_MORE,
ID_BTN_SUPPORT_MINIFILTER,
Expand Down Expand Up @@ -89,6 +90,7 @@ class CDriverToolFrame :
wxButton* m_pBtnStart;
wxButton* m_pBtnStop;
wxButton* m_pBtnUninstall;
wxButton* m_pBtnAuthNeed;

//wxBoxSizer* m_pBottomBoxSizer;
wxStaticBoxSizer *m_pBottomBoxSizer;
Expand Down Expand Up @@ -168,6 +170,7 @@ class CDriverToolFrame :
void OnStart(wxCommandEvent &event);
void OnStop(wxCommandEvent &event);
void OnUnInstall(wxCommandEvent &event);
void OnStartElevatedProcess(wxCommandEvent &event);
void OnWindowTop(wxCommandEvent &event);
void OnWindowMove(wxMoveEvent &event);
void OnStartChange(wxCommandEvent & event);
Expand Down

0 comments on commit a5bb6fe

Please # to comment.