find_dll.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //go:build windows
  2. package webviewloader
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "golang.org/x/sys/windows/registry"
  10. )
  11. var (
  12. errNoClientDLLFound = errors.New("no webview2 found")
  13. )
  14. func findEmbeddedBrowserVersion(filename string) (string, error) {
  15. block, err := getFileVersionInfo(filename)
  16. if err != nil {
  17. return "", err
  18. }
  19. info, err := verQueryValueString(block, "\\StringFileInfo\\040904B0\\ProductVersion")
  20. if err != nil {
  21. return "", err
  22. }
  23. return info, nil
  24. }
  25. func findEmbeddedClientDll(embeddedEdgeSubFolder string) (outClientPath string, err error) {
  26. if !filepath.IsAbs(embeddedEdgeSubFolder) {
  27. exe, err := os.Executable()
  28. if err != nil {
  29. return "", err
  30. }
  31. embeddedEdgeSubFolder = filepath.Join(filepath.Dir(exe), embeddedEdgeSubFolder)
  32. }
  33. return findClientDllInFolder(embeddedEdgeSubFolder)
  34. }
  35. func findClientDllInFolder(folder string) (string, error) {
  36. arch := ""
  37. switch runtime.GOARCH {
  38. case "arm64":
  39. arch = "arm64"
  40. case "amd64":
  41. arch = "x64"
  42. case "386":
  43. arch = "x86"
  44. default:
  45. return "", fmt.Errorf("Unsupported architecture")
  46. }
  47. dllPath := filepath.Join(folder, "EBWebView", arch, "EmbeddedBrowserWebView.dll")
  48. if _, err := os.Stat(dllPath); err != nil {
  49. return "", mapFindErr(err)
  50. }
  51. return dllPath, nil
  52. }
  53. func mapFindErr(err error) error {
  54. if errors.Is(err, registry.ErrNotExist) {
  55. return errNoClientDLLFound
  56. }
  57. if errors.Is(err, os.ErrNotExist) {
  58. return errNoClientDLLFound
  59. }
  60. return err
  61. }