|
- //SOURCES
- TARGETNAME=hello
- TARGETPATH=obj
- TARGETTYPE=DRIVER
- SOURCES=hello.c
- //hello.c
- void StartSrv()
- {
- char namebuf[256];
- memset(namebuf, 0, sizeof(namebuf));
- SC_HANDLE hMgr, hCS, hOS;
- GetCurrentDirectory(256, namebuf );
- strcat(namebuf, "\\objchk\\i386\\hello.sys" );
- hMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
- if(hMgr == NULL)
- {
- printf("OpenSCManager failed!!!\n");
- exit(0);
- }
- hCS = CreateService(hMgr,TEXT("hello"), TEXT("hello"),
- SERVICE_ALL_ACCESS,
- SERVICE_KERNEL_DRIVER,
- SERVICE_DEMAND_START,
- SERVICE_ERROR_IGNORE,
- TEXT(namebuf),0,0,0,0,0);
- if(hCS == NULL)
- {
- printf("CreateService failed!\n");
- CloseHandle(hMgr);
- exit(0);
- }
- hOS = OpenService(hMgr, "hello", SERVICE_ALL_ACCESS);
- if(hOS == NULL)
- {
- printf("OpenService failed!\n");
- CloseHandle(hCS);
- CloseHandle(hMgr);
- exit(0);
- }
- StartService(hOS,NULL,NULL);
- CloseServiceHandle(hCS);//到这里就可以在DeviceTree里看到hello了
- CloseServiceHandle(hOS);
- CloseServiceHandle(hMgr);
- getch();
- CloseSvc(TEXT("hello"));
- }
- void CloseSvc(char *pServiceName)
- {
- SC_HANDLE sch;
- SERVICE_STATUS ss;
- sch = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);//成功
- if(sch == NULL)
- {
- printf("OpenSCManager failed!\n");
- exit(0);
- }
- SC_HANDLE scm;
- scm = OpenService(sch,TEXT(pServiceName),SERVICE_ALL_ACCESS);//成功
- if(scm == NULL)
- {
- CloseServiceHandle(sch);
- printf("OpenService failed!\n");
- exit(0);
- }
- if(!ControlService(scm, SERVICE_CONTROL_STOP, &ss))//这个失败
- {
- CloseServiceHandle(sch);
- CloseServiceHandle(scm);
- printf("ControlService failed!\n");
- exit(0);
- }
- if(!DeleteService(scm))
- {
- CloseServiceHandle(sch);
- CloseServiceHandle(sch);
- printf("DeleteService failed!\n");
- exit(0);
- }
- }
复制代码
|
|