コード例 #1
0
ファイル: TestMarshalUtils.cs プロジェクト: msiva21/DnsLibs
        public void TestManualMarshalStringToPtr()
        {
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            try
            {
                string      testString  = "hello, my name is Test!";
                StringClass stringClass = new StringClass
                {
                    SimpleString = testString
                };

                OtherStruct otherStruct = new OtherStruct
                {
                    OtherInt    = 42,
                    OtherString = "blablabla"
                };

                StringStructure stringStructure = new StringStructure
                {
                    OtherField = otherStruct
                };

                Assert.Throws <ArgumentNullException>(() =>
                                                      MarshalUtils.AllStringsToPtrs <StringClass, StringStructure>(
                                                          null,
                                                          ref stringStructure,
                                                          allocatedPointers));

                Assert.Throws <ArgumentNullException>(() =>
                                                      MarshalUtils.AllStringsToPtrs(
                                                          stringClass,
                                                          ref stringStructure,
                                                          null));

                MarshalUtils.AllStringsToPtrs(
                    stringClass,
                    ref stringStructure,
                    allocatedPointers);

                string restoredString = MarshalUtils.PtrToString(stringStructure.SimpleString);
                Assert.AreEqual(stringClass.SimpleString, restoredString);
                Assert.AreEqual(1, allocatedPointers.Count);
                Assert.AreEqual(stringStructure.OtherField.OtherInt, otherStruct.OtherInt);
                Assert.AreEqual(stringStructure.OtherField.OtherString, otherStruct.OtherString);
            }
            finally
            {
                MarshalUtils.SafeFreeHGlobal(allocatedPointers);
            }
        }
コード例 #2
0
ファイル: TestMarshalUtils.cs プロジェクト: msiva21/DnsLibs
        public void TestInvalidManualMarshalStringToPtr()
        {
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            try
            {
                string             testString  = "hello, my name is Test!";
                InvalidStringClass stringClass = new InvalidStringClass
                {
                    SimpleString = testString.ToArray()
                };

                StringStructure stringStructure = new StringStructure();
                Assert.Throws <InvalidOperationException>(() =>
                                                          MarshalUtils.AllStringsToPtrs(
                                                              stringClass,
                                                              ref stringStructure,
                                                              allocatedPointers));
            }
            finally
            {
                MarshalUtils.SafeFreeHGlobal(allocatedPointers);
            }
        }