C++编撰服务,在服务中打开外部exe
C++编写服务,在服务中打开外部exe
现在服务中打开一个外部文件,exe、txt都行,目前用ShellExecute(NULL, "open", "D:\\0.exe", "", "", SW_SHOW);没办法打开
代码在这里
------解决方案--------------------
这样就可以的,你试下
现在服务中打开一个外部文件,exe、txt都行,目前用ShellExecute(NULL, "open", "D:\\0.exe", "", "", SW_SHOW);没办法打开
代码在这里
#include <windows.h>
#include <stdio.h>
#include "XmlSettings.h"
#pragma comment(lib, "Advapi32")
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int main(int argc, char* argv[])
{
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = "yy8";
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
// 启动服务的控制分派机线程
StartServiceCtrlDispatcher(ServiceTable);
}
void ServiceMain(int argc, char** argv)
{
ServiceStatus.dwServiceType =
SERVICE_WIN32;
ServiceStatus.dwCurrentState =
SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted =
SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler(
"MemoryStatus",
(LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
return;
}
// We report the running status to SCM.
ServiceStatus.dwCurrentState =
SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);
/** 服务启动一分钟后开始进入循环 */
//Sleep(10000);
CXmlSettings xml;
xml.addTag("year");
xml.addTag("month");
xml.addTag("day");
xml.addTag("hour");
xml.addTag("minute");
xml.addTag("second");
// The worker loop of a service
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
ShellExecute(NULL, "open", "D:\\0.bat", "", "", SW_SHOW);
SYSTEMTIME curr_st;
GetLocalTime(&curr_st);
int year = curr_st.wYear;
int month = curr_st.wMonth;
int day = curr_st.wDay;
int hour = curr_st.wHour;
int minute = curr_st.wMinute;
int second = curr_st.wSecond;
xml.setValue("year", year);
xml.setValue("month", month);
xml.setValue("day", day);
xml.setValue("hour", hour);
xml.setValue("minute", minute);
xml.setValue("second", second);
xml.saveFile("D:\\test.xml");
//MessageBox(NULL, "1111", "111", MB_OK);
Sleep(5000);
}
return;
}
void ControlHandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
C++服务
------解决方案--------------------
这样就可以的,你试下
HDESK hdeskCurrent;
HDESK hdesk;
HWINSTA hwinstaCurrent;
HWINSTA hwinsta;
hwinstaCurrent = GetProcessWindowStation();
hdeskCurrent = GetThreadDesktop(GetCurrentThreadId());
//打开用户的winsta0
hwinsta = OpenWindowStation("winsta0", FALSE,