private static void StructAndArraySamples() { PInvokeWrapper.DisplayBetterCar(new Car2() { Car = new Car() { Color = "Black", Make = "Toyota" }, PetName = "Blacky" }); foreach (var car in PInvokeWrapper.GiveMeThreeBasicCarsHelper()) { Console.WriteLine(car.Make); } var cars = new CarStruct[3]; PInvokeWrapper.FillThreeBasicCars(cars); foreach (var car in cars) { Console.WriteLine(car.Make); } var makes = new string[3]; int length = 0; PInvokeWrapper.GiveMeMakes(out makes, out length); }
// Helper function to consume imported GiveMeThreeBasicCars function public static IEnumerable <Car> GiveMeThreeBasicCarsHelper() { const int size = 3; var result = new List <Car>(size); // Pass in an IntPtr as an output parameter. IntPtr outArray; PInvokeWrapper.GiveMeThreeBasicCars(out outArray); try { // Helper for iterating over array elements IntPtr current = outArray; for (int i = 0; i < size; i++) { // Get next car using Marshal.PtrToStructure() var car = Marshal.PtrToStructure <Car>(current); result.Add(car); // Calculate location of next structure using Marshal.SizeOf(). current = (IntPtr)((int)current + Marshal.SizeOf <Car>()); } } finally { // Free memory for the allocated array. Marshal.FreeCoTaskMem(outArray); } return(result); }
private static void CalculationFunctionExample() { Console.WriteLine(PInvokeWrapper.AddNumbers(1, 2)); var source = new[] { 1, 2, 3 }; Console.WriteLine(PInvokeWrapper.AddArray(source, source.Length)); }
private static void Win32Samples() { PInvokeWrapper.DisplayMessage(0, "Hello World!", "Greeting", 0); PInvokeWrapper.DisplayMessage(999, "Hello World!", "Greeting", 0); Console.WriteLine("Last Win32 Error: {0}", Marshal.GetLastWin32Error()); Console.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message); }
private static void ClassSamples() { var miniVan = PInvokeWrapper.CreateMiniVan(); try { Console.WriteLine(PInvokeWrapper.GetNumberOfSeats(miniVan)); } finally { PInvokeWrapper.DeleteMiniVan(miniVan); } }
private static void CallbackSamples() { PInvokeWrapper.CallMeBackToSayHello(() => Console.WriteLine("\tHello from C#")); PInvokeWrapper.ReportPythagorasBack(1, 2, t => Console.WriteLine(t.c)); }