windows服务和注册表简单编程例子

一:注册表的操作编程



#include <windows.h> #include <iostream> #include <cstdlib> using namespace std; int main() { HKEY hKey; LPCTSTR lpRun = "Software\_MyTest"; DWORD state,dwtype,sizeBuff; long lRet; char reBuff[10] = {0}; /*创建键***************************************************************************/ ////创建键 lRet = RegCreateKeyEx(HKEY_CURRENT_USER,lpRun,0,NULL,0,0,NULL,&hKey,&state); if(lRet == ERROR_SUCCESS) { if(state == REG_CREATED_NEW_KEY) cout<<"表项创建成功"<<endl; //关闭键 RegCloseKey(hKey); } /*打开/修改键值********************************************************************/ ////打开键 // lRet= RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_WRITE, &hKey); // if(lRet == ERROR_SUCCESS) // { // //创建或者修改键值 // RegSetValueEx(hKey, "test",0,REG_SZ,(BYTE *)"success",10); // //关闭键 // RegCloseKey(hKey); // } /*读取键值*************************************************************************/ ////打开键 // lRet= RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_READ, &hKey); // if(lRet == ERROR_SUCCESS) // { // sizeBuff = sizeof(reBuff); // //读取键值 // if(RegQueryValueEx(hKey,"test",0,&dwtype,(BYTE*)reBuff,&sizeBuff) == ERROR_SUCCESS) // cout<<reBuff<<endl; // //关闭键 // RegCloseKey(hKey); // } /*删除键值************************************************************************/ ////打开键 // lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_WRITE, &hKey); // if(lRet==ERROR_SUCCESS) // { // //删除键 // RegDeleteValue(hKey,"test"); // //关闭键 // RegCloseKey(hKey); // } /*删除键**************************************************************************/ // RegDeleteKey(HKEY_CURRENT_USER,lpRun); // system("pause"); }

二:windows服务编程的基本框架

// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
//main.cpp

#include "stdafx.h"



CHAR * SERVICE_NAME = "testService";
SERVICE_STATUS_HANDLE serviceStatusHandle;
SERVICE_STATUS ServStatus;
static BOOL shutdownSVC = FALSE;
static FILE *logfile = NULL;
TCHAR cTimeString[30] = _T("");
time_t currentTime;

VOID Handler(DWORD controlcode)
{
    switch (controlcode)
    {
    case SERVICE_CONTROL_STOP:
        shutdownSVC = TRUE;
        ServStatus.dwCurrentState = SERVICE_STOPPED;
        SetServiceStatus(serviceStatusHandle, &ServStatus);
        break;
    case SERVICE_CONTROL_PAUSE:
        break;
    case SERVICE_CONTROL_CONTINUE:
        break;
    case SERVICE_CONTROL_INTERROGATE:
        break;
    case SERVICE_CONTROL_SHUTDOWN:
        break;
    }
    return;
}

VOID ServiceMain(DWORD argc, LPTSTR * argv)
{
    ServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    ServStatus.dwCurrentState = SERVICE_START_PENDING;
    ServStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
    ServStatus.dwWin32ExitCode = NO_ERROR;
    ServStatus.dwServiceSpecificExitCode = 0;
    ServStatus.dwCheckPoint = 0;
    ServStatus.dwWaitHint = 2000;

    serviceStatusHandle = RegisterServiceCtrlHandler((LPCWSTR)SERVICE_NAME, (LPHANDLER_FUNCTION)Handler);

    ServStatus.dwCurrentState = SERVICE_RUNNING;
    SetServiceStatus(serviceStatusHandle, &ServStatus);

    FILE *fp;
    fopen_s(&fp, "F:\\test.c", "w");
    fprintf(fp, "%s", "#include <stdio.h>\n");
    fprintf(fp, "%s", "int main(void)\n");
    fprintf(fp, "%s", "{\n");
    fprintf(fp, "%s", "printf(\"hello World!\");\n");
    fprintf(fp, "%s", "}\n");
    fprintf(fp, "%s", "return 0;\n");
    fclose(fp);

}

int main()
{

    SERVICE_TABLE_ENTRY serviceTable[] =
    {
        { (LPWSTR)SERVICE_NAME,(LPSERVICE_MAIN_FUNCTION)ServiceMain },
        { NULL,NULL }
    };
    BOOL success;
    success = StartServiceCtrlDispatcher(serviceTable);
    if (!success)
        ExitProcess(GetLastError());
    return 1;
}
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>




// TODO: 在此处引用程序需要的其他头文件

 

posted @ 2018-09-26 21:39  Duke777  阅读(672)  评论(0编辑  收藏  举报