开发者

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

开发者 https://www.devze.com 2023-03-17 10:03 出处:网络
I\'m using OpenTK OpenGL wrapper. Since it loads OpenGL dll (or .so on Linux) it contains a开发者_Go百科 lot of DLL imported functions.

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

精彩评论

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