⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 service.cpp

📁 一个串口通信的程序
💻 CPP
字号:
#include "stdafx.h"
#include "service.h"
// service.cpp
BOOL SV_ServiceRegister( DWORD dwType )
{
    // Function address defination
    REGPROC  hookServiceProc; 

     HMODULE  hModule;
    LPCSTR  lpProcName;

     hModule = GetModuleHandle("KERNEL32");
    lpProcName = TEXT("RegisterServiceProcess");

    // Get address of function
    hookServiceProc =(REGPROC)GetProcAddress( hModule,lpProcName);

    // Register the my service
    return (hookServiceProc(NULL,dwType)==0) ? FALSE : TRUE;
}



BOOL SV_StartService( DWORD dwType )
{
//    TCHAR  lpszStr[MAX_PATH]="Software\\Microsoft\\Windows\\CurrentVersion\\RunServices";
    TCHAR  lpszStr[MAX_PATH]="Software\\Microsoft\\Windows\\CurrentVersion\\Run";
    LPTSTR  lpszName;
    HKEY    hKey;
     LONG    lValue;
    DWORD  dwDataType;
    DWORD  dwSize = sizeof(lpszStr);

    // Get service name currently
    lpszName = GetCommandLine();

    for( int i = _tcslen(lpszName)-1; i>=0; i-- ){
        if( lpszName[i] != '"' &&  lpszName[i]!=' ' ) break;
        else if( lpszName[i] == '"' ) lpszName[i] = '\0';
     }
    
     if( lpszName[0] == '"' )  lpszName++;

    // Registe as start up service
    lValue = RegOpenKeyEx ( HKEY_LOCAL_MACHINE,
                            lpszStr,
                            0,
                            KEY_QUERY_VALUE  |  KEY_SET_VALUE,
                            &hKey );
     
     if( lValue != ERROR_SUCCESS ) {
         if( RegCreateKey( HKEY_LOCAL_MACHINE, lpszStr, &hKey ) != ERROR_SUCCESS ) 
             return FALSE;
     }

    if( RegQueryValueEx(hKey, 
                        "OurService",    // Your service name
                        NULL,
                        &dwDataType,
                        (LPBYTE)lpszStr,
                        &dwSize ) == ERROR_SUCCESS ){

        // Find this key value
        if( _tcscmp( lpszStr, lpszName )==0 ){
            // Remove the service
            if( dwType == UNREGISTER_SERVICE ) {

                if( RegDeleteValue( hKey, "OurService" ) == ERROR_SUCCESS ){
                    RegCloseKey ( hKey );
                    return TRUE;
                }
                
                RegCloseKey( hKey );
                return FALSE;
            }

            // Already exist service
            if( dwType == REGISTER_SERVICE ) {
                RegCloseKey( hKey );
                return TRUE;
            }
         } 
     } 


    if( dwType == UNREGISTER_SERVICE ) {
        RegCloseKey( hKey );
        return TRUE;
     }

    if( dwType == REGISTER_SERVICE )
    {

        if( RegSetValueEx(hKey,
                          "OurService",
                          0,
                          REG_SZ,
                          (BYTE *)lpszName,
                          dwSize ) != ERROR_SUCCESS ) {
            RegCloseKey( hKey );
            return FALSE;
        }

         RegCloseKey( hKey );
        return TRUE;
     }

    RegCloseKey( hKey );
    return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -