#include <Windows.h>
#include <tchar.h>
void CopenrunDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
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);
}