1

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 of spiflash_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..

5
  • Looks like you are getting a pointer. Use PtrToStructure : learn.microsoft.com/en-us/dotnet/api/… Commented Feb 14, 2024 at 14:22
  • for the first parameter, ok, but what about the second? a out ref seems to be the correct way, but it ouputs garbage data. Commented Feb 14, 2024 at 14:31
  • How do you know how large the array is? Commented Feb 14, 2024 at 15:49
  • is unsafe code an option here? i.e. private static unsafe extern int GetFileList(ptr_spiflash_filelist** ptr_spiflash_filelist, int** file_num); ? Usage would be something like ptr_spiflash_filelist* ptr = null; int* file_num = null; GetFileList(&ptr, &file_num);, which should leave the two locals initialized Commented Feb 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. Commented Feb 14, 2024 at 18:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.