如何让程序自动运行
2013-09-12 19:25
225 查看
让程序自动运行,一般是通过注册表进行,首先打开注册表
运用
void CMainFrame::EnableAutoStart()
{
CString sPath;
int nPos;
HKEY RegKey;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer();
nPos=sPath.ReverseFind('\\');
sPath=sPath.Left(nPos);
CString lpszFile=sPath+"\\test.exe";
CFileFind fFind;
BOOL bSuccess;
bSuccess=fFind.FindFile(lpszFile);
fFind.Close();
if(bSuccess)
{
CString fullName;
fullName=lpszFile;
RegKey=NULL;
RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&RegKey);
RegSetValueEx(RegKey,"made by wpf",0,REG_SZ,(const unsigned char*)(LPCTSTR)fullName,fullName.GetLength());
this->UpdateData(FALSE);
}
else
{
::AfxMessageBox("程序运行失败");
exit(0);
}[/code]
}
这篇文章是转的,没有时间去实践,有时间看看。
运用
LONG RegOpenKey( HKEY hKey, // handle to open key LPCTSTR lpSubKey, // address of name of subkey to open PHKEY phkResult // address of handle to open key ); 打开注册表;然后设置注册表的值
LONG RegSetValueEx( HKEY hKey, // handle to key to set value for LPCTSTR lpValueName, // name of the value to set DWORD Reserved, // reserved DWORD dwType, // flag for value type CONST BYTE *lpData, // address of value data DWORD cbData // size of value data );
以下是一个完整的函数,经过测试已经通过,可以直接运用,
void CMainFrame::EnableAutoStart()
{
CString sPath;
int nPos;
HKEY RegKey;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer();
nPos=sPath.ReverseFind('\\');
sPath=sPath.Left(nPos);
CString lpszFile=sPath+"\\test.exe";
CFileFind fFind;
BOOL bSuccess;
bSuccess=fFind.FindFile(lpszFile);
fFind.Close();
if(bSuccess)
{
CString fullName;
fullName=lpszFile;
RegKey=NULL;
RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Run",&RegKey);
RegSetValueEx(RegKey,"made by wpf",0,REG_SZ,(const unsigned char*)(LPCTSTR)fullName,fullName.GetLength());
this->UpdateData(FALSE);
}
else
{
::AfxMessageBox("程序运行失败");
exit(0);
}[/code]
}
这篇文章是转的,没有时间去实践,有时间看看。
相关文章推荐
- 如何使程序在运行时自动注册ActiveX控件
- 如何让 Windows 8 的程序双击后自动以管理员身份运行
- win7自动运行程序如何设置(图文)
- 如何使程序在运行时自动注册ActiveX控件
- 如何使程序在运行时自动注册ActiveX控件
- ARMSYS2410-MULTIUAR 如何让系统启动时自动运行用户程序
- win7如何添加开机启动程序(开机就自动运行打开)
- 如何把Java程序打成JAR包并能自动运行
- Android程序如何在开机时自动运行
- 如何在Linux实现自动运行程序
- Java中如何让web服务器启动的时候自动运行web程序中某个类的某个方法
- Win10如何关闭家庭组和自动维护程序来减轻电脑运行的负担?
- 如何让AIX在启动和停机的时候自动运行一些命令和程序?
- Ubuntu 开机如何自动运行程序
- 如何让程序能够开机自动运行
- 如何自动以管理员身份运行.NET程序?
- 如何在Linux实现自动运行程序
- 如何在Linux实现自动运行程序
- C#程序如何以管理员身份自动运行
- 如何在LINUX中开机、登陆、退出、定时、定期自动运行程序