|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace PInvokeSamples |
| 5 | +{ |
| 6 | + public static class Program |
| 7 | + { |
| 8 | + // Define a delegate that has the same signature as the native function. |
| 9 | + private delegate int DirClbk(string fName, StatClass stat, int typeFlag); |
| 10 | + |
| 11 | + // Import the libc and define the method to represent the native function. |
| 12 | + [DllImport("libc.so.6")] |
| 13 | + private static extern int ftw(string dirpath, DirClbk cl, int descriptors); |
| 14 | + |
| 15 | + // Implement the above DirClbk delegate; |
| 16 | + // this one just prints out the filename that is passed to it. |
| 17 | + private static int DisplayEntry(string fName, StatClass stat, int typeFlag) |
| 18 | + { |
| 19 | + Console.WriteLine(fName); |
| 20 | + return 0; |
| 21 | + } |
| 22 | + |
| 23 | + public static void Main(string[] args) |
| 24 | + { |
| 25 | + // Call the native function. |
| 26 | + // Note the second parameter which represents the delegate (callback). |
| 27 | + ftw(".", DisplayEntry, 10); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // The native callback takes a pointer to a struct. The below class |
| 32 | + // represents that struct in managed code. You can find more information |
| 33 | + // about this in the section on marshalling below. |
| 34 | + [StructLayout(LayoutKind.Sequential)] |
| 35 | + public class StatClass |
| 36 | + { |
| 37 | + public uint DeviceID; |
| 38 | + public uint InodeNumber; |
| 39 | + public uint Mode; |
| 40 | + public uint HardLinks; |
| 41 | + public uint UserID; |
| 42 | + public uint GroupID; |
| 43 | + public uint SpecialDeviceID; |
| 44 | + public ulong Size; |
| 45 | + public ulong BlockSize; |
| 46 | + public uint Blocks; |
| 47 | + public long TimeLastAccess; |
| 48 | + public long TimeLastModification; |
| 49 | + public long TimeLastStatusChange; |
| 50 | + } |
| 51 | +} |
0 commit comments