F.N
F.N

Reputation: 401

Generate correct path for RegSetValueEx()

I am writing this C code that will store a Key in Registry which points in the current path of the application. Here is the code.

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);
}

The problem is that GetModuleFileName() returns the path in that form :

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

while in the RegSetValueEx() is expected a path in that form :

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

Is there any way to convert the first path into the second one ? Tried many ways to replace that string but no one worked.

Thank you.

Upvotes: 0

Views: 146

Answers (1)

cesar_sr91
cesar_sr91

Reputation: 130

Try the answer of this question. This should be enough to solve your problem.

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

Upvotes: 1

Related Questions