操作注册表出现问题.让文件自动运行.

来源:百度知道 编辑:UC知道 时间:2024/05/17 09:28:25
szFilePath的值是c:\Users\a\Documents\Visual Studio 2008\Projects\autoRun\Debug\autoRun.exe
而写入的值是:c:\Users\a\Documents\Visual Studio 20
后面的几个字符没有了.
我把工程打包了了http://www.zhouqiming.com/asp/autoRun.rar
void CautoRunDlg::OnBnClickedButton1()
{
TCHAR szFilePath[MAX_PATH+1];
GetModuleFileName(NULL,szFilePath,MAX_PATH);
HKEY hKey;
HKEY RootKey=HKEY_CURRENT_USER;
TCHAR *KeyTree =_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
if(RegOpenKeyEx(RootKey,KeyTree,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
{
int tmp=CString(szFilePath).GetLength();
if(RegSetValueEx(hKey,_T("example"),NULL,REG_SZ,(byte*)szFilePath,tmp)!=ERROR_SUCCESS)
{
AfxMessageBox(_T("错误:无法设置有关的注册表信息"));
}
RegCloseKey(h

int tmp=CString(szFilePath).GetLength();
改为
int tmp = strlen(szFilePath);

这段程序在我机器上编译通过,运行也没有问题,我用的是VC++ 6.0 SP6
TCHAR szFilePath[MAX_PATH + 1];
GetModuleFileName( NULL, szFilePath, MAX_PATH );
HKEY hKey;
LONG lResult;
lResult = RegOpenKeyEx(HKEY_CURRENT_USER,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);
if(lResult == ERROR_SUCCESS)
{
lResult = RegSetValueEx( hKey, "example", NULL, REG_SZ, (byte*)szFilePath, strlen(szFilePath) );
if(lResult != ERROR_SUCCESS)
AfxMessageBox(_T("错误:无法设置有关的注册表信息"));

RegCloseKey(hKey);
}
else
AfxMessageBox(_T("错误:无法查看有关的注册表信息"));

格式化为c:\\Users\\a\\Documents\\Visual Studio 2008\\Projects\\autoRun\\Debug\\autoRun.exe

HKEY hKey1;
char *SubKey="Sof