vba 如何检查是否安装了 Ghostscript 版本以及安装位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21115713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:22:35  来源:igfitidea点击:

How can I check IF a version of Ghostscript is installed and IF SO where it is installed?

vbavbscriptregistry

提问by Pr0no

As the title says. I need to check in VBS whether (1) Ghostscript is installed on the local computer and if so (2) where it is installed.

正如标题所说。我需要在 VBS 中检查 (1) Ghostscript 是否安装在本地计算机上,如果是 (2) 它的安装位置。

I think question (1) I have solved:

我认为问题(1)我已经解决了:

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
Set oReg = GetObject("winmgmts:!root/default:StdRegProv")
    If oReg.EnumKey(HKLM, "SOFTWARE\GPL Ghostscript\", arrSubKeys) = 0 Then
    KeyExists = True
Else
    KeyExists = False 'The script stops since it requires GS to be installed
End If

...

objShell.Run(pathToGhostScript & "-arguments")

However, in my case GS in the registry looks like

但是,就我而言,注册表中的 GS 看起来像

HKEY_LOCAL_MACHINE\SOFTWARE\GPL Ghostscript.01\

Note: I'm checking for a registry entry SOFTWARE\GPL Ghostscript\and not SOFTWARE\GPL Ghostscript\9.01\because my check would return FALSEif someone would have installed ...\9.2\or something. I'm assuming that whichever version might be installed, the registry key SOFTWARE\GPL Ghostscript\would always exist. Is this assumption correct?

注意:我正在检查注册表项,SOFTWARE\GPL Ghostscript\而不是SOFTWARE\GPL Ghostscript\9.01\因为FALSE如果有人安装...\9.2\或其他东西,我的支票会返回。我假设无论安装哪个版本,注册表项SOFTWARE\GPL Ghostscript\都将始终存在。这个假设正确吗?

Eventually, I need to call (in my case) C:\Program Files\gs\gs9.01\bin\gswin32.exe. If I look at the registry, the path to ..\bin\can only bderived from the registry value SOFTWARE\GPL Ghostscript\9.01\GS_DLL, which returns C:\Program Files\gs\gs9.01\bin\gsdll32.dll. I assume I could take this value, remove everything after ..\bin\and concatenat gswin32.exeto the path. I would then have filled the pathToGhostScriptvariable in my script above.

最终,我需要打电话(就我而言)C:\Program Files\gs\gs9.01\bin\gswin32.exe。如果我查看注册表,则 的路径..\bin\只能从注册表值派生,该值SOFTWARE\GPL Ghostscript\9.01\GS_DLL返回C:\Program Files\gs\gs9.01\bin\gsdll32.dll. 我假设我可以采用这个值,删除之后的所有内容..\bin\并连接gswin32.exe到路径。然后我会pathToGhostScript在上面的脚本中填充变量。

Question: how do I return the value for GS_DLLfrom my script? Because I know the value can be found under ..\9.01\GS_DLLin my case, but on someone else's pc it might also be eg. ..\9.57\GS_DLL...

问题:如何GS_DLL从我的脚本返回值?因为我知道可以..\9.01\GS_DLL在我的情况下找到该值,但在其他人的电脑上它也可能是例如。..\9.57\GS_DLL...

My questions:

我的问题:

  1. Is this the best approach?
  2. If not, what would be a more robust approach to see if gswin32.exe is available?
  3. If so, could you help me fill in the blanks discussed above?
  1. 这是最好的方法吗?
  2. 如果没有,那么查看 gswin32.exe 是否可用的更可靠方法是什么?
  3. 如果是这样,你能帮我填写上面讨论的空白吗?

回答by Ansgar Wiechers

I think it's safe to assume that the Ghostscript executable will be located in the same directory as the DLL, so something like this should work:

我认为可以安全地假设 Ghostscript 可执行文件将与 DLL 位于同一目录中,因此应该可以使用以下内容:

Const HKLM    = &h80000002
Const baseKey = "SOFTWARE\GPL Ghostscript"
Const value   = "GS_DLL"

Set reg = GetObject("winmgmts://./root/default:StdRegProv")

If reg.EnumKey(HKLM, baseKey, subkeys) <> 0 Then
  WScript.Echo "Cannot enumerate subkeys of " & baseKey & "."
  WScript.Quit 1
End If

For Each sk In subkeys
  If reg.GetStringValue(HKLM, baseKey & "\" & sk, value, gsLib) <> 0 Then
    WScript.Echo "Cannot read value " & value & "."
    WScript.Quit 1
  End If
Next

Set fso = CreateObject("Scripting.FileSystemObject")

gsDir = fso.GetParentFolderName(gsLib)
gs = fso.BuildPath(gsDir, "gswin32.exe")

WScript.Echo gs