forked from tursodatabase/turso-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturso_windows.go
More file actions
54 lines (45 loc) · 1.27 KB
/
turso_windows.go
File metadata and controls
54 lines (45 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//go:build windows
package turso_go
import (
"fmt"
"os"
"path/filepath"
"strings"
"golang.org/x/sys/windows"
)
const libName = "turso_go"
func loadLibrary() (uintptr, error) {
// Try to extract embedded library first
libPath, err := extractEmbeddedLibrary()
if err == nil {
// Successfully extracted embedded library, try to load it
slib, dlerr := windows.LoadLibrary(libPath)
if dlerr == nil {
return uintptr(slib), nil
}
// If loading failed, log the error and fall back to system paths
fmt.Printf("Warning: Failed to load embedded library: %v\n", dlerr)
} else {
fmt.Printf("Warning: Failed to extract embedded library: %v\n", err)
}
// Fall back to original behavior
libraryName := fmt.Sprintf("%s.dll", libName)
pathEnv := os.Getenv("PATH")
paths := strings.Split(pathEnv, ";")
cwd, err := os.Getwd()
if err != nil {
return 0, err
}
paths = append(paths, cwd)
for _, path := range paths {
dllPath := filepath.Join(path, libraryName)
if _, err := os.Stat(dllPath); err == nil {
slib, loadErr := windows.LoadLibrary(dllPath)
if loadErr != nil {
return 0, fmt.Errorf("failed to load library at %s: %w", dllPath, loadErr)
}
return uintptr(slib), nil
}
}
return 0, fmt.Errorf("library %s not found in PATH or CWD", libraryName)
}