The MASM Forum Archive 2004 to 2012

Project Support Forums => 64 Bit Assembler => Topic started by: ChillyWilly on January 22, 2010, 12:04:39 AM

Title: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 22, 2010, 12:04:39 AM
is it possible? i sent my friend an app that works fine on my 32bit  machine, but on his 64bit it errors reading the key

using this
GetString proc  lpszKey:DWORD,lpszValueName:DWORD,lpszBuffer:DWORD
LOCAL dwStrLength:DWORD
LOCAL phkResult :DWORD
LOCAL RType:DWORD
LOCAL lpcbData :DWORD
local szbuf[256]:BYTE

invoke RegOpenKeyEx,HKEY_LOCAL_MACHINE,lpszKey,0,KEY_READ,addr phkResult

.if eax==ERROR_SUCCESS
mov RType,REG_SZ
mov     lpcbData, 250
invoke RegQueryValueEx,phkResult,lpszValueName,NULL, addr RType, lpszBuffer,addr lpcbData
.if eax==ERROR_SUCCESS
.else
invoke lstrcat,lpszBuffer,CTEXT("RegKey Does Not Exist")
.endif

.else
invoke lstrcat,lpszBuffer,CTEXT("Error Opening RegKey!")
.endif
invoke RegCloseKey, phkResult
ret
GetString endp
Title: Re: 32bit masm app reading from 64bit registry
Post by: Slugsnack on January 22, 2010, 12:51:48 AM
See if this page helps at all :
http://msdn.microsoft.com/en-us/library/ms724072(VS.85).aspx
Title: Re: 32bit masm app reading from 64bit registry
Post by: sinsi on January 22, 2010, 01:22:24 AM
You might also need admin access when using HKEY_LOCAL_MACHINE on vista/win7
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 22, 2010, 03:30:32 PM
im using a manifest.xml to prompt for admin access and still nada
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on January 22, 2010, 05:56:13 PM
i think the registry gets virtualized - you have to access it through the a "wow64" hive (or something like that)
so, HKEY_LOCAL_MACHINE becomes wow64\HKEY_LOCAL_MACHINE

i had to do it once - that worked for what i was doing because i was only reading the registry
you may have to jump through hoops if you want to write to it and have it stick after a re-boot
Title: Re: 32bit masm app reading from 64bit registry
Post by: evlncrn8 on January 22, 2010, 08:55:02 PM
actually, its done using a flag in the access rights..


KEY_WOW64_32KEY (0x0200)


Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. For more information, see Accessing an Alternate Registry View.

This flag must be combined using the OR operator with the other flags in this table that either query or access registry values.

    Windows 2000:  This flag is not supported.

KEY_WOW64_64KEY (0x0100)


Indicates that an application on 64-bit Windows should operate on the 64-bit registry view. For more information, see Accessing an Alternate Registry View.

This flag must be combined using the OR operator with the other flags in this table that either query or access registry values.

    Windows 2000:  This flag is not supported.

source : http://msdn.microsoft.com/en-us/library/ms724878%28VS.85%29.aspx
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 23, 2010, 09:15:19 PM
testing on win7 home premium 64bit I tried using the samDesired parameter of KEY_WOW64_64KEY
heres the code so you can get a good example of what im trying to achieve
.386
.model flat, stdcall
option casemap:none

GetString proto :DWORD,:DWORD ,:DWORD
CTEXT MACRO y:VARARG
LOCAL sym, dummy
dummy EQU $;; MASM error fix
CONST segment
IFIDNI <y>,<>
sym db 0
ELSE
sym db y,0
ENDIF
CONST ends
EXITM <OFFSET sym>
ENDM

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

includelib user32.lib
includelib kernel32.lib
includelib advapi32.lib

.data
szKey     db "SOFTWARE\Microsoft\Windows NT\CurrentVersion",0

.data?
buffstuff db 1024 dup(?)

.code
start:




invoke GetString,addr szKey,CTEXT("ProductName"),addr buffstuff
invoke MessageBox, NULL,addr buffstuff,CTEXT('Result'), MB_OK
invoke ExitProcess,NULL


GetString proc  lpszKey:DWORD,lpszValueName:DWORD,lpszBuffer:DWORD
LOCAL dwStrLength:DWORD
LOCAL phkResult :DWORD
LOCAL RType:DWORD
LOCAL lpcbData :DWORD
local szbuf[256]:BYTE

invoke RegOpenKeyEx,HKEY_LOCAL_MACHINE,lpszKey,0,KEY_READ or KEY_WOW64_64KEY ,addr phkResult

