private static void MarshalCStyleArray() { int[] intArray = new int[] { 1, 3, 5, 7, 9 }; Console.WriteLine("���������е�Ԫ��Ϊ��"); foreach (int i in intArray) { Console.WriteLine(i); } // ����COM�����ʾ�� MarshalCOMDataTypeClass comObj = new MarshalCOMDataTypeClass(); // �������������ڷ��й��ڴ�����ռ�ڴ�Ĵ�С int bufferSize = Marshal.SizeOf(typeof(int)) * intArray.Length; // Ϊ�������������й��ڴ� IntPtr pArray = Marshal.AllocCoTaskMem(bufferSize); // ������������͵����й��ڴ��� Marshal.Copy(intArray, 0, pArray, intArray.Length); // ����COM��������������Ԫ��֮�� int sum = comObj.MarshalCStylelArray(pArray, intArray.Length); // �ͷŷ��й��ڴ� Marshal.FreeCoTaskMem(pArray); Console.WriteLine("\n������Ԫ��֮��Ϊ��{0}", sum); }
private static void MarshalCOMVariant() { MarshalCOMDataTypeClass comObj = new MarshalCOMDataTypeClass(); object outArg = null; string description = string.Empty; uint uintArg = 10; description = comObj.MarshalCOMVariant(uintArg, out outArg); Console.WriteLine("VARIANT - uint: {0}, {1}, {2}", uintArg.GetType().Name, outArg.GetType().Name, description); double doubleArg = 10.0; description = comObj.MarshalCOMVariant(doubleArg, out outArg); Console.WriteLine("VARIANT - double: {0}, {1}, {2}", doubleArg.GetType().Name, outArg.GetType().Name, description); string stringArg = "�ַ���"; description = comObj.MarshalCOMVariant(stringArg, out outArg); Console.WriteLine("VARIANT - string: {0}, {1}, {2}", stringArg.GetType().Name, outArg.GetType().Name, description); decimal decimalArg = 10.20m; description = comObj.MarshalCOMVariant(decimalArg, out outArg); Console.WriteLine("VARIANT - decimal: {0}, {1}, {2}", decimalArg.GetType().Name, outArg.GetType().Name, description); CurrencyWrapper currencyArg = new CurrencyWrapper(10.20m); description = comObj.MarshalCOMVariant(currencyArg, out outArg); Console.WriteLine("VARIANT - currency: {0}, {1}, {2}", currencyArg.GetType().Name, outArg.GetType().Name, description); DateTime dateArg = new DateTime(2008, 8, 8); description = comObj.MarshalCOMVariant(dateArg, out outArg); Console.WriteLine("VARIANT - date: {0}, {1}, {2}", dateArg.GetType().Name, outArg.GetType().Name, description); object objectArg = new object(); description = comObj.MarshalCOMVariant(objectArg, out outArg); Console.WriteLine("VARIANT - object: {0}, {1}, {2}", objectArg.GetType().Name, outArg.GetType().Name, description); object nullArg = null; description = comObj.MarshalCOMVariant(nullArg, out outArg); Console.WriteLine("VARIANT - null: {0}, {1}, {2}", "null", outArg == null ? "null" : outArg.GetType().Name, description); IntPtr intptrArg = IntPtr.Zero; description = comObj.MarshalCOMVariant(intptrArg, out outArg); Console.WriteLine("VARIANT - IntPtr: {0}, {1}, {2}", intptrArg.GetType().Name, outArg.GetType().Name, description); }
private static void MarshalCOMStructure() { MarshalCOMDataTypeClass comObj = new MarshalCOMDataTypeClass(); SampleStruct sampleStruct = new SampleStruct(); sampleStruct.ID = 1; sampleStruct.stringName = "TestString"; Console.WriteLine("��ʼ�ṹ�����ݣ�\n\tSampleStruct.ID = {0}\n\tSampleStruct.stringName = {1}", sampleStruct.ID, sampleStruct.stringName); comObj.MarshalStructure(ref sampleStruct); Console.WriteLine("���º�ṹ�����ݣ�\n\tSampleStruct.ID = {0}\n\tSampleStruct.stringName = {1}", sampleStruct.ID, sampleStruct.stringName); }
private static void MarshalCommonDataType() { MarshalCOMDataTypeClass comObj = new MarshalCOMDataTypeClass(); // �����ַ� byte outChar = (byte)0; comObj.MarshalChar((byte)'a', ref outChar); Console.WriteLine("MarshalChar: {0}", (char)outChar); sbyte outComChar = (sbyte)0; comObj.MarshalCOMChar((sbyte)'b', ref outComChar); Console.WriteLine("MarshalComChar: {0}", (char)outComChar); // �����ַ��� string outBSTR = String.Empty; comObj.MarshalBSTR("BSTR�ַ���", ref outBSTR); Console.WriteLine("MarshalBSTR: {0}", outBSTR); string outLPSTR = String.Empty; comObj.MarshalLPSTR("LPSTR�ַ���", ref outLPSTR); Console.WriteLine("MarshalLPSTR: {0}", outLPSTR); // ����Decimal��Currency decimal outDecimal = 0; comObj.MarshalDECIMAL(2008.88m, ref outDecimal); Console.WriteLine("MarshalDecimal: {0}", outDecimal); decimal outCurrency = 0; comObj.MarshalCURRENCY(2008.88m, ref outCurrency); Console.WriteLine("MarshalCurrency: {0:C}", outCurrency); // ���Ͳ���ֵ sbyte outBool = 0; comObj.MarshalBoolean(1, ref outBool); Console.WriteLine("UseBoolean: {0}", outBool); bool outVariantBool = false; comObj.MarshalVariantBool(true, ref outVariantBool); Console.WriteLine("UseVariantBool: {0}", outVariantBool); // ����DATE DateTime outDate = new DateTime(); comObj.MarshalDATE(new DateTime(2008, 8, 8, 20, 0, 0), ref outDate); Console.WriteLine("UseDate: {0}", outDate); }
private static void MarshalCOMSafeArray() { MarshalCOMDataTypeClass comObj = new MarshalCOMDataTypeClass(); string[] stringArray = new string[] { "This", "is", "a", "SAFEARRAY", "sample" }; Console.WriteLine("��ʼ�ַ������飺"); foreach (string str in stringArray) { Console.WriteLine(str); } string result = comObj.SortArray(stringArray); Console.WriteLine("\n���÷��ؽ����{0}", result); Console.WriteLine("\n�������ַ������飺"); foreach (string str in stringArray) { Console.WriteLine(str); } }