main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //go:build windows
  2. package yu_win
  3. import (
  4. yu_math "gogs.qqck.cn/s/tools/math"
  5. _ "syscall"
  6. _ "unsafe"
  7. )
  8. //go:linkname SyscallN syscall.SyscallN
  9. func SyscallN(addr uintptr, args ...uintptr) (r1, r2, err uintptr)
  10. type Code byte
  11. const (
  12. Gbk Code = iota
  13. Unicode
  14. )
  15. type S struct {
  16. // Code
  17. //
  18. // @Description: 要将字符串转换为哪种编码,转换后会自动在尾部补充 \x00 结束符
  19. // utf8 请直接传递 string,无需此高级类型,utf8 请自行决定是否在尾部补充 \x00 结束符
  20. Code Code
  21. // Utf8
  22. //
  23. // @Description: 欲转换的 utf8 字符串
  24. Utf8 string
  25. }
  26. type Address uintptr
  27. func (t Address) CallInt(args ...any) int {
  28. j_i, _, _ := t.Call(args...)
  29. return int(j_i)
  30. }
  31. func (t Address) CallInt32(args ...any) int32 {
  32. j_i, _, _ := t.Call(args...)
  33. return int32(j_i)
  34. }
  35. func (t Address) CallUint(args ...any) uint {
  36. j_i, _, _ := t.Call(args...)
  37. return uint(j_i)
  38. }
  39. func (t Address) CallUint32(args ...any) uint32 {
  40. j_i, _, _ := t.Call(args...)
  41. return uint32(j_i)
  42. }
  43. func (t Address) CallBool(args ...any) bool {
  44. j_i, _, _ := t.Call(args...)
  45. return j_i == 1
  46. }
  47. func (t Address) CallUintptr(args ...any) uintptr {
  48. j_i, _, _ := t.Call(args...)
  49. return j_i
  50. }
  51. func (t Address) CallGbkToUtf8(args ...any) string {
  52. j_str, _, _ := t.Call(args...)
  53. if j_str == 0 {
  54. return ""
  55. }
  56. j_unicode_len, _, _ := MultiByteToWideChar.Call(936, 0, j_str, uint(yu_math.MaxUint), 0, 0)
  57. if j_unicode_len < 1 {
  58. return ""
  59. }
  60. j_unicode_len *= 2
  61. j_unicode := make([]byte, j_unicode_len+2) // 结尾 {0,0}
  62. MultiByteToWideChar.Call(936, 0, j_str, uint(yu_math.MaxUint), j_unicode, j_unicode_len)
  63. j_utf8_size, _, _ := WideCharToMultiByte.Call(65001, 0, j_unicode, uint(yu_math.MaxUint), 0, 0, 0, 0)
  64. j_utf8_size -= 1
  65. if j_utf8_size < 1 {
  66. return ""
  67. }
  68. j_utf8 := make([]byte, j_utf8_size)
  69. WideCharToMultiByte.Call(65001, 0, j_unicode, uint(yu_math.MaxUint), j_utf8, j_utf8_size, 0, 0)
  70. return string(j_utf8)
  71. }
  72. func (t Address) CallUnicodeToUtf8(args ...any) string {
  73. j_ptr, _, _ := t.Call(args...)
  74. if j_ptr == 0 {
  75. return ""
  76. }
  77. j_utf8_size, _, _ := WideCharToMultiByte.Call(65001, 0, j_ptr, uint(yu_math.MaxUint), 0, 0, 0, 0)
  78. j_utf8_size -= 1
  79. if j_utf8_size < 1 {
  80. return ""
  81. }
  82. j_utf8 := make([]byte, j_utf8_size)
  83. WideCharToMultiByte.Call(65001, 0, j_ptr, uint(yu_math.MaxUint), j_utf8, j_utf8_size, 0, 0)
  84. return string(j_utf8)
  85. }
  86. type Module uintptr
  87. func LoadLibrary(path string) Module {
  88. return Module(LoadLibraryW.CallUintptr(S{Unicode, path}))
  89. }
  90. func (t Module) GetProcAddress(name string) Address {
  91. return Address(GetProcAddress.CallUintptr(uintptr(t), name+"\x00"))
  92. }