动态库加载方法
来源:互联网 发布:macbook安装软件找不到 编辑:程序博客网 时间:2024/04/30 15:15
Introduction
Have you ever got tired of loading Dynamic Link Libraries the long way, with the usual steps LoadLibrary, and GetProcAddress, then you have to check for each function address if they are NULL, and don't mention about casting the function pointer and hard ways that make your brain strain. And wish there was an easier way to get around things? Well this will just do that in a way and is about the easiest way I know of actually
Collapse code snippet Copy Code
- //GetProcAddresses
- //Argument1: hLibrary - Handle for the Library Loaded
- //Argument2: lpszLibrary - Library to Load
- //Argument3: nCount - Number of functions to load
- //[Arguments Format]
- //Argument4: Function Address - Function address we want to store
- //Argument5: Function Name - Name of the function we want
- //[Repeat Format]
- //
- //Returns: FALSE if failure
- //Returns: TRUE if successful
- BOOL GetProcAddresses( HINSTANCE *hLibrary,
- LPCSTR lpszLibrary, INT nCount, ... )
- {
- va_list va;
- va_start( va, nCount );
- if ( ( *hLibrary = LoadLibrary( lpszLibrary ) )
- != NULL )
- {
- FARPROC * lpfProcFunction = NULL;
- LPSTR lpszFuncName = NULL;
- INT nIdxCount = 0;
- while ( nIdxCount < nCount )
- {
- lpfProcFunction = va_arg( va, FARPROC* );
- lpszFuncName = va_arg( va, LPSTR );
- if ( ( *lpfProcFunction =
- GetProcAddress( *hLibrary,
- lpszFuncName ) ) == NULL )
- {
- lpfProcFunction = NULL;
- return FALSE;
- }
- nIdxCount++;
- }
- }
- else
- {
- va_end( va );
- return FALSE;
- }
- va_end( va );
- return TRUE;
- }
So since we now have the main core to this article, lets now look at how to use this with a short sample that was compiled as a Windows console application.
Collapse Copy Code
- #include <windows.h>
- typedef int ( WINAPI *MESSAGEBOX )
- ( HWND , LPCSTR, LPCSTR, DWORD );
- typedef int ( WINAPI *MESSAGEBOXEX )
- ( HWND , LPCSTR, LPCSTR, DWORD , WORD );
- void main(void)
- {
- MESSAGEBOX lpfMsgBox = NULL;
- MESSAGEBOXEX lpfMsgBoxEx = NULL;
- HINSTANCE hLib;
- if(GetProcAddresses( &hLib, "User32.dll", 2,
- &lpfMsgBox, "MessageBoxA",
- &lpfMsgBoxEx, "MessageBoxExA" ) )
- {
- lpfMsgBox( 0, "Test1", "Test1", MB_OK );
- lpfMsgBoxEx( 0, "Test2", "Test2", MB_OK,
- MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ) );
- }
- if ( hLib != NULL )
- FreeLibrary( hLib );
- }
- 动态库加载方法
- 动态加载链接库方法
- 主程序加载动态链接库(类方法)(动态加载)
- VC++动态链接库的加载方法
- 加载动态链接库的方法
- .NET 动态加载类库的方法
- .net加载子目录中动态库方法
- javascript动态加载方法
- 动态加载JS方法
- linux中动态加载动态库的方法
- 动态库的静态和动态加载方法
- linux中动态加载动态库的方法
- linux中动态加载动态库的方法
- 动态加载动态库
- 动态加载动态库
- 三种动态加载方法
- 实用@selector动态加载方法
- linux动态加载内核方法
- DLL学习系列一:c++静态链接库
- htaccess
- 构建高性能J2EE应用的五种核心策略
- Linux Shell命令集锦
- DLL学习系列之二:非MFC动态链接库
- 动态库加载方法
- 瑞星在windows登录界面图标按钮
- sql server迁移到oracle
- C# 特性(Attribute)学习。
- 代码精英(不断更新中)
- sqlmetal
- 常用网址收集
- C# 里怎样得到当前执行的函数名,当前代码行,源代码文件名。
- Oracle数据库中表与视图的应用