想让程序开机启动有管理员权限,使用 Windows 服务吧!
来源:互联网 发布:博物馆软件 编辑:程序博客网 时间:2024/06/11 18:07
// 服务代码
// Author : langshanglibie
#include "tchar.h"#include <windows.h>#include <tlhelp32.h>#include <Wtsapi32.h>#pragma comment(lib, "Wtsapi32.lib")TCHAR RC_SERVICE_NAME[] = _T("MyFirstSvc");TCHAR RC_DISPLAY_NAME[] = _T("我的第一个服务");SERVICE_STATUS g_serviceStatus;SERVICE_STATUS_HANDLE g_hServiceStatus = NULL;DWORD g_mainThreadID;// 服务控制主函数,这里实现对服务的控制,当在服务管理器上停止或其它操作时,将会运行此处代码void WINAPI ServiceStrl(DWORD dwControl){ switch (dwControl) { case SERVICE_CONTROL_STOP:{// 更新服务状态g_serviceStatus.dwWin32ExitCode = 0; g_serviceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus (g_hServiceStatus, &g_serviceStatus);PostThreadMessage(g_mainThreadID, WM_CLOSE, 0, 0); DWORD resp = 0;WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, WTSGetActiveConsoleSessionId(), _T("服务已经停止"), 12, _T("服务已经停止"), 12, 0, 0, &resp, false);} break; case SERVICE_CONTROL_PAUSE: break; case SERVICE_CONTROL_CONTINUE: break; case SERVICE_CONTROL_INTERROGATE: break; case SERVICE_CONTROL_SHUTDOWN: break; default: break; }}bool WinShellExecuteEx(LPCWSTR lpOperation, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, DWORD* errorCode = NULL, ULONG fMask = 0, HWND hWnd = NULL, INT nShowCmd = SW_SHOWNORMAL){ SHELLEXECUTEINFO execInfo = { 0 }; execInfo.cbSize = sizeof(SHELLEXECUTEINFO); execInfo.fMask = fMask; execInfo.hwnd = (hWnd != NULL) ? hWnd : ::GetActiveWindow(); execInfo.lpVerb = lpOperation; execInfo.lpFile = lpFile; execInfo.lpParameters = lpParameters; execInfo.lpDirectory = lpDirectory; execInfo.nShow = nShowCmd; execInfo.hProcess = NULL; if(::ShellExecuteEx(&execInfo)) { return (execInfo.hInstApp > (HINSTANCE)32); } else { if(errorCode) { *errorCode = ::GetLastError(); } return false; }}// 工作函数void DoWord(){ static bool haveBeenWorking = false; if (haveBeenWorking) { return; } haveBeenWorking = true; // 这里放你的代码// ......// ......}// 服务主函数,这在里进行控制对服务控制的注册void WINAPI ServiceMain(){// 注册服务控制函数g_hServiceStatus = RegisterServiceCtrlHandler(RC_SERVICE_NAME, ServiceStrl);if (g_hServiceStatus == NULL){OutputDebugString(_T("RegisterServiceCtrlHandler failed!"));return;}// 设置服务状态g_serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;g_serviceStatus.dwCurrentState = SERVICE_RUNNING;g_serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;g_serviceStatus.dwWin32ExitCode = 0;g_serviceStatus.dwServiceSpecificExitCode = 0;g_serviceStatus.dwCheckPoint = 0;g_serviceStatus.dwWaitHint = 0;SetServiceStatus(g_hServiceStatus, &g_serviceStatus);// 服务的工作循环while (g_serviceStatus.dwCurrentState == SERVICE_RUNNING){ DoWord();DWORD resp = 0;WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, WTSGetActiveConsoleSessionId(), _T("服务正在运行"), 12, _T("服务正在运行"), 12, 0, 0, &resp, false);Sleep(10000);}}// 判断服务是否已经被安装bool IsInstalled(){// 打开服务控制管理器 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) {MessageBox(NULL, _T("打开服务控制管理器失败!"), RC_SERVICE_NAME, MB_OK);return false;}// 打开服务SC_HANDLE hService = OpenService(hSCM, RC_SERVICE_NAME, SERVICE_QUERY_CONFIG);if (hService == NULL){ CloseServiceHandle(hSCM);return false;}CloseServiceHandle(hService);CloseServiceHandle(hSCM); return true;}// 安装服务函数bool InstallService(){ if (IsInstalled()){ return true;}// 打开服务控制管理器 SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) { MessageBox(NULL, _T("打开服务控制管理器失败!"), RC_SERVICE_NAME, MB_OK); return FALSE; } // 获取应用程序路径TCHAR szFilePath[MAX_PATH] = _T(""); GetModuleFileName(NULL, szFilePath, MAX_PATH);// 创建服务 SC_HANDLE hService = CreateService(hSCM, RC_SERVICE_NAME, RC_DISPLAY_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szFilePath, NULL, NULL, _T(""), NULL, NULL); if (hService == NULL) {MessageBox(NULL, _T("创建服务失败!"), RC_SERVICE_NAME, MB_OK);CloseServiceHandle(hSCM); return false; } CloseServiceHandle(hService); CloseServiceHandle(hSCM); return true;}// 删除服务函数bool UninstallService(){ if (!IsInstalled()){ return true;} SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) { MessageBox(NULL, _T("打开服务控制管理器失败!"), RC_SERVICE_NAME, MB_OK); return FALSE; } SC_HANDLE hService = OpenService(hSCM, RC_SERVICE_NAME, SERVICE_STOP | DELETE); if (hService == NULL) {MessageBox(NULL, _T("打开服务失败!"), RC_SERVICE_NAME, MB_OK);CloseServiceHandle(hSCM); return false; }// 停止服务 SERVICE_STATUS status; ControlService(hService, SERVICE_CONTROL_STOP, &status);// 删除服务 BOOL bDelete = DeleteService(hService); CloseServiceHandle(hService); CloseServiceHandle(hSCM);return bDelete ? true : false;}int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd){g_mainThreadID = GetCurrentThreadId();if (_tcsicmp(lpCmdLine, _T("/install")) == 0){if (InstallService()){::MessageBox(NULL, _T("安装服务成功!"), _T("安装服务成功!"), MB_OK | MB_ICONINFORMATION);}else{::MessageBox(NULL, _T("安装服务失败!"), _T("安装服务失败!"), MB_OK | MB_ICONINFORMATION);}}else if (_tcsicmp(lpCmdLine, _T("/uninstall")) == 0){if (UninstallService()){::MessageBox(NULL, _T("卸载服务成功!"), _T("卸载服务成功!"), MB_OK | MB_ICONINFORMATION);}else{::MessageBox(NULL, _T("卸载服务失败!"), _T("卸载服务失败!"), MB_OK | MB_ICONINFORMATION);}}else{// 注册服务主函数 ServiceMainSERVICE_TABLE_ENTRY serviceTable[] ={{RC_SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},{NULL, NULL}};if (!::StartServiceCtrlDispatcher(serviceTable)){::MessageBox(NULL, _T("StartServiceCtrlDispatcher 失败!"), _T("StartServiceCtrlDispatcher 失败!"), MB_OK | MB_ICONINFORMATION);}}return 0;}
<pre class="cpp" name="code">// 安装服务// 建个批处理,写入MyFirstSvc.exe /install// 卸载服务// 建个批处理,写入MyFirstSvc.exe /uninstall
0 0
- 想让程序开机启动有管理员权限,使用 Windows 服务吧!
- 让你写的c#程序一开机就自动运行 Windows服务实现开机启动
- 让生成的程序默认有管理员权限
- 让你的程序有管理员权限
- windows下让SVN服务开机自启动
- 让Nginx作为Windows服务开机自启动
- Windows 如何以管理员权限启动程序
- Windows系统开机自动运行程序和自动启动服务
- 让程序(服务)开机运行
- 让程序开机自动启动
- 【C#App】C#使用管理员权限启动程序
- 服务程序中以管理员权限启动其它程序
- 让你的GUI程序随WINDOWS服务一起启动
- C#正确方式让程序以管理员权限启动
- 设置windows程序开机启动
- 让SVN服务开机自动启动
- bat 命令让服务开机启动
- 让Linux开机自动启动mysql服务
- 《数据结构》第二章 线性表 同学提问答案收集 分配
- UVA - 10534 Wavio Sequence
- poj3278--Catch That Cow
- Xcode的快捷键
- 字符串的一些知识
- 想让程序开机启动有管理员权限,使用 Windows 服务吧!
- QPainter
- POJ 1458 Common Subsequence
- 深度优先搜寻(DFS) C语言实现
- 软件模型检测简介
- using thymeleaf之七设置属性的值(th:attr/th:value/th:alt-title/th:attrappend/th:attrprepend/th:checked)
- 提交表单提示框确定取消 点取消不会刷新页面
- 第四章 4.11.1节练习
- OSG中的智能指针