What is the reason for different value after reg add from within a C# application and query the value from within a cmd window? -


i have program coded in c# opens cmd console , adds/modifies registry value.

after console outputs task completed, test reg query if registry value changed. there no problem. value 0x1 expected me.

but when query registry outside current cmd window cmd executed me admin, value 0x0 on reg query.

how possible?

here c# code:

string command = @"/k reg add hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock /t reg_dword /f /v allowdevelopmentwithoutdevlicense /d 1";          process proc = process.start("cmd.exe", command); 

query inside executed cmd program:

reg query hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock  hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock     allowdevelopmentwithoutdevlicense    reg_dword    0x1 

from outside:

reg query hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock  hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock     allowdevelopmentwithoutdevlicense    reg_dword    0x0     allowalltrustedapps    reg_dword    0x0  hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock\allowdevelopmentwithoutdevlicense hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock\allowdevelopmentwithoutdevlicense=1 

when 32-bit application on 64-bit windows accesses registry key

hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock 

it redirected windows registry redirector to

hkey_local_machine\software\wow6432node\microsoft\windows\currentversion\appmodelunlock 

whereby 64-bit application accesses

hkey_local_machine\software\microsoft\windows\currentversion\appmodelunlock 

the difference between 2 registry paths wow6432node visible default 64-bit applications on windows x64.

see microsoft developer article registry keys affected wow64.

and completeness see also

there 32-bit cmd.exe , reg.exe on windows x64 in directory %systemroot%\syswow64 being %systemroot%\system32 32-bit applications , 64-bit cmd.exe , reg.exe in %systemroot%\system32.

your c# application compiled 32-bit application , therefore calls 32-bit cmd.exe calls 32-bit reg.exe.

you workaround within 32-bit application calling explicitly %systemroot%\sysnative\cmd.exe.

but aware of fact %systemroot%\sysnative not exist on 32-bit windows , not exist 64-bit applications on 64-bit windows. (sysnative special alias, not hard link, junction or real folder.)

therefore 32-bit c# application needs first check if %systemroot%\sysnative\cmd.exe exists , use path edit 64-bit registry value using 64-bit cmd , reg. on 32-bit windows %systemroot%\system32.exe\cmd.exe must called 32-bit c# application. way: %systemroot% references value of environment variable systemroot.

see answers on:

note 1:

running cmd.exe not needed @ all. possible run directly on windows x86

%systemroot%\system32\reg.exe 

or on windows x64

%systemroot%\sysnative\reg.exe 

from 32-bit application.

note 2:

and last not least c# application not need use reg.exe @ .net framework has built-in support accessing windows registry - registry.setvalue method. microsoft added examples in example code block.

key_wow64_64key can used in 32-bit application explicitly access 64-bit registry key explained in microsoft developer network article accessing alternate registry view.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -