JeffreySu/WeiXinMPSDK · critical · Win32Exception

无法加载企业微信 Finance 原生库:

Error message

无法加载企业微信 Finance 原生库:{path}

What it means

FinanceLibraryHandle.Load could not load the WeChat Work Finance native DLL on Windows: LoadLibraryW returned IntPtr.Zero. The wrapper throws Win32Exception including the Win32 error code and the requested path. The library file is missing, blocked, or its dependencies cannot be resolved.

Solutions

  1. Check the path/relative path in LibraryPath and that the dll exists in the output directory (CopyToOutputDirectory=true).
  2. Read the Win32Exception error code: 126 = file not found (fix path/dependencies), 193 = wrong bitness (use matching x64/x86 dll), 5 = access denied.
  3. Install missing dependencies of the SDK (VC++ redistributables) and unblock the file (Unblock-File).
  4. On load failure, prefer an absolute path to the SDK dll over a bare file name.

Example fix

// before
var api = MsgAuditFinanceNativeApi.Create(); // defaults to "libWeWorkFinanceSdk.dll" not copied
// after
var path = Path.Combine(AppContext.BaseDirectory, "libWeWorkFinanceSdk_Csharp", "libWeWorkFinanceSdk.dll");
if (!File.Exists(path)) throw new FileNotFoundException("Finance SDK dll missing", path);
var api = MsgAuditFinanceNativeApi.Create(path);
Defensive patterns

Strategy: try-catch

Validate before calling

var path = Path.Combine(AppContext.BaseDirectory, "libWeWorkFinanceSdk.dll");
if (!File.Exists(path)) throw new FileNotFoundException("Finance SDK dll not deployed", path);

Type guard

static bool LibraryAvailable(string path) => File.Exists(path) && Path.GetExtension(path) == ".dll";

Try / catch

try { return FinanceLibraryHandle.Load(path); }
catch (Win32Exception ex) when (ex.NativeErrorCode == 126) { throw new InvalidOperationException("Finance dll or its dependencies missing at " + path, ex); }
catch (Win32Exception ex) when (ex.NativeErrorCode == 193) { throw new InvalidOperationException("Finance dll bitness mismatch", ex); }

Prevention

When it happens

Trigger: Constructing the finance API with a LibraryPath that points to a nonexistent/moved libWeWorkFinanceSdk.dll; the DLL's dependent DLLs (VC runtime, OpenSSL) missing; the file blocked by Windows ('downloaded from internet' mark); running x64 process with x86 dll or vice versa (returns error 193 'bad exe format').

Common situations: Deploying without copying the SDK dll next to the app; CI/Docker builds on windows where the dll wasn't published; antivirus quarantine of the dll.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/9a445bd063a9ed43. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/MsgAudit/MsgAuditFinanceNativeApi.cs:352

        {
            _handle = handle;
            _isWindows = isWindows;
            _useLegacyLibDl = useLegacyLibDl;
        }

        /// <summary>
        /// 加载动态库并返回受控句柄。
        /// </summary>
        /// <param name="path">动态库路径或文件名。</param>
        /// <returns>已经加载的动态库句柄。</returns>
        public static FinanceLibraryHandle Load(string path)
        {
            if (FinanceRuntimePlatform.IsWindows)
            {
                var handle = WindowsNative.LoadLibrary(path);
                if (handle == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(),
                        $"无法加载企业微信 Finance 原生库:{path}");
                }

                return new FinanceLibraryHandle(handle, true, false);
            }

            if (!FinanceRuntimePlatform.IsLinux)
            {
                throw new PlatformNotSupportedException(
                    "企业微信 Finance 会话内容存档原生库仅支持 Windows 和 Linux。");
            }

            IntPtr linuxHandle;
            var useLegacy = false;
            try
            {
                linuxHandle = LinuxNative.DlOpen(path, RtldNow);
            }

View on GitHub (pinned to be573f6f94)