go - golang, call GetVolumeInformation winapi function -
tries call getvolumeinformation function golang. want volume name.
use spec's of api:
bool winapi getvolumeinformation( _in_opt_ lpctstr lprootpathname, _out_opt_ lptstr lpvolumenamebuffer, _in_ dword nvolumenamesize, _out_opt_ lpdword lpvolumeserialnumber, _out_opt_ lpdword lpmaximumcomponentlength, _out_opt_ lpdword lpfilesystemflags, _out_opt_ lptstr lpfilesystemnamebuffer, _in_ dword nfilesystemnamesize );
use code:
// test package main import ( "fmt" "syscall" "unsafe" ) func main() { var lprootpathname = "c:\\" var lpvolumenamebuffer string var nvolumenamesize uint64 var lpvolumeserialnumber uint64 var lpmaximumcomponentlength uint64 var lpfilesystemflags uint64 var lpfilesystemnamebuffer string var nfilesystemnamesize uint32 kernel32, _ := syscall.loadlibrary("kernel32.dll") getvolume, _ := syscall.getprocaddress(kernel32, "getvolumeinformationw") var nargs uintptr = 8 ret, _, callerr := syscall.syscall9(uintptr(getvolume), nargs, uintptr(unsafe.pointer(syscall.stringtoutf16ptr(lprootpathname))), uintptr(unsafe.pointer(&lpvolumenamebuffer)), uintptr(unsafe.pointer(&nvolumenamesize)), uintptr(unsafe.pointer(&lpvolumeserialnumber)), uintptr(unsafe.pointer(&lpmaximumcomponentlength)), uintptr(unsafe.pointer(&lpfilesystemflags)), uintptr(unsafe.pointer(&lpfilesystemnamebuffer)), uintptr(unsafe.pointer(&nfilesystemnamesize)), 0) fmt.println(ret, callerr, lpvolumenamebuffer) }
... , have error :(
unexpected fault address 0xffffffffffffffff fatal error: fault [signal 0xc0000005 code=0x0 addr=0xffffffffffffffff pc=0x456b11]
don't understand , google cant'd calling winapi functions , returng string result.
thank's.
package unsafe contains operations step around type safety of go programs.
type pointer *arbitrarytype
pointer represents pointer arbitrary type. there 4 special operations available type pointer not available other types.
1) pointer value of type can converted pointer.
2) pointer can converted pointer value of type.
3) uintptr can converted pointer.
4) pointer can converted uintptr.
pointer therefore allows program defeat type system , read , write arbitrary memory. should used extreme care.
you failed heed warning unsafe.pointer
"should used extreme care."
try this:
package main import ( "fmt" "syscall" "unsafe" ) func main() { var rootpathname = `c:\` var volumenamebuffer = make([]uint16, syscall.max_path+1) var nvolumenamesize = uint32(len(volumenamebuffer)) var volumeserialnumber uint32 var maximumcomponentlength uint32 var filesystemflags uint32 var filesystemnamebuffer = make([]uint16, 255) var nfilesystemnamesize uint32 = syscall.max_path + 1 kernel32, _ := syscall.loadlibrary("kernel32.dll") getvolume, _ := syscall.getprocaddress(kernel32, "getvolumeinformationw") var nargs uintptr = 8 ret, _, callerr := syscall.syscall9(uintptr(getvolume), nargs, uintptr(unsafe.pointer(syscall.stringtoutf16ptr(rootpathname))), uintptr(unsafe.pointer(&volumenamebuffer[0])), uintptr(nvolumenamesize), uintptr(unsafe.pointer(&volumeserialnumber)), uintptr(unsafe.pointer(&maximumcomponentlength)), uintptr(unsafe.pointer(&filesystemflags)), uintptr(unsafe.pointer(&filesystemnamebuffer[0])), uintptr(nfilesystemnamesize), 0) fmt.println(ret, callerr, syscall.utf16tostring(volumenamebuffer)) }
Comments
Post a Comment