.if eax==ERROR_SUCCESS
mov RType,REG_SZ
mov     lpcbData, 250
invoke RegQueryValueEx,phkResult,lpszValueName,NULL, addr RType, lpszBuffer,addr lpcbData
.if eax==ERROR_SUCCESS
.else
invoke lstrcat,lpszBuffer,CTEXT("RegKey Does Not Exist")
.endif

.else
invoke lstrcat,lpszBuffer,CTEXT("Error Opening RegKey!")
.endif
invoke RegCloseKey, phkResult
ret
GetString endp
end start
Title: Re: 32bit masm app reading from 64bit registry
Post by: Gunner on January 23, 2010, 09:40:09 PM
Chilly, in response to http://www.masm32.com/board/index.php?topic=13209.0

here is the code I used for that

EnumExtensions32 proc uses ebx edi esi
LOCAL FileExt[50]:BYTE
LOCAL lpcBuffer:DWORD
LOCAL FTime:FILETIME
LOCAL   hKey:DWORD

    invoke  RegOpenKeyEx, HKEY_CLASSES_ROOT, NULL, NULL,KEY_ENUMERATE_SUB_KEYS or KEY_WOW64_32KEY, addr hKey
xor ebx, ebx
inc ebx ; Skip * key
xor edi, edi
GetNextExtension:
lea esi, FileExt
push NULL
push 50
push esi
call memfill

mov lpcBuffer, 50

; Enumerate each key in HCR
lea eax, FTime
push eax
push NULL
push NULL
push NULL
lea ecx, lpcBuffer
push ecx
push esi
push ebx
push hKey
call RegEnumKeyEx
cmp eax, ERROR_NO_MORE_ITEMS
je EnumDone

; Get only extensions
mov al, byte ptr [esi]
cmp     al, '.'
;jne NextExt
jne EnumDone

; Make sure not a .??_ file
mov al, byte ptr [esi + 3]
cmp     al, '_'
je NextExt


NextExt:
inc ebx
jmp GetNextExtension

EnumDone:
lea esi, FileExt
push NULL
push 50
push esi
call memfill

        push    esi
        push    ebx
        call    dwtoa
       
        push    0
        push    offset szMainCaption1
        push    esi
        push    0
        call    MessageBox

ret
EnumExtensions32 endp


I do a lot of Registry access with many of my programs, I will take a closer look at your code in a bit to see if I notice anything
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 23, 2010, 10:29:01 PM
RegOpenKeyEx seems to be failing

if im doing formatmessage right it seems to be returning "access is denied" even though app is running as admin

change my .else to this:

.else
invoke ShowErrorMessage,0,eax
invoke lstrcat,lpszBuffer,CTEXT("Error Opening RegKey!")
.endif



ShowErrorMessage hWnd,dwError
  local lpBuffer:DWORD
lea eax,[lpBuffer]
invoke FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
invoke MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
invoke LocalFree,[lpBuffer]
ret
ShowErrorMessage endp

Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on January 24, 2010, 03:59:22 AM
it may be the stack alignment - there is concurrently another thread on that issue
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 25, 2010, 04:51:46 PM
whats the link to the other thread
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on January 25, 2010, 06:39:31 PM
http://www.masm32.com/board/index.php?topic=13186.msg102596#msg102596
Title: Re: 32bit masm app reading from 64bit registry
Post by: GregL on January 25, 2010, 07:12:34 PM
The stack alignment issue in the other thread pertains to 64-bit programs.
Title: Re: 32bit masm app reading from 64bit registry
Post by: tofu-sensei on January 25, 2010, 08:13:42 PM
Quote from: ChillyWilly on January 23, 2010, 09:15:19 PM
testing on win7 home premium 64bit I tried using the samDesired parameter of KEY_WOW64_64KEY
heres the code so you can get a good example of what im trying to achieve
this is working just fine for me if i remove the KEY_WOW64_64KEY flag...
Title: Re: 32bit masm app reading from 64bit registry
Post by: sinsi on January 28, 2010, 09:25:50 AM
>this is working just fine for me if i remove the KEY_WOW64_64KEY flag

Same here, even without admin.
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 30, 2010, 04:13:00 AM
which version of windows are you guys running?
Title: Re: 32bit masm app reading from 64bit registry
Post by: sinsi on January 30, 2010, 04:33:23 AM
Win7 RC x64
Title: Re: 32bit masm app reading from 64bit registry
Post by: dacid on January 30, 2010, 09:55:14 PM
ChillyWilly .. I have the same problem here. If I use KEY_WOW64_64KEY I get an error 5 = "access denied" and if I dont use it, I can open the key but I cant read the value (error 2 = DONT_FOUND). I tried running as an Administrator and Disabling UAC with no sucess. Im on Windows 7 x64.

