static void TestSimpleDllLoadLibrary() { Console.WriteLine("Simple:Use DllLoadLibrary,"); IntPtr hModule = Win32.LoadLibrary("testCppDll.dll"); int error = Marshal.GetLastWin32Error(); if (hModule == IntPtr.Zero) { return; } unsafe { Add addFunction = (Add)UseDllByLoadLibrary.GetFunctionAddress(hModule, "Add", typeof(Add)); error = Marshal.GetLastWin32Error(); Console.WriteLine(addFunction(1.0, 2.0)); } Win32.FreeLibrary(hModule); Console.WriteLine("Simple:Test END\n"); }
public void TestComplexLoadLibrary() { IntPtr hModule = Win32.LoadLibrary("testCppDll.dll"); int error = Marshal.GetLastWin32Error(); //Console.WriteLine("The Last Win32 Error was: " + error); if (hModule == IntPtr.Zero) { return; } unsafe { InitFunc initFunc = (InitFunc)UseDllByLoadLibrary.GetFunctionAddress(hModule, "Initialize", typeof(InitFunc)); bool isInit = initFunc(""); if (isInit) { string text = ""; FindNames findNamesFunc = (FindNames)UseDllByLoadLibrary.GetFunctionAddress(hModule, "FindNames", typeof(FindNames)); IntPtr arrayPtr = IntPtr.Zero; uint arraySize; if (findNamesFunc(text, out arrayPtr, out arraySize)) { NameEntity[] names = new NameEntity[arraySize]; IntPtr cur = arrayPtr; for (int i = 0; i < arraySize; i++) { names[i] = new NameEntity(); Marshal.PtrToStructure(cur, names[i]); Marshal.DestroyStructure(cur, typeof(NameEntity)); cur = (IntPtr)((int)cur + Marshal.SizeOf(names[i])); } Assert.AreEqual(names.Count(), arraySize); } Assert.AreNotEqual(arrayPtr, IntPtr.Zero); Marshal.FreeCoTaskMem(arrayPtr); } } Win32.FreeLibrary(hModule); }