I have to interact with a provided C library where one of the functions is declared as follows:
int GetFileList(struct spiflash_file **ptr_spiflash_filelist, int **file_num);
where**ptr_spiflash_filelist is a pointer to a pointer that represent an array ofspiflash_file structs, and**file_num is a pointer to a pointer of the total file counts.
I am having quite the trouble in marshalling those 2 parameters...This is what I came up with until now:
[DllImport(LibName, SetLastError = true)] private static extern int GetFileList(out IntPtr ptr_spiflash_filelist, out IntPtr file_num);This does not throw exceptions nor errors, but when I inspect the two variables I get absurd values ( i.e. on an empty flash the file count is 1790873936...)
I can't understand how to properly do it and it feels like I am missing just a couple steps..
- Looks like you are getting a pointer. Use PtrToStructure :learn.microsoft.com/en-us/dotnet/api/…jdweng– jdweng2024-02-14 14:22:12 +00:00CommentedFeb 14, 2024 at 14:22
- for the first parameter, ok, but what about the second? a
out refseems to be the correct way, but it ouputs garbage data.Marcomattia Mocellin– Marcomattia Mocellin2024-02-14 14:31:31 +00:00CommentedFeb 14, 2024 at 14:31 - How do you know how large the array is?Charlieface– Charlieface2024-02-14 15:49:49 +00:00CommentedFeb 14, 2024 at 15:49
- is
unsafecode an option here? i.e.private static unsafe extern int GetFileList(ptr_spiflash_filelist** ptr_spiflash_filelist, int** file_num);? Usage would be something likeptr_spiflash_filelist* ptr = null; int* file_num = null; GetFileList(&ptr, &file_num);, whichshould leave the two locals initializedMarc Gravell– Marc Gravell2024-02-14 16:00:25 +00:00CommentedFeb 14, 2024 at 16:00 - What value do you get for 2nd number? What do you expect? It may not be an integer (32 bits), or unsigned instead of signed.jdweng– jdweng2024-02-14 18:21:35 +00:00CommentedFeb 14, 2024 at 18:21
