当前位置:早雪网网络学院编程文档其他语言 → 自己编制windows的后台进程程序

自己编制windows的后台进程程序

减小字体 增大字体 作者:未知  来源:supcode.com收集整理  发布时间:2005-7-1 14:55:55
ACHINE,
TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunServices"),
                                        &hKey ) != ERROR_SUCCESS )
                {
                        //DebugOut( "RegCreateKey() error!");
                        return FALSE;
                }
        }

        dwValueType     = REG_SZ;
        dwStrCb         = 128;

        // Take value
        if( RegQueryValueEx(hKey,
                                SERVICE_NAME,
                                0,
                                &dwValueType,
                                (LPBYTE)lpszStr,
                                &dwStrCb ) == ERROR_SUCCESS )
        
        {
                // Find this key value
                if( _tcscmp( lpszStr, lpszName )==0 )
                {
                        // Remove the service
                        if( dwType == RSP_UNREGISTER_SERVICE )
                        {
if( RegDeleteValue( hKey, SERVICE_NAME ) == ERROR_SUCCESS )
                                {
                                        RegCloseKey ( hKey );
                                        return TRUE;
                                }
                                RegCloseKey( hKey );
                                return FALSE;
                        }
                        // Already exist service
                        if( dwType == RSP_SIMPLE_SERVICE )
                        {
                                //DebugOut("Already registed!");
                                RegCloseKey( hKey );
                                return TRUE;
                        }
                }
                // Not find it
        } // No this value

        // Unregiste return
        if( dwType == RSP_UNREGISTER_SERVICE )
        {
                RegCloseKey( hKey );
                return TRUE;
        }

        // No this value then create it
        if( dwType == RSP_SIMPLE_SERVICE )
        {
                dwStrCb = 128;

                // Set value
                if( RegSetValueEx(hKey,
                                        SERVICE_NAME,
                                        0,
                                        REG_SZ,
                                        (CONST BYTE *)lpszName,
                                        dwStrCb ) != ERROR_SUCCESS )
                {
                        //DebugOut("RegSetValueEx() error!");
                        RegCloseKey( hKey );

                        return FALSE;
                }
                RegCloseKey( hKey );
                return TRUE;
        }

        // Unknow type
        RegCloseKey( hKey );
        return FALSE;
}

主程序:
// WinMain function is the entry of the this program
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
        if( W95ServiceRegister( RSP_SIMPLE_SERVICE ) )
        {
                W95StartService( RSP_SIMPLE_SERVICE );
        }

        MessageBox(NULL, "Sample service", "SERVICE", MB_OK );
        UNREFERENCED_PARAMETER( hInstance );
        UNREFERENCED_PARAMETER( lpCmdLine );
        UNREFERENCED_PARAMETER( nCmdShow );
        UNREFERENCED_PARAMETER( hPrevInstance );
        return 0;
}

  运行这个程序, 等到MessageBox 弹出后, 从WINDOWS 中退出到LOG ON 状态, 你会看见MessageBox 一直保持打开状态直至受到响应或系统关机.

上一页  [1] [2] 

[数据载入中...] [返回上一页] [打 印]