示例#1
0
        public void Setup()
        {
            IntPtr hModule = Win32.LoadLibrary("testCppDll.dll");
            int    error   = Marshal.GetLastWin32Error();

            //Console.WriteLine("The Last Win32 Error was: " + error);
            if (hModule == IntPtr.Zero)
            {
                return;
            }
            addFunction = (Add)UseComplexDllByLoadLibrary.GetFunctionAddress(hModule, "Add", typeof(Add));
        }
示例#2
0
文件: main.cs 项目: lpwwpl/testCSharp
        static void TestComplexDllLoadLibrary()
        {
            Console.WriteLine("Complex:Use DllLoadLibrary, ");
            PrintCurrentProcessMemUsage();
            IntPtr hModule = Win32.LoadLibrary("testCppDll.dll");
            int    error   = Marshal.GetLastWin32Error();
            //NameEntityType type = NameEntityType.OrganizationName;
            int type = 0;

            if (hModule == IntPtr.Zero)
            {
                return;
            }
            {
                //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);
                }
            }
            Win32.FreeLibrary(hModule);
            PrintCurrentProcessMemUsage();
            Console.WriteLine("Complex: Test END\n");
        }
示例#3
0
        public bool CreateNameEntityByTypeByParam(int value, ref NameEntity nameEntity)
        {
            int    type    = 0;
            IntPtr hModule = Win32.LoadLibrary("testCppDll.dll");
            CreateNameEntityByTypeDelegate createNameEntity = (CreateNameEntityByTypeDelegate)UseComplexDllByLoadLibrary.GetFunctionAddress(hModule, "CreateNameEntityByType", typeof(CreateNameEntityByTypeDelegate));
            IntPtr namePtr = IntPtr.Zero;

            if (createNameEntity(out namePtr, type))
            {
                nameEntity = (NameEntity)Marshal.PtrToStructure(namePtr, typeof(NameEntity));
                // 在非托管代码中使用CoTaskMemAlloc分配的内存,可以使用Marshal.FreeCoTaskMem方法释放
                Marshal.FreeCoTaskMem(namePtr);
                Console.WriteLine("非托管函数返回的结构体数据:double = {0:f6}, int = {1}",
                                  nameEntity._score, nameEntity._highlightLength);
            }
            Win32.FreeLibrary(hModule);
            return(true);
        }