{"record":{"id":"b912e2584f4344b8","repo":"BoundaryML/baml","slug":"loadlibrary-failed-for-s-error-code-d-w","errorCode":null,"errorMessage":"LoadLibrary failed for %s: error code %d: %w","messagePattern":"LoadLibrary failed for (.+?): error code (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"engine/language_client_go/baml_go/lib_windows.go","lineNumber":39,"sourceCode":"\tkernel32 = syscall.NewLazyDLL(\"kernel32.dll\")\n\n\tprocLoadLibraryW   = kernel32.NewProc(\"LoadLibraryW\")\n\tprocGetProcAddress = kernel32.NewProc(\"GetProcAddress\")\n\tprocFreeLibrary    = kernel32.NewProc(\"FreeLibrary\")\n\tprocGetLastError   = kernel32.NewProc(\"GetLastError\")\n)\n\n// loadLibrary loads the shared library using LoadLibrary\nfunc loadLibrary(path string) (unsafe.Pointer, error) {\n\tpathPtr, err := syscall.UTF16PtrFromString(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid library path: %w\", err)\n\t}\n\n\thandle, _, err := procLoadLibraryW.Call(uintptr(unsafe.Pointer(pathPtr)))\n\tif handle == 0 {\n\t\tlastErr, _, _ := procGetLastError.Call()\n\t\treturn nil, fmt.Errorf(\"LoadLibrary failed for %s: error code %d: %w\", path, lastErr, err)\n\t}\n\n\treturn unsafe.Pointer(handle), nil\n}\n\n// getSymbol retrieves a symbol from the loaded library\nfunc getSymbol(handle unsafe.Pointer, name string) (unsafe.Pointer, error) {\n\tnamePtr, err := syscall.BytePtrFromString(name)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid symbol name: %w\", err)\n\t}\n\n\tproc, _, err := procGetProcAddress.Call(\n\t\tuintptr(handle),\n\t\tuintptr(unsafe.Pointer(namePtr)),\n\t)\n\n\tif proc == 0 {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/lib_windows.go#L21-L57","documentation":"loadLibrary on Windows reports this error when LoadLibraryW returns a zero handle, meaning Windows could not load the BAML native DLL. The message includes the requested path, the GetLastError code, and the syscall's syscall.Errno. Typical causes are a missing DLL, a bad path, or missing dependent DLLs / architecture mismatch.","triggerScenarios":"BAML runtime initialization on Windows where the bundled native library is absent from the expected location, the path is wrong, the DLL's own dependencies (e.g. MSVC runtime) are missing, or a 32-bit process tries to load a 64-bit DLL (or vice versa).","commonSituations":"Deployment that excludes the native .dll from the artifact; antivirus quarantine of the DLL; moving the executable away from the DLL directory; installing the wrong-architecture build; missing VC++ redistributables on clean machines.","solutions":["Verify the DLL exists at the path in the error message (os.Stat) and that the process architecture matches the DLL (use `file`/dumpbin or check GOARCH).","Install missing dependencies of the DLL, especially the Microsoft Visual C++ Redistributable, and confirm dependent DLLs are on PATH or next to the executable.","Reinstall/refresh the baml Go module so the bundled native library is restored (go clean -cache + go mod download), and check antivirus quarantine logs.","Cross-check the Windows error code in the message (e.g. 126 = module not found, 193 = bad executable format) to pinpoint the cause."],"exampleFix":"// before\n// fails: baml.dll not shipped next to the exe\nruntime.Init(filepath.Join(os.Args[0], \"baml.dll\"))\n\n// after\ndllPath := filepath.Join(filepath.Dir(os.Executable()), \"baml.dll\")\nif _, err := os.Stat(dllPath); err != nil {\n    return fmt.Errorf(\"baml native library missing: %w\", err)\n}\nruntime.Init(dllPath)","handlingStrategy":"validation","validationCode":"if _, err := os.Stat(dllPath); err != nil {\n    return fmt.Errorf(\"native library missing at %s: %w\", dllPath, err)\n}","typeGuard":"func dllPresent(path string) bool {\n    _, err := os.Stat(path)\n    return err == nil\n}","tryCatchPattern":"if _, err := runtimeInit(path); err != nil {\n    var loadErr *runtime.LoadError\n    if errors.As(err, &loadErr) {\n        log.Fatalf(\"cannot load BAML DLL %s: %v — check arch and VC++ runtime\", loadErr.Path, loadErr.Code)\n    }\n    return err\n}","preventionTips":["Ship the native DLL alongside the executable in deployment artifacts.","Match process architecture (GOARCH) to the DLL build.","Install the Microsoft VC++ Redistributable on target machines.","Check the Windows error code in the message (126/193 etc.) for the precise cause."],"tags":["windows","dynamic-linking","dll","missing-dependency"],"backgroundTag":"missing-dependency","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}