[入门] windows服务为什么没有运行
lipogem
2007-12-03
请帮我看看这段代码,问题出在哪里:
import std.c.windows.windows; import std.stream; import std.string; extern(Windows) { alias void function(in int,in char**) LPSERVICE_MAIN_FUNCTION; struct _SERVICE_TABLE_ENTRY { char* lpServiceName; LPSERVICE_MAIN_FUNCTION lpServiceProc; } alias _SERVICE_TABLE_ENTRY SERVICE_TABLE_ENTRY; bool StartServiceCtrlDispatcherW(SERVICE_TABLE_ENTRY*); alias StartServiceCtrlDispatcherW StartServiceCtrlDispatcher; struct _SERVICE_STATUS { int dwServiceType; int dwCurrentState; int dwControlsAccepted; int dwWin32ExitCode; int dwServiceSpecificExitCode; int dwCheckPoint; int dwWaitHint; } alias _SERVICE_STATUS SERVICE_STATUS; alias void* SERVICE_STATUS_HANDLE; enum:uint { SERVICE_WIN32_OWN_PROCESS = 0x00000010, SERVICE_WIN32_SHARE_PROCESS = 0x00000020, SERVICE_START_PENDING = 0x00000002, SERVICE_ACCEPT_STOP = 0x00000001, SERVICE_ACCEPT_SHUTDOWN = 0x00000004, SERVICE_RUNNING = 0x00000004, SERVICE_CONTROL_STOP = 0x00000001, SERVICE_CONTROL_SHUTDOWN = 0x00000005, SERVICE_STOPPED = 0x00000001, SERVICE_WIN32 = (SERVICE_WIN32_OWN_PROCESS|SERVICE_WIN32_SHARE_PROCESS), } SERVICE_STATUS_HANDLE RegisterServiceCtrlHandlerW(char*,void function (int)); alias RegisterServiceCtrlHandlerW RegisterServiceCtrlHandler; bool SetServiceStatus(SERVICE_STATUS_HANDLE,SERVICE_STATUS*); void Sleep(int); SERVICE_STATUS ServiceStatus; SERVICE_STATUS_HANDLE hStatus; 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("Service", &ControlHandler); if (hStatus == null) { return; } ServiceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(hStatus, &ServiceStatus); while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) { if (My()) { ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwWin32ExitCode = -1; SetServiceStatus(hStatus, &ServiceStatus); return; } Sleep(SLEEP_TIME); } } void ControlHandler(int request) { switch(request) { case SERVICE_CONTROL_STOP: My(); ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus (hStatus, &ServiceStatus); return; case SERVICE_CONTROL_SHUTDOWN: My(); ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus (hStatus, &ServiceStatus); return; default: break; } SetServiceStatus (hStatus, &ServiceStatus); } } const int SLEEP_TIME=3000; int main() { SERVICE_TABLE_ENTRY ServiceTable[2]; ServiceTable[0].lpServiceName ="Service"; ServiceTable[0].lpServiceProc = &ServiceMain; ServiceTable[1].lpServiceName = null; ServiceTable[1].lpServiceProc = null; StartServiceCtrlDispatcher(&ServiceTable[0]); return 0; } int tn=0; int My() { File OutFile = new File; try { tn++; OutFile.open("f.txt", FileMode.Out); OutFile.write(tn); OutFile.close(); } catch (Exception x) { return -1; } return 0; } 这个服务可以注册和启动但是就是不能往f.txt里边写入内容,问题出在哪里呢? |
|
qiezi
2007-12-03
知道f.txt在哪吗?
|
|
lipogem
2007-12-03
f.txt和程序在同一目录,我用控制台程序测试文件可以写入内容,服务就不能写入
|
|
lipogem
2007-12-03
谢谢楼上朋友的指点,问题解决了。内容没有写入本目录的f.txt而是在系统文件夹下新建了一个f.txt把内容写到那里边去了。
|
相关讨论
相关资源推荐
- C++-64位整数-转字符串-16进制-字符串转64位整数
- C++数值与字符串相互转换的那些事(一)字符串转数值(转载请注明)
- Delphi、VB、C#将16进制字符串转换成64位有符号整数Convert.ToInt64 Method (String, Int32)
- C++ __int64 类型
- c语言将字符串转换为int64,Go语言string,int,int64 ,float之间类型转换方法
- C语言16进制转字符串,并从文件中查询到这个16进制字符串问题解析
- 字符串转64位(strtol,_atoi64)
- C语言格式化Int64
- 16进制字符串到十进制的转换
- iOS 16进制字符串转换成int十进制