public unsafe void GetProcAddress_String() { using FreeLibrarySafeHandle moduleHandle = PInvoke.LoadLibrary("kernel32"); FARPROC pGetTickCount = PInvoke.GetProcAddress(moduleHandle, "GetTickCount"); GetTickCountDelegate getTickCount = pGetTickCount.CreateDelegate <GetTickCountDelegate>(); uint ticks = getTickCount(); Assert.NotEqual(0u, ticks); var getTickCountPtr = (delegate * < uint >)pGetTickCount.Value; ticks = getTickCountPtr(); Assert.NotEqual(0u, ticks); }
private unsafe static void TestDllImports(List <string> errors) { MethodInfo[] methods = typeof(NativeMethods).GetMethods(BindingFlags.Public | BindingFlags.Static); var attributes = methods.ToDictionary(x => x, x => x.GetCustomAttribute <DllImportAttribute>()); Dictionary <string, IntPtr> libraries = new Dictionary <string, IntPtr>(); foreach (var attribute in attributes) { if (!libraries.ContainsKey(attribute.Value.Value)) { fixed(char *library = attribute.Value.Value.ToArray()) { IntPtr handle = NativeMethods.LoadLibrary(library); if (handle == IntPtr.Zero) { errors.Add("Library " + attribute.Value.Value + " cannot be loaded."); continue; } libraries[attribute.Value.Value] = handle; } } fixed(byte *entryPoint = Encoding.ASCII.GetBytes(attribute.Value.EntryPoint)) { FARPROC address = NativeMethods.GetProcAddress(libraries[attribute.Value.Value], (sbyte *)entryPoint); if (address == IntPtr.Zero) { errors.Add("Function " + attribute.Value.EntryPoint + " was not found in library " + attribute.Value.Value + "."); continue; } } } }
public static extern bool IsBadCodePtr(FARPROC lpfn);
public static extern System.IntPtr ICOpenFunction(uint fccType, uint fccHandler, uint wMode, FARPROC lpfnHandler);
private static void FARPROC_InSignatureChangedToIntPtr() { FARPROC p = PInvoke.GetProcAddress(default(HINSTANCE), default(PCSTR)); p = PInvoke.GetProcAddress(default(SafeHandle), null); }