rbranson/node-ffi — Issue #35: <pointer>.getStruct
Author: raphaelr
Created: 2012-03-03 19:09:11+00:00
State: open

There is no way to read a struct from a pointer.

Example: The [GetAdaptersAddresses](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365915.aspx) function from the Windows API. It has an out parameter called `AdapterAddresses`. This parameter should be a buffer large enough to hold some/many `IP_ADAPTER_ADDRESSES` structs, plus space for the strings referenced by the structs:

var iphlpapi = new ffi.Library('iphlpapi', {
'GetAdaptersAddresses': ['ulong', ['ulong', 'ulong', 'pointer', 'pointer', 'pointer']]
});

iphlpapi.IPAdapterAddresses = FFI.Struct([
['ulong', 'length'], ['ulong', 'ifIndex'],
['pointer', 'next'], ['string', 'name'],
// etc.
]);

function getAdaptersAddresses()
{
var size = new ffi.Pointer(ffi.TYPE_SIZE_MAP.ulong);
size.setULong(15 * 1024);
var buffer = new ffi.Pointer(size.getULong());
iphlpapi.GetAdaptersAddresses(0, some_flags, ffi.Pointer.NULL, buffer, size);
// And now?
}

But there is no function to get a struct from a pointer, so the only way to use the function is to use the `pointer.getXXXX` functions directly.

Proposal: Give pointers a `getStruct` function, which takes the `Struct`s constructor and returns an instance of it with the fields filled out as in memory.