Skip to content

Commit

Permalink
pkcs11mod: Avoid hardcoding module name
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Mar 17, 2022
1 parent 945cc67 commit de78ddc
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions prevent_unload_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"time"
"unsafe"

"golang.org/x/sys/windows"
)
Expand All @@ -18,19 +20,13 @@ import (
// https://blogs.msmvps.com/vandooren/2006/10/09/preventing-a-dll-from-being-unloaded-by-the-app-that-uses-it/
// https://stackoverflow.com/a/14436845
// https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexa?redirectedfrom=MSDN#parameters
// https://github.com/pipelined/vst2/blob/bc659a1443b585c376cc25a6d13614488c9fa4ee/plugin_export_windows.go#L11-L34
func preventUnload() {
// TODO: don't hardcode this module name

moduleNameGo := "nssckbi"

moduleNameWin, err := windows.UTF16PtrFromString(moduleNameGo)
if err != nil {
log.Printf("pkcs11mod: Error converting module name: %s", err)
return
}
// See https://pkg.go.dev/unsafe#Pointer (reflect.Value.Pointer)
moduleAddressWin := (*uint16)(unsafe.Pointer(reflect.ValueOf(preventUnload).Pointer()))

var module windows.Handle
err = windows.GetModuleHandleEx(windows.GET_MODULE_HANDLE_EX_FLAG_PIN, moduleNameWin, &module)
err = windows.GetModuleHandleEx(windows.GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|windows.GET_MODULE_HANDLE_EX_FLAG_PIN, moduleAddressWin, &module)
if err != nil {
log.Printf("pkcs11mod: Error pinning module: %s", err)
return
Expand Down

0 comments on commit de78ddc

Please sign in to comment.