static void Main(string[] args) { // Add some numbers. Console.WriteLine("Invoking AddNumbers()..."); Console.WriteLine("10 + 10 is {0}", MyCustomDLLWrapper.AddNumbers(10, 10)); // Add array of numbers. Console.WriteLine("\nInvoking AddArray()..."); int[] theVals = { 10, 23, 83, 9, 12 }; Console.WriteLine("Sum of array is {0}", MyCustomDLLWrapper.AddArray(theVals, 5)); // Display a better car. Console.WriteLine("\nInvoking DisplayBetterCar()..."); Console.WriteLine("...message boxes are displaying..."); CAR2 myCar = new CAR2(); myCar.petName = "Frank"; myCar.theCar.color = "Rust"; myCar.theCar.make = "Colt"; MyCustomDLLWrapper.DisplayBetterCar(myCar); // Get three basic cars. Console.WriteLine("\nInvoking GiveMeThreeBasicCars()..."); int size = 3; IntPtr outArray; MyCustomDLLWrapper.GiveMeThreeBasicCars(out outArray); CAR[] carArray = new CAR[size]; IntPtr current = outArray; // Print out each structure. for (int i = 0; i < size; i++) { carArray[i] = new CAR(); Marshal.PtrToStructure(current, carArray[i]); Console.WriteLine("Structure {0}: {1} {2}", i, carArray[i].make, carArray[i].color); Marshal.DestroyStructure(current, typeof(CAR)); current = (IntPtr)((int)current + Marshal.SizeOf(carArray[i])); } Marshal.FreeCoTaskMem(outArray); outArray = IntPtr.Zero; Marshal.FreeCoTaskMem(current); current = IntPtr.Zero; // Get a new CMiniVan type. Console.WriteLine("\nInvoking CMiniVan.DisplayNumberOfKids()..."); IntPtr instancePtr = MyCustomDLLWrapper.CreateMiniVan(); int kidCount = MyCustomDLLWrapper.GetTheKids(instancePtr); Console.WriteLine("Number of kids in Mini Van is: {0} \n", kidCount); MyCustomDLLWrapper.DeleteMiniVan(instancePtr); }
static void Main(string[] args) { // Add some numbers. Console.WriteLine("Invoking AddNumbers()..."); Console.WriteLine("10 + 10 is {0}", MyCustomDLLWrapper.AddNumbers(10, 10)); // Add array of numbers. Console.WriteLine("\nInvoking AddArray()..."); int[] theVals = {10, 23, 83, 9, 12}; Console.WriteLine("Sum of array is {0}", MyCustomDLLWrapper.AddArray(theVals, 5)); // Display a better car. Console.WriteLine("\nInvoking DisplayBetterCar()..."); Console.WriteLine("...message boxes are displaying..."); CAR2 myCar = new CAR2(); myCar.petName = "Frank"; myCar.theCar.color = "Rust"; myCar.theCar.make = "Colt"; MyCustomDLLWrapper.DisplayBetterCar(myCar); // Get three basic cars. Console.WriteLine("\nInvoking GiveMeThreeBasicCars()..."); int size = 3; IntPtr outArray; MyCustomDLLWrapper.GiveMeThreeBasicCars(out outArray ); CAR[] carArray = new CAR[size]; IntPtr current = outArray; // Print out each structure. for( int i = 0; i < size; i++ ) { carArray[ i ] = new CAR(); Marshal.PtrToStructure( current, carArray[ i ]); Console.WriteLine( "Structure {0}: {1} {2}", i, carArray[ i ].make, carArray[ i ].color); Marshal.DestroyStructure( current, typeof(CAR) ); current = (IntPtr)((int)current + Marshal.SizeOf( carArray[ i ] )); } Marshal.FreeCoTaskMem( outArray ); outArray = IntPtr.Zero; Marshal.FreeCoTaskMem( current); current = IntPtr.Zero; // Get a new CMiniVan type. Console.WriteLine("\nInvoking CMiniVan.DisplayNumberOfKids()..."); IntPtr instancePtr = MyCustomDLLWrapper.CreateMiniVan(); int kidCount = MyCustomDLLWrapper.GetTheKids(instancePtr); Console.WriteLine("Number of kids in Mini Van is: {0} \n", kidCount); MyCustomDLLWrapper.DeleteMiniVan(instancePtr); }