Batch File Querying Registry in 64-Bit Vista

a 夏天 提交于 2019-12-14 04:26:37

问题


Hi guys me again :) I have a problem with a batch file I wrote. It works fine on 32-bit, but apparently it doesn't work on 64-bit systems, and I don't know why because I do not have access to a 64-bit system.

This is the code that works on Vista 32-bit system

    @echo off
Set Reg.Key=HKLM\SOFTWARE\Malwarebytes' Anti-Malware
  Set Reg.Val=InstallPath
  For /F "Tokens=2*" %%A In (
   'Reg Query "%Reg.Key%" /v "%Reg.Val%" ^| Find /I "%Reg.Val%"'
  ) Do Call Set MBAMPATH=%%B

Can someone re-write it to work on a 64-bit please?

Thanks always :)


回答1:


There's no difference between batch files on x86 and x64 versions of Windows. The problem you have is due to WoW64 and transparent Registry redirection, see here for more details.

HKLM\SOFTWARE\Wow6432Node is the correct registry key for 32bit software on a 64bit installation of Windows, and this code works on my machine:

Set Reg.Key=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Malwarebytes' Anti-Malware
Set Reg.Val=InstallPath
For /F "Tokens=2*" %%A In (
    'Reg Query "%Reg.Key%" /v "%Reg.Val%" ^| Find /I "%Reg.Val%"' )
Do Call Set MBAMPATH=%%B
echo %mbampath%

Your other post includes a conditional branch based on the %processor_architecture% environment variable. The problem with that is that (on my machine at least) %processor_architecture% is "AMD64" not "x64".

Why not just query both possible registry keys rather than branching based on architecture?




回答2:


First of all there is nothing I know which would be different for batch files on 64-bit Windows compared to 32 bit.

You might try removing echo off from the start to see which line produces which error message (assuming you see any).

However, maybe something clashes with the apostrophes you have used in the for loop. You could try changing it to

for /f "usebackq tokens=2*" %%A in (`Reg Query "%Reg.Key%" /v "%Reg.Val%" ^| Find /I "%Reg.Val%"`) Do Call Set MBAMPATH=%%B

And while we're at it. Why do you call set there? That isn't necessary, so you could remove it as well:

... do set MBAMPATH=%%B



回答3:


I have a similar problem where I needed to know the Notepad++ install directory. If it is on a x64 OS then I need to look in the HKLM\SOFTWARE\Wow6432Node\Notepad++ reg key. The reason I don't look for arch of the processor is that you can put a 32 bit OS on a 64bit capable machine. This is the code I came up with and seems to find the right path for me:

:: find out if x86 or x64 OS - need different reg key to query for install dir IF EXIST "%PROGRAMFILES(X86)%" goto X64 ELSE goto X86

:X64 FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKLM\SOFTWARE\Wow6432Node\Notepad++" ')DO SET NPPPath="%%B\plugins\APIs" echo The NPPPath is %NPPPath% SET BITNESS="SixtyFour" goto CopyAPIFile

:X86 FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKLM\SOFTWARE\Notepad++" ')DO SET NPPPath="%%B\plugins\APIs" :: echo The NPPPath is %NPPPath% SET BITNESS="ThirtyTwo" goto CopyAPIFile



来源:https://stackoverflow.com/questions/771240/batch-file-querying-registry-in-64-bit-vista

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!