为RegSetValueEx()生成正确的路径()

问题描述:

我在写这个C代码,它将在注册表中存储一个指向应用程序当前路径的密钥。这是代码。为RegSetValueEx()生成正确的路径()

HKEY hKey; 
LPCTSTR appPath; 
LPCTSTR regPath = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 
char buffer[300]; 

GetModuleFileName(NULL,buffer,300); 
appPath = buffer; 

if(RegOpenKeyEx(HKEY_CURRENT_USER,regPath,0,KEY_ALL_ACCESS,&hKey)== ERROR_SUCCESS) 
{ 
    RegSetValueEx(hKey,"storing.exe",0,REG_SZ,appPath,sizeof(appPath)); 
    RegCloseKey(hKey); 
} 

的问题是,GetModuleFileName()返回路径以这样的形式:

C:\Documents and Settings\User\Desktop\storing.exe

而在函数RegSetValueEx(),预计在该形式的路径:

C:\\Document and Settings\\User\\Desktop\\storring.exe

有没有办法将第一个路径转换为第二个路径? 尝试了很多方法来替换该字符串,但没有人工作。

谢谢。

试试this question的答案。 这应该足以解决您的问题。

str_replace(appPath, "\", "\\");