hi

i write a program to add a value in windows registry but it didn't work
and doesn't add anything in registry, what's a problem?
my assembler is masm32.

and here is my code:

.386
.model flat, stdcall
option casemap:none

include windows.inc
include kernel32.inc
include advapi32.inc

includelib kernel32.lib
includelib advapi32.lib

.data
    szRegSubKey db    "Software\Microsoft\Windows\CurrentVersion\Policies\System", 0
    szValue     db    "DisableTaskMgr", 0
	
.data?
    hKey    PHKEY    ?

.code
start:

    invoke  RegOpenKeyEx, RegOpenKeyEx, ADDR szRegSubKey, NULL, KEY_ALL_ACCESS, ADDR hKey
	
    OR    EAX, EAX
    JNE   _error
		  invoke  RegSetValueEx, hKey, ADDR szValue, NULL, REG_DWORD, 1, DWORD

_error:

    invoke  RegCloseKey, hKey
    invoke  ExitProcess, NULL

end start

Dani AI

Generated

Quick summary: the failure came from passing the wrong argument to RegOpenKeyEx and from giving RegSetValueEx an immediate instead of a pointer/size. As noted, the first parameter must be a predefined HKEY (for example, HKEY_LOCAL_MACHINE), not the function name. Writing under HKLM also requires elevated rights on modern Windows.

RegOpenKeyEx expects (HKEY, lpSubKey, ulOptions, samDesired, phkResult). The call should pass a real HKEY constant, the address of the subkey string, 0 for reserved, a sensible access mask (KEY_SET_VALUE or KEY_WRITE instead of KEY_ALL_ACCESS if only setting a value), and the address of a DWORD variable that will receive the handle. RegSetValueEx requires a pointer to the data buffer and a byte count — for a DWORD that means passing the address of a 4‑byte variable and 4 as cbData.

Example (minimal, corrected pattern):

.data
  subKey    db "Software\Microsoft\Windows\CurrentVersion\Policies\System",0
  valueName db "DisableTaskMgr",0
  dwVal     dd 1
  hKey      dd 0

.code
  invoke RegOpenKeyEx, HKEY_LOCAL_MACHINE, ADDR subKey, 0, KEY_SET_VALUE, ADDR hKey
  cmp   eax, 0
  jne   open_failed
  invoke RegSetValueEx, hKey, ADDR valueName, 0, REG_DWORD, ADDR dwVal, 4
  ; check eax, close key when open, exit

Additional notes: always check the return code (ERROR_SUCCESS == 0) and only call RegCloseKey if a handle was returned. If the key may not exist, use RegCreateKeyEx. On 64‑bit Windows consider WOW64 redirection (use KEY_WOW64_64KEY or KEY_WOW64_32KEY as appropriate). Remember that writing to HKLM on Vista+ requires elevation (Run as Administrator or an elevated process). Finally, declare the handle as a dword (for example hKey dd 0) so its address can be passed to RegOpenKeyEx and its value used with RegSetValueEx.

Recommended Answers

All 2 Replies

try

invoke RegOpenKeyEx HKEY_LOCAL_MACHINE, ADDR szRegSubKey, NULL, KEY_ALL_ACCESS, ADDR hKey)

thanks Tight_Coder_Ex it's work. :icon_lol:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.