Esempio n. 1
0
    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);
    }
Esempio n. 2
0
        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;
                    }
                }
            }
        }
Esempio n. 3
0
 public static extern bool IsBadCodePtr(FARPROC lpfn);
Esempio n. 4
0
 public static extern System.IntPtr ICOpenFunction(uint fccType, uint fccHandler, uint wMode, FARPROC lpfnHandler);
Esempio n. 5
0
    private static void FARPROC_InSignatureChangedToIntPtr()
    {
        FARPROC p = PInvoke.GetProcAddress(default(HINSTANCE), default(PCSTR));

        p = PInvoke.GetProcAddress(default(SafeHandle), null);
    }