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

askedFeb 14, 2024 at 14:13
Marcomattia Mocellin's user avatar
5
  • Looks like you are getting a pointer. Use PtrToStructure :learn.microsoft.com/en-us/dotnet/api/…CommentedFeb 14, 2024 at 14:22
  • for the first parameter, ok, but what about the second? aout ref seems to be the correct way, but it ouputs garbage data.CommentedFeb 14, 2024 at 14:31
  • How do you know how large the array is?CommentedFeb 14, 2024 at 15:49
  • isunsafe 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 likeptr_spiflash_filelist* ptr = null; int* file_num = null; GetFileList(&ptr, &file_num);, whichshould leave the two locals initializedCommentedFeb 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.CommentedFeb 14, 2024 at 18:21

0

Know someone who can answer? Share a link to thisquestion viaemail,Twitter, orFacebook.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.