I trying to call WindowsAPI with syscall from golang package (try not to use cgo) but I'm facing uintptr
I have no idea how to access the data that uintptr have its address
Here it code I'm doing
package soq
import (
"fmt"
"net"
"syscall"
"unsafe"
)
const (
retErrInsufficientBuffer = 122
)
var (
kernel32, _ = syscall.LoadLibrary("kernel32")
heapAlloc, _ = syscall.GetProcAddress(kernel32, "HeapAlloc")
heapFree, _ = syscall.GetProcAddress(kernel32, "HeapFree")
getProcessHeap, _ = syscall.GetProcAddress(kernel32, "GetProcessHeap")
processHeap, _, _ = syscall.Syscall6(getProcessHeap, 0, 0, 0, 0, 0, 0, 0)
libIphlpapi, _ = syscall.LoadLibrary("iphlpapi")
pGetUDPTable, _ = syscall.GetProcAddress(libIphlpapi, "GetUdpTable")
pUDPTable uintptr
)
// UDPTableRow .
type UDPTableRow struct {
LocalAddr net.IP
LocalPort uint32
}
// UDPTable .
type UDPTable struct {
NumEntries uint32
Table []UDPTableRow
}
// HeapAlloc .
func HeapAlloc(size uint32) uintptr {
pMem, _, _ := syscall.Syscall(
heapAlloc, 3,
processHeap,
0,
uintptr(size),
)
return pMem
}
// HeapFree .
func HeapFree(pMem uintptr) {
syscall.Syscall(
heapFree,
3,
processHeap,
0,
pMem,
)
}
// GetUDPTable .
func GetUDPTable() (*UDPTable, error) {
var dwSize uint32
if r1, _, _ := syscall.Syscall(
pGetUDPTable,
3,
pUDPTable,
uintptr(unsafe.Pointer(&dwSize)),
0,
); r1 == retErrInsufficientBuffer {
HeapFree(pUDPTable)
pUDPTable = HeapAlloc(dwSize)
}
if r1, _, _ := syscall.Syscall(
pGetUDPTable,
3,
pUDPTable,
uintptr(unsafe.Pointer(&dwSize)),
0,
); r1 != 0 {
fmt.Println("GetUdpTable() failed with return value", r1)
}
/*
How to retrive data from "pUDPTable"
It's structure is
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366930(v=vs.85).aspx
typedef struct _MIB_UDPTABLE {
DWORD dwNumEntries;
MIB_UDPROW table[ANY_SIZE];
} MIB_UDPTABLE, *PMIB_UDPTABLE;
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366926(v=vs.85).aspx
typedef struct _MIB_UDPROW {
DWORD dwLocalAddr;
DWORD dwLocalPort;
} MIB_UDPROW, *PMIB_UDPROW;
*/
fmt.Printf("%#v", pUDPTable)
return nil, nil
}