-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"syscall" | ||
"unicode/utf16" | ||
"unicode/utf8" | ||
"unsafe" | ||
) | ||
|
||
var ( | ||
modntdll = syscall.NewLazyDLL("ntdll") | ||
procNtQueryKey = modntdll.NewProc("NtQueryKey") | ||
) | ||
|
||
// The KeyInformationClass constants have been derived from the KEY_INFORMATION_CLASS enum definition. | ||
type KeyInformationClass uint32 | ||
|
||
const ( | ||
KeyBasicInformation KeyInformationClass = iota | ||
KeyNodeInformation | ||
KeyFullInformation | ||
KeyNameInformation | ||
KeyCachedInformation | ||
KeyFlagsInformation | ||
KeyVirtualizationInformation | ||
KeyHandleTagsInformation | ||
MaxKeyInfoClass | ||
) | ||
|
||
const STATUS_BUFFER_TOO_SMALL = 0xC0000023 | ||
|
||
// OUT-parameter: KeyInformation, ResultLength. | ||
// *OPT-parameter: KeyInformation. | ||
func NtQueryKey( | ||
keyHandle uintptr, | ||
keyInformationClass KeyInformationClass, | ||
keyInformation *byte, | ||
length uint32, | ||
resultLength *uint32, | ||
) int { | ||
r0, _, _ := procNtQueryKey.Call(keyHandle, | ||
uintptr(keyInformationClass), | ||
uintptr(unsafe.Pointer(keyInformation)), | ||
uintptr(length), | ||
uintptr(unsafe.Pointer(resultLength))) | ||
return int(r0) | ||
} | ||
|
||
// GetRegistryLocation(uintptr(device.reg)) | ||
|
||
func GetRegistryLocation(regHandle uintptr) (string, error) { | ||
var size uint32 = 0 | ||
result := NtQueryKey(regHandle, KeyNameInformation, nil, 0, &size) | ||
if result == STATUS_BUFFER_TOO_SMALL { | ||
buf := make([]byte, size) | ||
if result := NtQueryKey(regHandle, KeyNameInformation, &buf[0], size, &size); result == 0 { | ||
regPath, err := DecodeUTF16(buf) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
|
||
tempRegPath := replaceRegistryMachine(regPath) | ||
tempRegPath = generalizeControlSet(tempRegPath) | ||
|
||
return `HKEY_LOCAL_MACHINE\` + tempRegPath, nil | ||
} | ||
} else { | ||
return "", fmt.Errorf("error: 0x%X", result) | ||
} | ||
return "", nil | ||
} | ||
|
||
func DecodeUTF16(b []byte) (string, error) { | ||
if len(b)%2 != 0 { | ||
return "", fmt.Errorf("must have even length byte slice") | ||
} | ||
|
||
u16s := make([]uint16, 1) | ||
ret := &bytes.Buffer{} | ||
b8buf := make([]byte, 4) | ||
|
||
lb := len(b) | ||
for i := 0; i < lb; i += 2 { | ||
u16s[0] = uint16(b[i]) + (uint16(b[i+1]) << 8) | ||
r := utf16.Decode(u16s) | ||
n := utf8.EncodeRune(b8buf, r[0]) | ||
ret.Write(b8buf[:n]) | ||
} | ||
|
||
return ret.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/lxn/walk" | ||
) | ||
|
||
func createRegFile(regpath string, item *Device) string { | ||
packageInfo := template.New("packageInfo") | ||
tmplProperty := template.Must(packageInfo.Parse(string(`Windows Registry Editor Version 5.00 | ||
[{{.RegPath}}\Interrupt Management] | ||
[{{.RegPath}}\Interrupt Management\Affinity Policy] | ||
"DevicePolicy"=dword:{{printf "%08d" .Device.DevicePolicy}} | ||
{{if eq .Device.DevicePriority 0}}"DevicePriority"=-{{else}}"DevicePriority"=dword:{{printf "%08d" .Device.DevicePriority}}{{end}} | ||
{{if ne .Device.DevicePolicy 4}}"AssignmentSetOverride"=-{{else}}"AssignmentSetOverride"=hex:{{.AssignmentSetOverride}}{{end}} | ||
[{{.RegPath}}\Interrupt Management\MessageSignaledInterruptProperties] | ||
"MSISupported"=dword:{{printf "%08d" .Device.MsiSupported}} | ||
{{if eq .Device.MsiSupported 1}}{{if ne .Device.MessageNumberLimit 0}}"MessageNumberLimit"=dword:{{printf "%08d" .Device.MessageNumberLimit}}{{end}}{{else}}"MessageNumberLimit"=-{{end}} | ||
`))) | ||
|
||
var buf bytes.Buffer | ||
err := tmplProperty.Execute(&buf, struct { | ||
RegPath string | ||
Device Device | ||
AssignmentSetOverride string | ||
}{ | ||
regpath, | ||
*item, | ||
addComma(fmt.Sprintf("%x", item.AssignmentSetOverride)), | ||
}) | ||
|
||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
return strings.ReplaceAll(buf.String(), "\n", "\r\n") | ||
|
||
} | ||
|
||
func addComma(data string) string { | ||
var b strings.Builder | ||
for i := 0; i < len(data); i++ { | ||
if i != 0 && i%2 == 0 { | ||
b.WriteString(",") | ||
} | ||
b.WriteString(string(data[i])) | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
func saveFileExplorer(owner walk.Form, path, filename, title, filter string) (filePath string, cancel bool, err error) { | ||
dlg := new(walk.FileDialog) | ||
|
||
dlg.Title = title | ||
dlg.InitialDirPath = path | ||
dlg.Filter = filter | ||
dlg.FilePath = filename | ||
|
||
ok, err := dlg.ShowSave(owner) | ||
if err != nil { | ||
return "", !ok, err | ||
} else if !ok { | ||
return "", !ok, nil | ||
} | ||
|
||
return dlg.FilePath, !ok, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters