M.H.
M.H.

Reputation: 283

Import registry from files

I have two problems with importing the registry from a file.

I import my registry files with regedit.exe:

string file = "regedit.exe /s D://ImageArchiveHour-1207150440.reg";
const char* ctv = file.c_str(); 

bool result = system(ctv);

In my example, the registry file is successfully imported, but the result is false. How can I get results of importing?

The other problem is, if my registry file path contain SPACE, import will fail.

For example:

string file = "regedit.exe /s D://New Folder//ImageArchiveHour-1207150440.reg";
const char* ctv = file.c_str(); 

system(ctv);

As you can see there is space in New Folder.

How should I fix this? Or is there any other way to do that?

Upvotes: 0

Views: 993

Answers (2)

thedarkside ofthemoon
thedarkside ofthemoon

Reputation: 2281

I see you have got the answer, but I want to tell that if there is a space in the path of some file you should use quotes; so I would write the string as: string file = "regedit.exe /s \"D://New Folder//ImageArchiveHour-1207150440.reg\""

Upvotes: 0

parrowdice
parrowdice

Reputation: 1942

0 is the result of importing. system returns -1 on error, but see the link for the possible return values. If all is okay, it will return the exit code of the regedit operation, 0 = ERROR_SUCCESS.

If command is NULL and the command interpreter is found, returns a nonzero value. If the command interpreter is not found, returns 0 and sets errno to ENOENT. If command is not NULL, system returns the value that is returned by the command interpreter. It returns the value 0 only if the command interpreter returns the value 0. A return value of – 1 indicates an error, and errno is set to one of the following values:...

The operation fails with a space because of the way command line parameters are parsed. You need to enclose your path in quotes (see MSDN C++ Character Literals).

So your

string file = "regedit.exe /s D://ImageArchiveHour-1207150440.reg";

would be

string file = "regedit.exe /s \"D://ImageArchiveHour-1207150440.reg\"";

Upvotes: 2

Related Questions