开发者

How to check if DLL entry point exists in C# without calling the function

I'm using OpenTK OpenGL wrapper. Since it loads OpenGL dll (or .so on Linux) it contains a开发者_Go百科 lot of DLL imported functions.

The trouble is, some drivers don't export all of the functions. Is there a way to check if the entry point exists? I need to do this since actually calling the function on systems that have it will cause a crash if not done in the proper sequence. So catching EntryPointNotFound exception doesn't work in my case.


You can P/Invoke the LoadLibrary and GetProcAddress calls from Win32:

[DllImport("kernel32", SetLastError=true)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);

Use LoadLibrary to load the module and get the handle, and GetProcAddress to get a function pointer to the entry point. If the latter returns an error, the entry point doesn't exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