Trouble adding value to registry key












-3















I'm trying to add a value ("UpdateSvc") under the registry key HKCUSoftwareMicrosoftWindowsCurrentVersionRun using C++. This is the code used:



bool regWrite(LPCSTR subkey, LPCSTR name, DWORD type, const char * value)
{
HKEY key;

if (RegCreateKeyA(HKEY_CURRENT_USER, subkey, &key) != ERROR_SUCCESS)
return false;
if (RegSetValueExA(key, name, NULL, type, (LPBYTE)value, strlen(value) * sizeof(char) + 1) != ERROR_SUCCESS)
return false;
if (RegCloseKey(key) != ERROR_SUCCESS)
return false;
return true;
}

int main()
{
bool ret;
ret = regWrite("Software\Microsoft\Windows\CurrentVersion\Run", "UpdateSvc", REG_SZ, "Test");
printf("%d", ret);
getchar();
return 0;
}


It prints '1', yet I can't seems to be able to find the value. Tried checking with regedit as well as reg query HKCUsoftwaremicrosoftwindowscurrentversionrun (including /reg:32 and /reg:64 for good measure).



The same thing occurs with



system("reg add HKCU\software\microsoft\windows\currentversion\run /v UpdateSvc /t REG_SZ /d Test /f"; (which I know is unsafe, just debugging).



Running 64-bit Visual Studio as Administrator on 64-bit Windows 10. The user I'm logged on to is not an Admin.



Edit



@IInspectable solved it. The value was added under the administrator's hive, not mine, hence why it wasn't visible.










share|improve this question




















  • 3





    You must escape the backslashes in your string, e.g. Software\Micros...

    – Paul Ogilvie
    Nov 26 '18 at 14:54






  • 3





    Didn't your compiler warn about unknown escape sequences? Turn warnings on.

    – Paul Ogilvie
    Nov 26 '18 at 14:55











  • I tried that, it doesn't help.

    – Mike Dvorkin
    Nov 26 '18 at 15:10






  • 2





    "Running Visual Studio as Administrator." - Where are you looking for the changes in the registry? The current user's hive, or the administrator's?

    – IInspectable
    Nov 26 '18 at 15:45






  • 2





    @mik: E.g. by supplying an application manifest, asking for autoElevate. You don't have to run Visual Studio with administrative privileges.

    – IInspectable
    Nov 26 '18 at 16:01
















-3















I'm trying to add a value ("UpdateSvc") under the registry key HKCUSoftwareMicrosoftWindowsCurrentVersionRun using C++. This is the code used:



bool regWrite(LPCSTR subkey, LPCSTR name, DWORD type, const char * value)
{
HKEY key;

if (RegCreateKeyA(HKEY_CURRENT_USER, subkey, &key) != ERROR_SUCCESS)
return false;
if (RegSetValueExA(key, name, NULL, type, (LPBYTE)value, strlen(value) * sizeof(char) + 1) != ERROR_SUCCESS)
return false;
if (RegCloseKey(key) != ERROR_SUCCESS)
return false;
return true;
}

int main()
{
bool ret;
ret = regWrite("Software\Microsoft\Windows\CurrentVersion\Run", "UpdateSvc", REG_SZ, "Test");
printf("%d", ret);
getchar();
return 0;
}


It prints '1', yet I can't seems to be able to find the value. Tried checking with regedit as well as reg query HKCUsoftwaremicrosoftwindowscurrentversionrun (including /reg:32 and /reg:64 for good measure).



The same thing occurs with



system("reg add HKCU\software\microsoft\windows\currentversion\run /v UpdateSvc /t REG_SZ /d Test /f"; (which I know is unsafe, just debugging).



Running 64-bit Visual Studio as Administrator on 64-bit Windows 10. The user I'm logged on to is not an Admin.



Edit



@IInspectable solved it. The value was added under the administrator's hive, not mine, hence why it wasn't visible.










share|improve this question




















  • 3





    You must escape the backslashes in your string, e.g. Software\Micros...

    – Paul Ogilvie
    Nov 26 '18 at 14:54






  • 3





    Didn't your compiler warn about unknown escape sequences? Turn warnings on.

    – Paul Ogilvie
    Nov 26 '18 at 14:55











  • I tried that, it doesn't help.

    – Mike Dvorkin
    Nov 26 '18 at 15:10






  • 2





    "Running Visual Studio as Administrator." - Where are you looking for the changes in the registry? The current user's hive, or the administrator's?

    – IInspectable
    Nov 26 '18 at 15:45






  • 2





    @mik: E.g. by supplying an application manifest, asking for autoElevate. You don't have to run Visual Studio with administrative privileges.

    – IInspectable
    Nov 26 '18 at 16:01














-3












-3








-3








I'm trying to add a value ("UpdateSvc") under the registry key HKCUSoftwareMicrosoftWindowsCurrentVersionRun using C++. This is the code used:



bool regWrite(LPCSTR subkey, LPCSTR name, DWORD type, const char * value)
{
HKEY key;

if (RegCreateKeyA(HKEY_CURRENT_USER, subkey, &key) != ERROR_SUCCESS)
return false;
if (RegSetValueExA(key, name, NULL, type, (LPBYTE)value, strlen(value) * sizeof(char) + 1) != ERROR_SUCCESS)
return false;
if (RegCloseKey(key) != ERROR_SUCCESS)
return false;
return true;
}

int main()
{
bool ret;
ret = regWrite("Software\Microsoft\Windows\CurrentVersion\Run", "UpdateSvc", REG_SZ, "Test");
printf("%d", ret);
getchar();
return 0;
}


It prints '1', yet I can't seems to be able to find the value. Tried checking with regedit as well as reg query HKCUsoftwaremicrosoftwindowscurrentversionrun (including /reg:32 and /reg:64 for good measure).



The same thing occurs with



system("reg add HKCU\software\microsoft\windows\currentversion\run /v UpdateSvc /t REG_SZ /d Test /f"; (which I know is unsafe, just debugging).



Running 64-bit Visual Studio as Administrator on 64-bit Windows 10. The user I'm logged on to is not an Admin.



Edit



@IInspectable solved it. The value was added under the administrator's hive, not mine, hence why it wasn't visible.










share|improve this question
















I'm trying to add a value ("UpdateSvc") under the registry key HKCUSoftwareMicrosoftWindowsCurrentVersionRun using C++. This is the code used:



bool regWrite(LPCSTR subkey, LPCSTR name, DWORD type, const char * value)
{
HKEY key;

if (RegCreateKeyA(HKEY_CURRENT_USER, subkey, &key) != ERROR_SUCCESS)
return false;
if (RegSetValueExA(key, name, NULL, type, (LPBYTE)value, strlen(value) * sizeof(char) + 1) != ERROR_SUCCESS)
return false;
if (RegCloseKey(key) != ERROR_SUCCESS)
return false;
return true;
}

int main()
{
bool ret;
ret = regWrite("Software\Microsoft\Windows\CurrentVersion\Run", "UpdateSvc", REG_SZ, "Test");
printf("%d", ret);
getchar();
return 0;
}


It prints '1', yet I can't seems to be able to find the value. Tried checking with regedit as well as reg query HKCUsoftwaremicrosoftwindowscurrentversionrun (including /reg:32 and /reg:64 for good measure).



The same thing occurs with



system("reg add HKCU\software\microsoft\windows\currentversion\run /v UpdateSvc /t REG_SZ /d Test /f"; (which I know is unsafe, just debugging).



Running 64-bit Visual Studio as Administrator on 64-bit Windows 10. The user I'm logged on to is not an Admin.



Edit



@IInspectable solved it. The value was added under the administrator's hive, not mine, hence why it wasn't visible.







c++ winapi registry






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 16:48







Mike Dvorkin

















asked Nov 26 '18 at 14:36









Mike DvorkinMike Dvorkin

3916




3916








  • 3





    You must escape the backslashes in your string, e.g. Software\Micros...

    – Paul Ogilvie
    Nov 26 '18 at 14:54






  • 3





    Didn't your compiler warn about unknown escape sequences? Turn warnings on.

    – Paul Ogilvie
    Nov 26 '18 at 14:55











  • I tried that, it doesn't help.

    – Mike Dvorkin
    Nov 26 '18 at 15:10






  • 2





    "Running Visual Studio as Administrator." - Where are you looking for the changes in the registry? The current user's hive, or the administrator's?

    – IInspectable
    Nov 26 '18 at 15:45






  • 2





    @mik: E.g. by supplying an application manifest, asking for autoElevate. You don't have to run Visual Studio with administrative privileges.

    – IInspectable
    Nov 26 '18 at 16:01














  • 3





    You must escape the backslashes in your string, e.g. Software\Micros...

    – Paul Ogilvie
    Nov 26 '18 at 14:54






  • 3





    Didn't your compiler warn about unknown escape sequences? Turn warnings on.

    – Paul Ogilvie
    Nov 26 '18 at 14:55











  • I tried that, it doesn't help.

    – Mike Dvorkin
    Nov 26 '18 at 15:10






  • 2





    "Running Visual Studio as Administrator." - Where are you looking for the changes in the registry? The current user's hive, or the administrator's?

    – IInspectable
    Nov 26 '18 at 15:45






  • 2





    @mik: E.g. by supplying an application manifest, asking for autoElevate. You don't have to run Visual Studio with administrative privileges.

    – IInspectable
    Nov 26 '18 at 16:01








3




3





You must escape the backslashes in your string, e.g. Software\Micros...

– Paul Ogilvie
Nov 26 '18 at 14:54





You must escape the backslashes in your string, e.g. Software\Micros...

– Paul Ogilvie
Nov 26 '18 at 14:54




3




3





Didn't your compiler warn about unknown escape sequences? Turn warnings on.

– Paul Ogilvie
Nov 26 '18 at 14:55





Didn't your compiler warn about unknown escape sequences? Turn warnings on.

– Paul Ogilvie
Nov 26 '18 at 14:55













I tried that, it doesn't help.

– Mike Dvorkin
Nov 26 '18 at 15:10





I tried that, it doesn't help.

– Mike Dvorkin
Nov 26 '18 at 15:10




2




2





"Running Visual Studio as Administrator." - Where are you looking for the changes in the registry? The current user's hive, or the administrator's?

– IInspectable
Nov 26 '18 at 15:45





"Running Visual Studio as Administrator." - Where are you looking for the changes in the registry? The current user's hive, or the administrator's?

– IInspectable
Nov 26 '18 at 15:45




2




2





@mik: E.g. by supplying an application manifest, asking for autoElevate. You don't have to run Visual Studio with administrative privileges.

– IInspectable
Nov 26 '18 at 16:01





@mik: E.g. by supplying an application manifest, asking for autoElevate. You don't have to run Visual Studio with administrative privileges.

– IInspectable
Nov 26 '18 at 16:01












1 Answer
1






active

oldest

votes


















1














The registry functions all return values of type LSTATUS indicating whether they succeeded or failed. If RegCreateKey fails, RegSetValueEx on the HKEY value will obviously also fail. Also, if RegCloseKey fails (unlikely) then pending writes may not be flushed to the registry.



if(RegCreateKeyA(...) != ERROR_SUCCESS)
{
// Handle error
}
if(RegSetValueExA(...) != ERROR_SUCCESS)
{
// Handle error
}





share|improve this answer


























  • I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

    – Mike Dvorkin
    Nov 26 '18 at 15:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53483419%2ftrouble-adding-value-to-registry-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The registry functions all return values of type LSTATUS indicating whether they succeeded or failed. If RegCreateKey fails, RegSetValueEx on the HKEY value will obviously also fail. Also, if RegCloseKey fails (unlikely) then pending writes may not be flushed to the registry.



if(RegCreateKeyA(...) != ERROR_SUCCESS)
{
// Handle error
}
if(RegSetValueExA(...) != ERROR_SUCCESS)
{
// Handle error
}





share|improve this answer


























  • I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

    – Mike Dvorkin
    Nov 26 '18 at 15:46
















1














The registry functions all return values of type LSTATUS indicating whether they succeeded or failed. If RegCreateKey fails, RegSetValueEx on the HKEY value will obviously also fail. Also, if RegCloseKey fails (unlikely) then pending writes may not be flushed to the registry.



if(RegCreateKeyA(...) != ERROR_SUCCESS)
{
// Handle error
}
if(RegSetValueExA(...) != ERROR_SUCCESS)
{
// Handle error
}





share|improve this answer


























  • I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

    – Mike Dvorkin
    Nov 26 '18 at 15:46














1












1








1







The registry functions all return values of type LSTATUS indicating whether they succeeded or failed. If RegCreateKey fails, RegSetValueEx on the HKEY value will obviously also fail. Also, if RegCloseKey fails (unlikely) then pending writes may not be flushed to the registry.



if(RegCreateKeyA(...) != ERROR_SUCCESS)
{
// Handle error
}
if(RegSetValueExA(...) != ERROR_SUCCESS)
{
// Handle error
}





share|improve this answer















The registry functions all return values of type LSTATUS indicating whether they succeeded or failed. If RegCreateKey fails, RegSetValueEx on the HKEY value will obviously also fail. Also, if RegCloseKey fails (unlikely) then pending writes may not be flushed to the registry.



if(RegCreateKeyA(...) != ERROR_SUCCESS)
{
// Handle error
}
if(RegSetValueExA(...) != ERROR_SUCCESS)
{
// Handle error
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 15:44

























answered Nov 26 '18 at 15:12









Govind ParmarGovind Parmar

10.4k53360




10.4k53360













  • I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

    – Mike Dvorkin
    Nov 26 '18 at 15:46



















  • I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

    – Mike Dvorkin
    Nov 26 '18 at 15:46

















I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

– Mike Dvorkin
Nov 26 '18 at 15:46





I was under the impression that raw string literals are a C feature, so the title is accidentally misleading - this is a C++ project.

– Mike Dvorkin
Nov 26 '18 at 15:46




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53483419%2ftrouble-adding-value-to-registry-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)