Add your own alternative version
Stats
116.5K views 22 bookmarked
Posted
30 Jan 2001
|
Comments and Discussions
|
|
// There is already Windows Shell API function for this, although it doesn't copy security attributes.
// Here is how to use it.
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
bool bResult = false;
HKEY hOldKey;
HKEY hNewKey;
CString oldKey("SOFTWARE\\MyCompany\\MyProduce\\Oldkey");
CString newKey("SOFTWARE\\MyCompany\\MyProduce\\Newkey");
if (RegOpenKey(HKEY_LOCAL_MACHINE, oldKey, &hOldKey) == 0)
{
if (RegCreateKey(HKEY_LOCAL_MACHINE, newKey, &hNewKey) == 0)
{
bResult = (SHCopyKey(hOldKey, NULL, hNewKey, 0) == 0);
RegCloseKey(hNewKey);
}
RegCloseKey(hOldKey);
if (bResult)
{
RegDeleteKey(HKEY_LOCAL_MACHINE, oldKey);
}
}
Ever had a bug that fixed itself? I hate those!
|
|
|
|
|
Doesn't handle UNICODE or security, and has a few bugs.
|
|
|
|
|
|
|
This is wonderful. Just what i needed.
Save me huge burden.
Thank you one more time.
|
|
|
|
|
...and ValueName can be longer then MAX_PATH
also, I don't see reason to allocate/deallocate buffers in the loop
RegQueryInfoKey would be definitely a reasonable choice
or at least do buffer reallocation only if need, not every time
code mark should be less then 4, I made a mistake 
|
|
|
|
|
After the first look I got few issues/ToDo for the author:
- UNICODE
--- why don't write with TCHARs from the beginning?
- security
--- are security attributes also copied from one key to another?
--- what if few subkeys has security settings which are not allowed to look into, etc?
it's just after 1 minute review, I must say that code is very far from ideal.
PS last remark, recursion is very easy to implement but a little bit limit usage of this code
Valery A. Boronin
|
|
|
|
|
i added this function to a program (which for now does nothing)
then i got these errors:
p:\regtest2\regtest2.cpp(20) : error C3861: 'free': identifier not found, even with argument-dependent lookup
p:\regtest2\regtest2.cpp(31) : error C3861: 'malloc': identifier not found, even with argument-dependent lookup
p:\regtest2\regtest2.cpp(110) : error C3861: 'CopyKey': identifier not found, even with argument-dependent lookup
did i do anythign wrong?
maybe forgot to include something or add a namespace?
i use ms visual studio .net 2003 c++
thx in advance
E-Male
|
|
|
|
|
Nice code! I had a binary key that was > 1024 bytes, and the code created a corrupted copy in this case. I fixed the problem by querying for the value length just *before* the AllocBuff in the innermost loop:
Err = RegQueryInfoKey(SrcKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &BuffSize, NULL, NULL);
// if (Err != ERROR_SUCCESS) ...
BuffSize = __max(BuffSize, 1024); // just to be safe minsize is 1024
|
|
|
|
|
An alternate way to handle > 1024 bytes of data.
Err = ERROR_MORE_DATA;<br />
BuffSize = 1024;<br />
Retries = -1;<br />
do {<br />
++Retries;<br />
AllocBuff(&Buff,BuffSize);<br />
size=MAX_PATH+1;<br />
Err = RegEnumValue(SrcKey,ValEnumIndx,ValName,&size,NULL,&VarType,Buff,&BuffSize);<br />
} while (Err == ERROR_MORE_DATA && Retries < 2);
|
|
|
|
|
Very good code, u saved me, and very clean that recurse but it´s wrong called, i think it should be RegCopyKey(...) instead of CopyKey(...) as the comment says is a recursive call. But Anyway, with that little change it works!! Thanks again 
|
|
|
|
|
Thank you, Ziv!
Yesterday I was thinking how tedious it would be to write registry copying function... and now I get it without any coding!
Very useful function! Thanks!
|
|
|
|
|
As a matter of fact very easy.
But it was just what I came looking for on CP today.
Thanks. ![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
There are some bugs or typos :
I changed the prototype, the char* is now const, this is cleaner.
LONG RegCopyKey(HKEY SrcKey, HKEY TrgKey, const char* TrgSubKeyName)
and
if ((Err = CopyKey(SrcSubKey, TrgSubKey, KeyName)) != ERROR_SUCCESS)
changed to
if ((Err = RegCopyKey(SrcSubKey, TrgSubKey, KeyName)) != ERROR_SUCCESS)
Otherwise it works great. Thank you, this saved me at leat one and a half
hour of work.
|
|
|
|
|
I had been looking for such a function for days when I finally came upon this article. How could I have missed it all this time? Anyway, it's exactly what I was looking for, I could put it to use in my registry class in 10 minutes.
I just modified it a little so that it would not overwrite existing values.
For this I replaced
...
// write value to target key
if (RegSetValueEx(TrgSubKey,ValName,NULL,VarType,Buff,BuffSize) != ERROR_SUCCESS)
goto quit_get_err;
...
with
...
if (RegGetValueEx(TrgSubKey,ValName,NULL,&VarType,NULL,NULL) == ERROR_SUCCESS)
{
// don't do anything, there is already such a value
}
else // write value to target key
if (RegSetValueEx(TrgSubKey,ValName,NULL,VarType,Buff,BuffSize) != ERROR_SUCCESS)
{
goto quit_get_err;
}
...
|
|
|
|
|
Good
|
|
|
|
|
any function to import reg files within program???
|
|
|
|
|
You could use ShellExecute with the path of the reg file you want to import.
g
|
|
|
|
|
Hi,
nice one, but shouldn't you open the TrgKey in KEY_WRITE mode if you copy to it?
Regards,
Marco
|
|
|
|
|
Hi,
Since the subkey(TrgSubKey) is created with KEY_ALL_ACCESS, this is not necessary.
Cheers Ziv
|
|
|
|
|
|
Hey this is very useful. Nice code,man.
|
|
|
|
|
The code is very useful to me, thank you!
But, what has this code to do with 'Book Chapters', as it is in the Book Chapters category of the site.
|
|
|
|
|
Anonymous wrote:
But, what has this code to do with 'Book Chapters', as it is in the Book Chapters category of the site.
HE IS A BOOK HAHAHAHAH BOOD BUG ! HAHAHAH 
|
|
|
|
|
|
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|