Ejemplo n.º 1
0
func init() {
	// Library
	libgdi32, _ = syscall.LoadLibrary("gdi32.dll")
	libuser32, _ = syscall.LoadLibrary("user32.dll")
	liboleaut32, _ = syscall.LoadLibrary("oleaut32.dll")
	libkernel32, _ = syscall.LoadLibrary("kernel32.dll")

	stretchDIBits, _ = syscall.GetProcAddress(libgdi32, "StretchDIBits")

	getClientRect, _ = syscall.GetProcAddress(libuser32, "GetClientRect")
	defWindowProc, _ = syscall.GetProcAddress(libuser32, "DefWindowProcW")
	loadCursor, _ = syscall.GetProcAddress(libuser32, "LoadCursorW")
	registerClassEx, _ = syscall.GetProcAddress(libuser32, "RegisterClassExW")
	adjustWindowRect, _ = syscall.GetProcAddress(libuser32, "AdjustWindowRect")
	createWindowEx, _ = syscall.GetProcAddress(libuser32, "CreateWindowExW")
	showWindow, _ = syscall.GetProcAddress(libuser32, "ShowWindow")
	getDC, _ = syscall.GetProcAddress(libuser32, "GetDC")
	getMessage, _ = syscall.GetProcAddress(libuser32, "GetMessageW")
	translateMessage, _ = syscall.GetProcAddress(libuser32, "TranslateMessage")
	dispatchMessage, _ = syscall.GetProcAddress(libuser32, "DispatchMessageW")
	invalidateRect, _ = syscall.GetProcAddress(libuser32, "InvalidateRect")
	validateRect, _ = syscall.GetProcAddress(libuser32, "ValidateRect")
	sendMessage, _ = syscall.GetProcAddress(libuser32, "SendMessageW")
	postMessage, _ = syscall.GetProcAddress(libuser32, "PostMessageW")
	destroyWindow, _ = syscall.GetProcAddress(libuser32, "DestroyWindow")

	sysAllocString, _ = syscall.GetProcAddress(liboleaut32, "SysAllocString")

	getLastError, _ = syscall.GetProcAddress(libkernel32, "GetLastError")
}
Ejemplo n.º 2
0
func XAudio2Create(ppXAudio2 **IXAudio2) (ret int32) {
	libole32, _ := syscall.LoadLibrary("ole32.dll")
	ptrcoInitializeEx, _ = syscall.GetProcAddress(syscall.Handle(libole32), "CoInitializeEx")
	ptrcoCreateInstance, _ = syscall.GetProcAddress(syscall.Handle(libole32), "CoCreateInstance")

	coInitializeEx(nil, 0)

	// XAudio 2.7 GUIDs
	clsid := guid{0x5a508685, 0xa254, 0x4fba, [8]byte{0x9b, 0x82, 0x9a, 0x24, 0xb0, 0x03, 0x06, 0xaf}}
	iid := guid{0x8bcf1f58, 0x9fe7, 0x4583, [8]byte{0x8a, 0xc6, 0xe2, 0xad, 0xc4, 0x65, 0xc8, 0xbb}}
	var com unsafe.Pointer
	ret = coCreateInstance(&clsid, nil, 1, &iid, &com)
	if ret < 0 {
		return
	}

	pXAudio2 := (*IXAudio2)(com)
	ret = pXAudio2.Initialize(0, 0xffffffff)
	if ret < 0 {
		pXAudio2.Release()
		return
	}

	*ppXAudio2 = pXAudio2
	return
}
Ejemplo n.º 3
0
func testFail() {
	h, err := syscall.LoadLibrary("msvcrt")
	if err != nil {
		fmt.Println(err)
		return
	}

	defer syscall.FreeLibrary(h)

	proc, err := syscall.GetProcAddress(h, "memcpy")
	if err != nil {
		fmt.Println(err)
		return
	}

	// dst := unsafe.Pointer(uintptr(1))
	// src := unsafe.Pointer(uintptr(2))
	// count := unsafe.Pointer(uintptr(4))
	dst := uintptr(1)
	src := uintptr(2)
	count := uintptr(3)

	fmt.Println("test memcpy error in go...")
	r, _, _ := syscall.Syscall(uintptr(proc), 3, dst, src, count)

	fmt.Println(r)
	fmt.Println("should not run here")

}
Ejemplo n.º 4
0
// Returns total and free bytes available in a directory.
// It returns free space available to the user (including quota limitations),
// so it can be lower than the free space of the disk.
func DiskSpace(path string) (total, free int64, err error) {
	kernel32, err := syscall.LoadLibrary("Kernel32.dll")
	if err != nil {
		return
	}
	defer syscall.FreeLibrary(kernel32)
	GetDiskFreeSpaceEx, err := syscall.GetProcAddress(
		syscall.Handle(kernel32), "GetDiskFreeSpaceExW")
	if err != nil {
		return
	}
	lpFreeBytesAvailable := int64(0)
	lpTotalNumberOfBytes := int64(0)
	lpTotalNumberOfFreeBytes := int64(0)
	r1, _, e1 := syscall.Syscall6(uintptr(GetDiskFreeSpaceEx), 4,
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
		uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
		uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
		uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)), 0, 0)
	if r1 == 0 {
		if e1 != 0 {
			err = error(e1)
		} else {
			err = syscall.EINVAL
		}
		return
	}
	total = lpTotalNumberOfBytes
	free = lpFreeBytesAvailable
	return
}
Ejemplo n.º 5
0
// *************************************************************************
// We can probably delete this, it is an alternative version in case we have
// problems with simpler version above
// *************************************************************************
func freeDiskSpaceBytes2(dir string) (uint64, error) {
	kernel32, err := syscall.LoadLibrary("Kernel32.dll")
	if err != nil {
		log.Panic(err)
	}
	defer syscall.FreeLibrary(kernel32)
	GetDiskFreeSpaceEx, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GetDiskFreeSpaceExW")
	if err != nil {
		return 0, err
	}
	lpFreeBytesAvailable := int64(0)
	lpTotalNumberOfBytes := int64(0)
	lpTotalNumberOfFreeBytes := int64(0)
	r, a, b := syscall.Syscall6(
		uintptr(GetDiskFreeSpaceEx),
		4,
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("C:"))),
		uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
		uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
		uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)),
		0,
		0,
	)
	log.Print("Disk space:", r, a, b, lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes)
	return uint64(lpFreeBytesAvailable), nil
}
Ejemplo n.º 6
0
func main() {
	//首先,准备输入参数, GetDiskFreeSpaceEx需要4个参数, 可查MSDN
	// dir := "C:"
	lpFreeBytesAvailable := int64(0) //注意类型需要跟API的类型相符
	lpTotalNumberOfBytes := int64(0)
	lpTotalNumberOfFreeBytes := int64(0)

	//获取方法的引用
	kernel32, err := syscall.LoadLibrary("Kernel32.dll") // 严格来说需要加上 defer syscall.FreeLibrary(kernel32)
	if err != nil {
		log.Println(err.Error())
	}
	defer syscall.FreeLibrary(kernel32)
	GetDiskFreeSpaceEx, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GetDiskFreeSpaceExW")

	//执行之. 因为有4个参数,故取Syscall6才能放得下. 最后2个参数,自然就是0了
	r, _, errno := syscall.Syscall6(uintptr(GetDiskFreeSpaceEx), 4,
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("C:"))),
		uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
		uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
		uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)), 0, 0)

	if errno != 0 {
		log.Println(errno)
	}
	// 注意, errno并非error接口的, 不可能是nil
	// 而且,根据MSDN的说明,返回值为0就fail, 不为0就是成功
	if r != 0 {
		log.Printf("Free %dmb", lpTotalNumberOfFreeBytes/1024/1024)
	}
}
Ejemplo n.º 7
0
func (jst *IDCGeneral) Initdriver(pin *driverlayer.DriverArg) {
	var err error
	jst.termdll, err = syscall.LoadLibrary("third_party/general/sdtapi.dll")
	if err != nil {
		panic("load library error:" + err.Error())
	}

	f := func(n string, res *uintptr) bool {
		*res, err = syscall.GetProcAddress(jst.termdll, n)
		if err != nil {
			syscall.FreeLibrary(jst.termdll)
			panic("load proc " + n + " error:" + err.Error())
		}
		return true
	}

	//获得dll的各种handle
	if f("SDT_OpenPort", &jst.initcomm) &&
		f("SDT_ClosePort", &jst.closecomm) &&
		f("SDT_StartFindIDCard", &jst.findcard) &&
		f("SDT_SelectIDCard", &jst.selectcard) &&
		f("SDT_ReadBaseMsg", &jst.readbasemsg) {
	} else {
		utils.Error("Init shensi Driver error...%s", err.Error())
	}

}
Ejemplo n.º 8
0
func loadDll(fname string) uint32 {
	h, e := syscall.LoadLibrary(fname)
	if e != 0 {
		abortf("LoadLibrary(%s) failed with err=%d.\n", fname, e)
	}
	return h
}
Ejemplo n.º 9
0
func Rename(src, dst string) error {
	kernel32, err := syscall.LoadLibrary("kernel32.dll")
	if err != nil {
		return err
	}
	defer syscall.FreeLibrary(kernel32)
	moveFileExUnicode, err := syscall.GetProcAddress(kernel32, "MoveFileExW")
	if err != nil {
		return err
	}

	srcString, err := syscall.UTF16PtrFromString(src)
	if err != nil {
		return err
	}

	dstString, err := syscall.UTF16PtrFromString(dst)
	if err != nil {
		return err
	}

	srcPtr := uintptr(unsafe.Pointer(srcString))
	dstPtr := uintptr(unsafe.Pointer(dstString))

	MOVEFILE_REPLACE_EXISTING := 0x1
	flag := uintptr(MOVEFILE_REPLACE_EXISTING)

	_, _, callErr := syscall.Syscall(uintptr(moveFileExUnicode), 3, srcPtr, dstPtr, flag)
	if callErr != 0 {
		return callErr
	}

	return nil
}
Ejemplo n.º 10
0
func loadDll(fname string) uint32 {
	h, e := syscall.LoadLibrary(fname)
	if e != 0 {
		panic("LoadLibrary failed")
	}
	return h
}
Ejemplo n.º 11
0
func (jst *ZZFinger) Initdriver(pin *driverlayer.DriverArg) {
	var err error
	jst.stdll, err = syscall.LoadLibrary("third_party/zhongzheng/FingerDLL.dll")
	if err != nil {
		panic("LoadLibrary " + err.Error())
	}
	jst.read, err = syscall.GetProcAddress(jst.stdll, "drive")
}
Ejemplo n.º 12
0
func LoadLib(name string) uintptr {
	lib, err := syscall.LoadLibrary(name)
	if err != nil {
		panic(fmt.Sprintf("syscall.LoadLibrary('%s') failed: %s",
			name, err.Error()))
	}
	return uintptr(lib)
}
Ejemplo n.º 13
0
func MustLoadLibrary(name string) uint32 {
	lib, errno := syscall.LoadLibrary(name)
	if errno != 0 {
		panic(fmt.Sprintf(`syscall.LoadLibrary("%s") failed: %s`, name, syscall.Errstr(errno)))
	}

	return lib
}
Ejemplo n.º 14
0
func (p *Dll) LoadLibrary(fileName string) error {
	if handler, e := syscall.LoadLibrary(fileName); e != nil {
		return e
	} else {
		p.dllHandler = handler
	}
	return nil
}
Ejemplo n.º 15
0
func MustLoadLibrary(name string) uintptr {
	lib, err := syscall.LoadLibrary(name)
	if err != nil {
		panic(err)
	}

	return uintptr(lib)
}
Ejemplo n.º 16
0
func init() {
	k32, err := syscall.LoadLibrary("kernel32.dll")
	if err != nil {
		panic("LoadLibrary " + err.Error())
	}
	defer syscall.FreeLibrary(k32)
	versionAddr, err = syscall.GetProcAddress(k32, "GetVersion")
}
Ejemplo n.º 17
0
func (jst *HTPingjia) Initdriver(pin *driverlayer.DriverArg) {
	var err error
	jst.htdll, err = syscall.LoadLibrary("estimator_ht.dll")
	if err != nil {
		panic("LoadLibrary " + err.Error())
	}
	jst.startEstimate, err = syscall.GetProcAddress(jst.htdll, "StartEstimate")
}
Ejemplo n.º 18
0
func MustLoadLibrary(name string) uintptr {
	lib, err := syscall.LoadLibrary(name)
	if err != nil {
		panic("DLL < " + name + " > failed to load," + err.Error())
	}

	return uintptr(lib)
}
Ejemplo n.º 19
0
func init() {
	kernel32, loadErr := syscall.LoadLibrary("kernel32.dll")
	if loadErr != nil {
		fmt.Println("loadErr", loadErr)
	}
	defer syscall.FreeLibrary(kernel32)
	_SetConsoleTitle, _ = syscall.GetProcAddress(kernel32, "SetConsoleTitleW")

}
Ejemplo n.º 20
0
func init() {
	var nStdHandle = -11
	kernel32, _ = syscall.LoadLibrary("Kernel32.dll")
	GetStdHandle, _ = syscall.GetProcAddress(syscall.Handle(kernel32), "GetStdHandle")
	SetConsoleTextAttribute, _ = syscall.GetProcAddress(syscall.Handle(kernel32), "SetConsoleTextAttribute")
	//	ReadConsoleOutputAttribute, _ := syscall.GetProcAddress(syscall.Handle(kernel32), "ReadConsoleOutputAttribute")
	GetConsoleScreenBufferInfo, _ = syscall.GetProcAddress(syscall.Handle(kernel32), "GetConsoleScreenBufferInfo")
	hnd, _, _ = syscall.Syscall(uintptr(GetStdHandle), 1, uintptr(nStdHandle), 0, 0)
}
Ejemplo n.º 21
0
func (jst *STICCard) Initdriver(pin *driverlayer.DriverArg) {
	var err error
	jst.termdll, err = syscall.LoadLibrary("third_party/shengteng/iccard_shengteng.dll")
	if err != nil {
		panic("load library error:" + err.Error())
	}
	jst.read, err = syscall.GetProcAddress(jst.termdll, "drive")

}
Ejemplo n.º 22
0
func (jst *TCFinger) Initdriver(pin *driverlayer.DriverArg) {
	var err error
	jst.stdll, err = syscall.LoadLibrary("third_party/teso/TesoLive.dll")
	if err != nil {
		panic("LoadLibrary " + err.Error())
	}
	jst.getRegisterImage, err = syscall.GetProcAddress(jst.stdll, "FPIGetTemplate")
	jst.getValidteImage, err = syscall.GetProcAddress(jst.stdll, "FPIGetFeature")
	jst.matchtz, err = syscall.GetProcAddress(jst.stdll, "FPIMatch")
}
Ejemplo n.º 23
0
func init() {
	k32, err := syscall.LoadLibrary("kernel32.dll")
	if err != nil {
		panic("LoadLibrary " + err.Error())
	}
	defer syscall.FreeLibrary(k32)
	nGetOverlappedResult = getProcAddr(k32, "GetOverlappedResult")
	nCreateEvent = getProcAddr(k32, "CreateEventW")
	nResetEvent = getProcAddr(k32, "ResetEvent")
}
Ejemplo n.º 24
0
func Open() *iup.Error {
	h, err := syscall.LoadLibrary("iupweb.dll")
	if err != nil {
		return &iup.Error{"LoadLibrary iupweb.dll false"}
	}
	proc, err := syscall.GetProcAddress(h, "IupWebBrowserOpen")
	if err != nil {
		return &iup.Error{"GetProcAddress IupWebBrowserOpen false"}
	}
	syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
	return nil
}
Ejemplo n.º 25
0
func init() {
	modwinmm, err := syscall.LoadLibrary("winmm.dll")
	if err != 0 {
		log.Print("LoadLibrary for winmm.dll failed:", err)
		return
	}

	playsoundproc, err = syscall.GetProcAddress(modwinmm, "PlaySoundW")
	if err != 0 {
		log.Print("GetProcAddress for PlaySoundW failed:", err)
	}
}
Ejemplo n.º 26
0
func main() {
	h, err := syscall.LoadLibrary("kernel32.dll")
	if err != nil {
		abort("LoadLibrary", err)
	}
	defer syscall.FreeLibrary(h)
	proc, err := syscall.GetProcAddress(h, "GetVersion")
	if err != nil {
		abort("GetProcAddress", err)
	}
	r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
	print_version(uint32(r))
}
Ejemplo n.º 27
0
func init() {
	dll, err := syscall.LoadLibrary("PowerPointWrapper.dll")
	if err != nil {
		panic(err)
	}

	procInitialize, err = syscall.GetProcAddress(dll, "Initialize")
	if err != nil {
		panic(err)
	}

	procUninitialize, err = syscall.GetProcAddress(dll, "Uninitialize")
	if err != nil {
		panic(err)
	}

	procPresentationCurrentSlideIndex, err = syscall.GetProcAddress(dll, "PresentationCurrentSlideIndex")
	if err != nil {
		panic(err)
	}

	procPresentationTotalSlidesCount, err = syscall.GetProcAddress(dll, "PresentationTotalSlidesCount")
	if err != nil {
		panic(err)
	}

	procPresentationPreviousSlide, err = syscall.GetProcAddress(dll, "PresentationPreviousSlide")
	if err != nil {
		panic(err)
	}

	procPresentationNextSlide, err = syscall.GetProcAddress(dll, "PresentationNextSlide")
	if err != nil {
		panic(err)
	}

	procPresentationCurrentSlideName, err = syscall.GetProcAddress(dll, "PresentationCurrentSlideName")
	if err != nil {
		panic(err)
	}

	procPresentationCurrentSlideNote, err = syscall.GetProcAddress(dll, "PresentationCurrentSlideNote")
	if err != nil {
		panic(err)
	}

	procRefreshPresentationSlidesThumbnail, err = syscall.GetProcAddress(dll, "RefreshPresentationSlidesThumbnail")
	if err != nil {
		panic(err)
	}
}
Ejemplo n.º 28
0
func callKernel32Procedure(procedureName string, procedureCaller func(uintptr) error) (err error) {
	kernelHandle, err := syscall.LoadLibrary("kernel32.dll")
	if err != nil {
		return err
	}
	defer syscall.FreeLibrary(kernelHandle)

	procedureAddress, err := syscall.GetProcAddress(kernelHandle, procedureName)
	if err != nil {
		return err
	}

	return procedureCaller(procedureAddress)
}
Ejemplo n.º 29
0
// Full path of the current executable
func GetExecutableFileName() string {
	kernel32, _ := syscall.LoadLibrary("kernel32.dll")
	defer syscall.FreeLibrary(kernel32)
	b := make([]uint16, syscall.MAX_PATH)
	getModuleFileName, _ := syscall.GetProcAddress(kernel32, "GetModuleFileNameW")
	ret, _, callErr := syscall.Syscall(uintptr(getModuleFileName),
		3, 0,
		uintptr(unsafe.Pointer(&b[0])),
		uintptr(len(b)))
	if callErr != 0 {
		panic(fmt.Sprintf("GetModuleFileNameW : err %d", int(callErr)))
	}
	return string(utf16.Decode(b[:ret]))
}
Ejemplo n.º 30
0
func init() {
	k32, err := syscall.LoadLibrary("kernel32.dll")
	if err != nil {
		panic("LoadLibrary " + err.Error())
	}
	defer syscall.FreeLibrary(k32)

	nSetCommState = getProcAddr(k32, "SetCommState")
	nSetCommTimeouts = getProcAddr(k32, "SetCommTimeouts")
	nSetCommMask = getProcAddr(k32, "SetCommMask")
	nSetupComm = getProcAddr(k32, "SetupComm")
	nGetOverlappedResult = getProcAddr(k32, "GetOverlappedResult")
	nCreateEvent = getProcAddr(k32, "CreateEventW")
	nResetEvent = getProcAddr(k32, "ResetEvent")
}