Пример #1
0
        public bool CreateNameEntitys(string text, ref List <NameEntity> nameList)
        {
            nameList = new List <NameEntity>();

            IntPtr arrayPtr  = IntPtr.Zero;
            uint   arraySize = 0;

            if (_handle.IsInvalid)
            {
                return(false);
            }
            //CoTaskMem, FreeCoTaskMem
            if (CreateNameEntitys(_handle, text, out arrayPtr, ref arraySize))
            {
                NameEntity[] names = new NameEntity[arraySize];
                IntPtr       cur   = arrayPtr;
                for (int i = 0; i < arraySize; i++)
                {
                    names[i] = new NameEntity();
                    //cur->names, free cur
                    Marshal.PtrToStructure(cur, names[i]);
                    Marshal.DestroyStructure(cur, typeof(NameEntity));
                    cur = (IntPtr)((int)cur + Marshal.SizeOf(names[i]));
                }
                Marshal.FreeCoTaskMem(arrayPtr);
                nameList.AddRange(names);

                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #2
0
        public bool TestComplexDllLoadLibraryByParam(IntPtr nameEntityPtr)
        {
            Console.WriteLine("Complex:Use DllLoadLibrary, ");
            IntPtr hModule = Win32.LoadLibrary("testCppDll.dll");
            int    error   = Marshal.GetLastWin32Error();
            //NameEntityType type = NameEntityType.OrganizationName;
            int type = 0;

            if (hModule == IntPtr.Zero)
            {
                return(false);
            }
            {
                //test findNames func
                CreateNameEntitysByType createNameEntitys = (CreateNameEntitysByType)UseComplexDllByLoadLibrary.GetFunctionAddress(hModule, "CreateNameEntitysByType", typeof(CreateNameEntitysByType));
                string            text      = "";
                IntPtr            arrayPtr  = IntPtr.Zero;
                uint              arraySize = 0;
                List <NameEntity> nameList  = new List <NameEntity>();
                if (createNameEntitys(text, out arrayPtr, ref arraySize, type))
                {
                    NameEntity[] names = new NameEntity[arraySize];
                    IntPtr       cur   = arrayPtr;
                    for (int i = 0; i < arraySize; i++)
                    {
                        names[i] = new NameEntity();
                        //cur->names, free cur
                        Marshal.PtrToStructure(cur, names[i]);
                        Marshal.DestroyStructure(cur, typeof(NameEntity));
                        cur = (IntPtr)((int)cur + Marshal.SizeOf(names[i]));
                    }
                    Marshal.FreeCoTaskMem(arrayPtr);
                    nameList.AddRange(names);
                }
                //createName
                CreateNameEntityByType createNameEntity = (CreateNameEntityByType)UseComplexDllByLoadLibrary.GetFunctionAddress(hModule, "CreateNameEntityByType", typeof(CreateNameEntityByType));
                IntPtr namePtr = IntPtr.Zero;
                if (createNameEntity(out namePtr, type))
                {
                    NameEntity retStruct = (NameEntity)Marshal.PtrToStructure(namePtr, typeof(NameEntity));
                    // 在非托管代码中使用CoTaskMemAlloc分配的内存,可以使用Marshal.FreeCoTaskMem方法释放
                    Marshal.FreeCoTaskMem(namePtr);
                    Console.WriteLine("非托管函数返回的结构体数据:double = {0:f6}, int = {1}",
                                      retStruct._score, retStruct._highlightLength);

                    AppDomain appDomain = AppDomain.CurrentDomain;
                    appDomain.SetData("createNameEntity", retStruct);
                }


                NameEntity paramStruct = (NameEntity)Marshal.PtrToStructure(nameEntityPtr, typeof(NameEntity));
                paramStruct._type = NameEntityType.OrganizationName;
                Marshal.StructureToPtr(paramStruct, nameEntityPtr, false);
                //在非托管代码中使用CoTaskMemAlloc分配的内存,可以使用Marshal.FreeCoTaskMem方法释放
            }
            Win32.FreeLibrary(hModule);
            Console.WriteLine("Complex: Test END\n");
            return(true);
        }
Пример #3
0
        public void TestReturnStructByNew()
        {
            IntPtr     pStruct   = TestReturnNewStruct();
            NameEntity retStruct =
                (NameEntity)Marshal.PtrToStructure(pStruct, typeof(NameEntity));

            // 在非托管代码中使用new/malloc分配的内存,
            // 需要调用对应的释放内存的释放方法将其释放掉
            FreeStruct(pStruct);
            Console.WriteLine("非托管函数返回的结构体数据:double = {0:f6}, int = {1}",
                              retStruct._score, retStruct._highlightLength);
        }
Пример #4
0
        public void TestReturnStructByArg()
        {
            IntPtr ppStruct = IntPtr.Zero;

            TestReturnStructFromArg(ref ppStruct);
            NameEntity retStruct =
                (NameEntity)Marshal.PtrToStructure(ppStruct, typeof(NameEntity));

            // 在非托管代码中使用CoTaskMemAlloc分配的内存,可以使用Marshal.FreeCoTaskMem方法释放
            Marshal.FreeCoTaskMem(ppStruct);
            Console.WriteLine("非托管函数返回的结构体数据:double = {0:f6}, int = {1}",
                              retStruct._score, retStruct._highlightLength);
        }
Пример #5
0
        public bool CreateNameEntity(ref NameEntity nameEntity)
        {
            IntPtr namePtr = IntPtr.Zero;

            if (_handle.IsInvalid)
            {
                return(false);
            }
            if (CreateNameEntity(_handle, out namePtr))
            {
                Marshal.PtrToStructure(namePtr, nameEntity);
                Marshal.DestroyStructure(namePtr, typeof(NameEntity));
                Marshal.FreeCoTaskMem(namePtr);
            }
            return(true);
        }