public void TestInvalidManualMarshalPtrToString() { IntPtr pTestString = IntPtr.Zero; try { string testString = "hello, my name is Test!"; pTestString = MarshalUtils.StringToPtr(testString); InvalidStringStructure stringStructure = new InvalidStringStructure { SimpleString = (int)pTestString }; StringClass stringClass = new StringClass(); Assert.Throws <InvalidOperationException>(() => MarshalUtils.AllPtrsToStrings(stringStructure, stringClass)); } finally { MarshalUtils.SafeFreeHGlobal(pTestString); } }
public void TestManualMarshalPtrToString() { IntPtr pTestString = IntPtr.Zero; try { string testString = "hello, my name is Test!"; pTestString = MarshalUtils.StringToPtr(testString); StringStructure stringStructure = new StringStructure { SimpleString = pTestString }; OtherStruct otherStruct = new OtherStruct { OtherInt = 42, OtherString = "blablabla" }; StringClass stringClass = new StringClass { OtherField = otherStruct }; Assert.Throws <ArgumentNullException>(() => MarshalUtils.AllPtrsToStrings <StringStructure, StringClass>( stringStructure, null)); MarshalUtils.AllPtrsToStrings(stringStructure, stringClass); Assert.NotNull(stringClass.SimpleString); Assert.AreEqual(stringClass.SimpleString, testString); Assert.AreEqual(stringClass.OtherField.OtherInt, otherStruct.OtherInt); Assert.AreEqual(stringClass.OtherField.OtherString, otherStruct.OtherString); } finally { MarshalUtils.SafeFreeHGlobal(pTestString); } }