Let me know if you found what the problem is and the solution...
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on January 31, 2010, 07:25:23 AM
yea thats where im at too
not sure how its working on sinsi if its not working on us
Title: Re: 32bit masm app reading from 64bit registry
Post by: GregL on January 31, 2010, 11:14:26 PM
It works for me if I remove the KEY_WOW64_64KEY.  Windows 7 Pro x64 retail.
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on February 01, 2010, 02:30:11 AM
what about reading the digitalproductid key?

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
DigitalProductId
Title: Re: 32bit masm app reading from 64bit registry
Post by: GregL on February 01, 2010, 03:42:15 AM
Quotewhat about reading the digitalproductid key?

Not without modifying your code quite a bit.  I can read it with PowerShell since I already had a script to do that. Good for retrieving the Windows Product Key.

Title: Re: 32bit masm app reading from 64bit registry
Post by: sinsi on February 01, 2010, 03:57:55 AM
DigitalProductId is REG_BINARY, not REG_SZ, so yeah lots of modification to your code.
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on February 01, 2010, 05:19:28 AM
ok ill try to whip something up tomorrow and test it and ill post back if its not working
Title: Re: 32bit masm app reading from 64bit registry
Post by: dacid on February 01, 2010, 09:38:32 AM
I have problems reading the "DigitalProductId"... the code works on Windows XP SP3 (x86) and Windows Vista SP2 (x86)... so it doesnt work in 64 bits Windows. Still dunno why.

I tried opening the key with:  KEY_QUERY_VALUE, KEY_READ and KEY_ALL_ACCESS and reading the "DigitalPRoductId" with REG_BINARY and REG_DWORD and as I said it works ok in 32 bits but fails in 64bits:

If I use KEY_WOW64_64KEY to open the key I get an error 5 = "access denied" and if I dont use it, I can open the key but I cant read the value "DigitalProductId" (error 2 = DONT_FOUND).
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on February 01, 2010, 06:16:40 PM
i would be curious to see the results of the last attachment in this post...

http://www.masm32.com/board/index.php?topic=11963.msg90835#msg90835

if you browse through that thread, there are posted results from 64-bit OS's
Title: Re: 32bit masm app reading from 64bit registry
Post by: dacid on February 01, 2010, 06:28:46 PM

DednDave OS Info Dump Ver 2.03

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion]
"ProductName"="Windows 7 Ultimate"
"CurrentVersion"="6.1"
"CurrentBuildNumber"="7600"
"SubVersionNumber"=(Value not found)
"CSDVersion"=(Value not found)
"BuildLab"="7600.win7_rtm.090713-1255"
"ProductId"=(Value not found)

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Media Center]
"Ident"="6.0"

OSVERSIONINFOEX Structure:

       Version.Build: 6.1.7600
         Platform ID: Win32 on Windows NT
         CSD Version:
Service Pack Version: 0.0
          Suite Mask: 0000000100000000
        Product Type: Windows 2000 Pro/XP/Vista Workstation

Press any key to exit



Im seeing right now the ProductId value in Regedit...  :eek

Im reading a lot on info online and in all examples they use KEY_WOW64_64KEY with success... why we get the "permission denied" error? ...
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on February 01, 2010, 06:32:06 PM
that's what i mean - lol
i didn't do anything special in that program - i am pretty sure the source code is there   :bg
Title: Re: 32bit masm app reading from 64bit registry
Post by: dacid on February 01, 2010, 06:49:28 PM
All people with 64 bits posted:  "ProductId"=(Value not found)

You need to use KEY_WOW64_64KEY  when open the key... but, at least here, I get the error code = 5 (access denied)
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on February 01, 2010, 08:00:55 PM
maybe this article will help, even though it applies to installing AVG - the cure might be the same

http://www.copyrunstart.net/quick-fix-avg-antivirus-80-error-message-error-action-failed-for-registry-key/
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on February 02, 2010, 03:38:06 AM
DednDave OS Info Dump Ver 2.03

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion]
"ProductName"="Windows 7 Home Premium"
"CurrentVersion"="6.1"
"CurrentBuildNumber"="7600"
"SubVersionNumber"=(Value not found)
"CSDVersion"=(Value not found)
"BuildLab"="7600.win7_rtm.090713-1255"
"ProductId"=(Value not found)

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Media Center]
"Ident"="6.0"

OSVERSIONINFOEX Structure:

       Version.Build: 6.1.7600
         Platform ID: Win32 on Windows NT
         CSD Version:
Service Pack Version: 0.0
          Suite Mask: 0000001100000000
        Product Type: Windows 2000 Pro/XP/Vista Workstation

