Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible Array Member convenience method #16

Open
marler8997 opened this issue Aug 30, 2022 · 0 comments
Open

Flexible Array Member convenience method #16

marler8997 opened this issue Aug 30, 2022 · 0 comments

Comments

@marler8997
Copy link
Collaborator

Win32 will sometimes use a "flexible array member" to copy data to/from the application. The GetInterfaceInfo function is an example:

https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getinterfaceinfo

//...

    dwRetVal = GetInterfaceInfo(NULL, &ulOutBufLen);
    if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) {
        pInfo = (IP_INTERFACE_INFO *) MALLOC(ulOutBufLen);
        if (pInfo == NULL) {
            printf
                ("Unable to allocate memory needed to call GetInterfaceInfo\n");
            return 1;
        }
    }

// Make a second call to GetInterfaceInfo to get
// the actual data we need
    dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen);
    if (dwRetVal == NO_ERROR) {
        printf("Number of Adapters: %ld\n\n", pInfo->NumAdapters);
        for (i = 0; i < pInfo->NumAdapters; i++) {
            printf("Adapter Index[%d]: %ld\n", i,
                   pInfo->Adapter[i].Index);
            printf("Adapter Name[%d]: %ws\n\n", i,
                   pInfo->Adapter[i].Name);
        }
//...

Zig could make this easier to do with a convenience method, i.e.

pub const IP_INTERFACE_INFO = extern struct {
    NumAdapters: i32,
    Adapter: [1]IP_ADAPTER_INDEX_MAP,
    pub fn AdapterSlice(self: *IP_INTERFACE_INFO) []IP_ADAPTER_INDEX_MAP {
        return @ptrCast([*]IP_ADAPTER_INDEX_MAP, &self.Adapter)[0 .. self.NumAdapters];
    }
};

The tricky part here would be knowing that NumAdapters is the length of the slice, but I think that info is available in the metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant