读书人

自动运行一个程序解决方法

发布时间: 2012-03-22 17:43:57 作者: rapoo

自动运行一个程序
我要在系统登录后自动的运行一个程序,要怎么加?
比如这个程序:
#include <iostream>
using namespace std;
class Clock
{
private:
int N,Y,R,H,M,S;
public:
Clock(int n,int y,int r,int h,int m,int s)
{
N=n,Y=y,R=r,H=h,M=m,S=s;
Y=(y>=0&&y<=12)?y:0;
R=(r>=0&&r<=30)?r:0;
H=(h>=0&&h<=24)?h:0;
M=(m>=0&&m<=60)?m:0;
S=(s>=0&&s<=60)?s:0;
cout<<"constructor:"<<N<<"-"<<Y<<"-"<<R<<","<<H<<":"<<M<<":"<<S<<endl;
}
~Clock(){}
};
void main()
{
Clock Z(2010,10,11,8,50,50);
}
我要在登录系统后自动的运行,怎么办,可以给出源代码吗?谢谢。

[解决办法]
修改注册表
系统是xp的话:开始\运行\输入"regedit"\打开注册表
你可以选择“本地机器上的HKEY_LOCAL_MACHINE”子窗口,定位到HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run分支 添加自动启动项
[解决办法]
Windows平台
1、在注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 项下面添加键值

C/C++ code
            HKEY hKey = NULL;    LONG nRetCode = 0;    BOOL bRetCode = FALSE;    LPCTSTR lpSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";    __try    {        nRetCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_ALL_ACCESS, &hKey);        if (nRetCode != ERROR_SUCCESS)        {            //创建新的注册表数据项            DWORD dwDisposition = 0;            nRetCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,    // handle to open key                                lpSubKey,                // subkey name                NULL,                    // reserved                NULL,                    // class string                REG_OPTION_NON_VOLATILE,// special options                KEY_ALL_ACCESS,            // desired security access                NULL,                    // inheritance                &hKey,                    // key handle                 &dwDisposition);        // disposition value buffer            if (nRetCode == ERROR_SUCCESS)             {                    nRetCode = RegSetValueEx(hKey,lpValueName,NULL,REG_SZ,(LPBYTE)&lpAppName[0],sizeof(TCHAR)*_tcslen(lpAppName));                    if (nRetCode == ERROR_SUCCESS)                    bRetCode = TRUE;             }        }                    else        {            nRetCode = RegSetValueEx(hKey,lpValueName,NULL,REG_SZ,(LPBYTE)&lpAppName[0],sizeof(TCHAR)*_tcslen(lpAppName));                if (nRetCode == ERROR_SUCCESS)                bRetCode = TRUE;        }        }    __finally    {        if (hKey != NULL)            RegCloseKey(hKey);        hKey = NULL;    }    return bRetCode; 

读书人网 >C++

热点推荐