Press any key to exit
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on February 02, 2010, 04:04:24 AM
well - i think i wrote that code about the same time the win 7 beta came out
so - things have obviously changed a bit - lol
for my purposes, i only needed the product ID to differentiate some of the oddball XP's (like XP gold, XP gamers edition, etc)
at the time, i was looking for a way to seperate XP media center editions, but i found that "Ident" key that you see
Title: Re: 32bit masm app reading from 64bit registry
Post by: dacid on February 20, 2010, 08:07:48 PM
any success?
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on May 13, 2010, 04:25:06 AM
how come I cant read this registry value?
the other string values in that key can be read  :'(


.386
.model flat, stdcall
option casemap:none

GetString proto :DWORD,:DWORD ,:DWORD
CTEXT MACRO y:VARARG
LOCAL sym, dummy
dummy EQU $;; MASM error fix
CONST segment
IFIDNI <y>,<>
sym db 0
ELSE
sym db y,0
ENDIF
CONST ends
EXITM <OFFSET sym>
ENDM

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

includelib user32.lib
includelib kernel32.lib
includelib advapi32.lib

.data


.data?
buffstuff db 1024 dup(?)

.code
start:
invoke GetString,CTEXT("SOFTWARE\Microsoft\Windows NT\CurrentVersion"),CTEXT("DigitalProductId"),addr buffstuff
invoke MessageBox, NULL,addr buffstuff,CTEXT('Result'), MB_OK
invoke ExitProcess,NULL


ShowErrorMessage proc hWnd,dwError
  local lpBuffer:DWORD
lea eax,[lpBuffer]
invoke FormatMessage,FORMAT_MESSAGE_ALLOCATE_BUFFER+FORMAT_MESSAGE_FROM_SYSTEM,0,[dwError],LANG_NEUTRAL,eax,0,0
invoke MessageBox,[hWnd],[lpBuffer],NULL,MB_ICONERROR+MB_OK
invoke LocalFree,[lpBuffer]
ret
ShowErrorMessage endp

GetString proc  lpszKey:DWORD,lpszValueName:DWORD,lpszBuffer:DWORD
LOCAL dwStrLength:DWORD
LOCAL phkResult :DWORD
LOCAL RType:DWORD
LOCAL lpcbData :DWORD
local szbuf[256]:BYTE

invoke RegOpenKeyEx,HKEY_LOCAL_MACHINE,lpszKey,0,KEY_QUERY_VALUE ,addr phkResult

.if eax==ERROR_SUCCESS
mov RType,REG_BINARY
mov     lpcbData, 250
invoke RegQueryValueEx,phkResult,lpszValueName,NULL, addr RType, lpszBuffer,addr lpcbData
.if eax==ERROR_SUCCESS
.else
invoke lstrcat,lpszBuffer,CTEXT("RegKey Does Not Exist")
.endif

.else
    invoke ShowErrorMessage,0,eax
invoke lstrcat,lpszBuffer,CTEXT("Error Opening RegKey!")
.endif
invoke RegCloseKey, phkResult
ret
GetString endp
end start
Title: Re: 32bit masm app reading from 64bit registry
Post by: ChillyWilly on May 31, 2010, 07:48:38 PM
noone knows? theres gotta be someone here with a 64bit cpu that better at coding than me  :dazzled:
Title: Re: 32bit masm app reading from 64bit registry
Post by: Gunner on May 31, 2010, 08:10:21 PM
It shows the value fine here... it shows the first hex value of that binary reg value on my Win7 32bit is a4, after a4 are a few NULLs so that is the ONLY character messages box shows.... You are going to have to parse that value and get what is after the nulls... it is REG_BINARY NOT REG_SZ which to me means the there are going to be NULLs in the value..

Is the format the same across os's?  don't know... here is how mine starts:

A4 00 00 00 03 00 00 00 00 DigitalProductId HERE

does the 3 mean there are 3 nulls before the DigitalProductId don't know, you are going to have to figure out how to parse the buffer to grab the DigitalProductId

Title: Re: 32bit masm app reading from 64bit registry
Post by: diablo2oo2 on December 30, 2010, 11:56:51 PM
its a bug in the windows.inc
correct:
KEY_WOW64_32KEY                  equ 0200h
KEY_WOW64_64KEY                  equ 0100h
KEY_WOW64_RES                    equ 0300h

wrong:
KEY_WOW64_32KEY                  equ 0200
KEY_WOW64_64KEY                  equ 0100
KEY_WOW64_RES                    equ 0300
Title: Re: 32bit masm app reading from 64bit registry
Post by: dedndave on December 31, 2010, 12:20:11 AM
nice catch, diablo
should post in the masm32 project forum, windows.inc thread