TCHAR m_fileName[100];
// CString m_fileName;
GetModuleFileName(NULL,m_fileName,100);
HKEY hKey;
CString str = _T("Software//Microsoft//Windows//CurrentVersion//Run");
if (ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, str, &hKey))
{
MessageBox(_T("打开注册表项失败"));
RegCloseKey(hKey);
return;
}
int length = 0;
while(m_fileName[length]!=_T('/0'))
length++;
if (ERROR_SUCCESS != RegSetValueEx(hKey, _T("MyAutoRun"), 0, REG_SZ, (const BYTE *)m_fileName, sizeof(TCHAR)*length))
{
MessageBox(_T("写注册表失败"));
RegCloseKey(hKey);
}
RegCloseKey(hKey);
把上面的代码添加到你的程序能一定被执行到的任意地方。
如果是mfc程序,不用包含任何库文件,其他的要包括<windows.h>和<tchar.h>
我在vc9下测试成功。
vc++写注册表自启动( 转)
最新推荐文章于 2024-05-02 18:04:02 发布