예제 #1
0
        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
        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);
            }
        }
예제 #3
0
        public static string RemoveDotsFromNames(this string code)
        {
            var result          = new StringBuilder();
            var stringStructure = new StringStructure();

            for (int i = 0; i < code.Length; i++)
            {
                stringStructure.Handle(code[i]);
                if (i > 0 && i < code.Length - 1 &&
                    code[i] == '.')
                {
                    if (!stringStructure.IsInsideString &&
                        IsInsideWord(i, code))
                    {
                        continue;
                    }
                }

                result.Append(code[i]);
            }

            return(result.ToString());
        }
예제 #4
0
        public static string ReplaceUnknownSymbols(this string code)
        {
            var result = new StringBuilder();

            var stringStructure = new StringStructure();

            for (var i = 0; i < code.Length; i++)
            {
                stringStructure.Handle(code[i]);

                if (char.IsLetter(code[i]) || char.IsDigit(code[i]) || stringStructure.IsInsideString || NotReplaceChars.Contains(code[i]))
                {
                    result.Append(code[i]);
                }
                else
                {
                    var bytes       = Encoding.Unicode.GetBytes(code[i].ToString());
                    var replacement = "u" + string.Join("_", bytes.Select(b => b.ToString()));
                    result.Append(replacement);
                }
            }

            return(result.ToString());
        }
예제 #5
0
        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);
            }
        }