我想在开机时自动启动一个叫JgClockXP.exe
他的路径是:C:\Program Files\clock\JgClockXP.exe
以下是一段实现代码:#define APPKEY "SOFTWARE\\Microsoft\\Windows\\CurrentVerion\\Run"
#define PROGRAM "JgClockXP"
BOOL CMyClockApp::WriteRegistry()
{
CString s;
s="C:\Program Files\clock\JgClockXP.exe";
HKEY hkey;
DWORD dwAction;
//create/open the key
if(RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
APPKEY,
0L,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkey,&dwAction)!=ERROR_SUCCESS)
{
MessageBox(NULL,"Error create/open the key","",MB_OK);
return FALSE;
}
//set the value
if(RegSetValueEx(
hkey,
PROGRAM,
0L,
REG_SZ,
(LPBYTE)(LPCTSTR)s,
s.GetLength()+1)!=ERROR_SUCCESS)
{
MessageBox(NULL,"Error write Program","",MB_OK);
return FALSE;
}
RegCloseKey(hkey);
return TRUE;
}
但我查看注册表时并没有将该键值加上去,不知错具体是不是这么写?
他的路径是:C:\Program Files\clock\JgClockXP.exe
以下是一段实现代码:#define APPKEY "SOFTWARE\\Microsoft\\Windows\\CurrentVerion\\Run"
#define PROGRAM "JgClockXP"
BOOL CMyClockApp::WriteRegistry()
{
CString s;
s="C:\Program Files\clock\JgClockXP.exe";
HKEY hkey;
DWORD dwAction;
//create/open the key
if(RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
APPKEY,
0L,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkey,&dwAction)!=ERROR_SUCCESS)
{
MessageBox(NULL,"Error create/open the key","",MB_OK);
return FALSE;
}
//set the value
if(RegSetValueEx(
hkey,
PROGRAM,
0L,
REG_SZ,
(LPBYTE)(LPCTSTR)s,
s.GetLength()+1)!=ERROR_SUCCESS)
{
MessageBox(NULL,"Error write Program","",MB_OK);
return FALSE;
}
RegCloseKey(hkey);
return TRUE;
}
但我查看注册表时并没有将该键值加上去,不知错具体是不是这么写?
还有RegCreateKeyEx函数的第二个参数是SOFTWARE\\Microsoft\\Windows\\CurrentVerion\\Run吗?
RegSetValueEx的第二个参数就是我要启动的程序的名称吗?
键值就是要启动的程序路径,注意要用双斜杠 DEBUG时输出一下看一下存路径的变量就明白了建议多看看MSDN
{
HKEY hKEY;
if(RegOpenKeyEx (HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\run",0L,KEY_ALL_ACCESS,&hKEY)==ERROR_SUCCESS)
{
LPCTSTR KeyName = "JgClockXP";
unsigned char KeyValue[20];
DWORD type=REG_SZ;
DWORD dwsize=20;
if(RegQueryValueEx(hKEY,KeyName,NULL,&type,KeyValue,&dwsize)==ERROR_SUCCESS)
{
}
else
{
unsigned char digest[16];
memset(digest,NULL,sizeof(digest));
CString lpstr = "JgClockXP.exe";
strcpy((char *)digest,lpstr);
if(RegSetValueEx(hKEY,KeyName,0,REG_SZ,digest,16)==ERROR_SUCCESS)
{
}
} RegCloseKey(hKEY);
}
}
void CServerDlg::WriteRegKey()
{
HKEY hKEY;
if(RegOpenKeyEx (HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\run",0L,KEY_ALL_ACCESS,&hKEY)==ERROR_SUCCESS)
{
LPCTSTR KeyName = "JgClockXP";
unsigned char KeyValue[20];
DWORD type=REG_SZ;
DWORD dwsize=20;
if(RegQueryValueEx(hKEY,KeyName,NULL,&type,KeyValue,&dwsize)==ERROR_SUCCESS)
{
}
else
{
unsigned char digest[50];
memset(digest,NULL,sizeof(digest));
CString lpstr = "C:\Program Files\clock\JgClockXP.exe";
strcpy((char *)digest,lpstr);
if(RegSetValueEx(hKEY,KeyName,0,REG_SZ,digest,strlen(digest))==ERROR_SUCCESS)
{
}
} RegCloseKey(hKEY);
}
}
s="C:\\Program Files\\clock\\JgClockXP.exe";