DLL库动态加载类_第1页
DLL库动态加载类_第2页
DLL库动态加载类_第3页
DLL库动态加载类_第4页
DLL库动态加载类_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、DLL库动态加载类/*# # DLLLOAD.H#*/#ifndef _DLL_LOAD_H_#define _DLL_LOAD_H_/= / class TDllModule/ Base class for dynamically loaded DLL's/=class TDllModule public:enum TErrorCode ERROR_OK = 0,ERROR_DLLNOTFOUND,ERROR_INVALIDEXPORT,ERROR_NOTCREATED;protected:int m_nErrorCode;CString m_strName;HINSTANCE m_

2、hHandle;public:TDllModule(LPCTSTR szName;virtual TDllModule(;virtual BOOL Create(void;virtual void Destroy(void;int GetErrorCode(void return m_nErrorCode; CString GetDLLName(void return m_strName; ;#endif/*# # End of file DLLLOAD.H#*/*# # DLLLOAD.CPP#*/#include"stdafx.h"#include"DllLo

3、ad.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/= / class TDllModule/=TDllModule:TDllModule(LPCTSTR szNamem_strName = szName;m_hHandle = NULL;m_nErrorCode = ERROR_NOTCREATED;TDllModule:TDllModule(Destroy(;/ Dynamically load a DLL/ Returns TRUE if loa

4、dedBOOL TDllModule:Create(voidm_hHandle = :LoadLibrary(m_strName;if (m_hHandle = NULL m_nErrorCode = ERROR_DLLNOTFOUND;return FALSE;m_nErrorCode = ERROR_OK;return TRUE;/ Unload the DLLvoid TDllModule:Destroy(voidif (m_hHandle :FreeLibrary(m_hHandle;m_hHandle = NULL;m_nErrorCode = ERROR_NOTCREATED;/*

5、# # End of file DLLLOAD.CPP#*/对话框实例:/ SampleDlg.h : header file/#if !defined(AFX_SAMPLEDLG_H_CDDAAEA6_B78B_11D3_8F9F_0080AD4311B1_INCLUDED_ #define AFX_SAMPLEDLG_H_CDDAAEA6_B78B_11D3_8F9F_0080AD4311B1_INCLUDED_#if _MSC_VER > 1000#pragma once#endif/ _MSC_VER > 1000/ / PSAPI wrapper/typedef BOOL

6、 (WINAPI *TFEnumProcesses(DWORD * lpidProcess, DWORD cb, DWORD * cbNeeded;typedef BOOL (WINAPI *TFEnumProcessModules(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded;typedef DWORD (WINAPI *TFGetModuleBaseName(HANDLE hProcess, HMODULE hModule, LPSTR lpBaseName, DWORD nSize;typedef DW

7、ORD (WINAPI *TFGetModuleFileNameEx(HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize;typedef BOOL (WINAPI *TFGetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, DWORD cb;class TProcessStatusModule : public TDllModule private:TFEnumProcesses FEnumProcesses;TFE

8、numProcessModules FEnumProcessModules;TFGetModuleBaseName FGetModuleBaseName;TFGetModuleFileNameEx FGetModuleFileNameEx;TFGetModuleInformation FGetModuleInformation;private:void initAll(void;public:TProcessStatusModule(;virtual TProcessStatusModule(;virtual BOOL Create(void;virtual void Destroy(void

9、;BOOL EnumProcesses(DWORD * lpidProcess, DWORD cb, DWORD * cbNeeded;BOOL EnumProcessModules(HANDLE hProcess,HMODULE *lphModule,DWORD cb,LPDWORD lpcbNeeded; DWORD GetModuleBaseName(HANDLE hProcess,HMODULE hModule,LPSTR lpBaseName,DWORD nSize; DWORD GetModuleFileNameEx(HANDLE hProcess,HMODULE hModule,

10、LPSTR lpFilename,DWORD nSize; BOOL GetModuleInformation(HANDLE hProcess,HMODULE hModule,LPMODULEINFO lpmodinfo,DWORD cb;/ CSampleDlg dialogclass CSampleDlg : public CDialog/ Constructionpublic:CSampleDlg(CWnd* pParent = NULL; / standard constructor/ Dialog Data/AFX_DATA(CSampleDlgenum IDD = IDD_SAMP

11、LE_DIALOG ;CListCtrl m_List;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CSampleDlgprotected:virtual void DoDataExchange(CDataExchange* pDX; / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:HICON m_hIcon;/ Generated message map functions/AFX_MSG(CSampleDlgvirtual BOO

12、L OnInitDialog(;afx_msg void OnPaint(;afx_msg HCURSOR OnQueryDragIcon(;afx_msg void OnEnum(; /按钮事件/AFX_MSGDECLARE_MESSAGE_MAP(public:;/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif/ !defined(AFX_SAMPLEDLG_H_CDDAAEA6_B78B_11D3

13、_8F9F_0080AD4311B1_INCLUDED_/*# End of file SampleDlg.h#*/ SampleDlg.cpp : implementation file/#include"stdafx.h"#include"Sample.h"#include"SampleDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ PSAPI wrapper/TProcessStatusM

14、odule:TProcessStatusModule(:TDllModule(_T("PSAPI.DLL"initAll(;TProcessStatusModule:TProcessStatusModule(void TProcessStatusModule:initAll(voidFEnumProcesses = NULL;FEnumProcessModules = NULL;FGetModuleInformation = NULL;FGetModuleBaseName = NULL;FGetModuleFileNameEx = NULL;BOOL TProcessSta

15、tusModule:Create(voidif (TDllModule:Create( / DLL handle is valid (DLL is loadedFEnumProcesses = (TFEnumProcesses:GetProcAddress(m_hHandle,_T("EnumProcesses" FEnumProcessModules =(TFEnumProcessModules:GetProcAddress(m_hHandle,_T("EnumProcessModules"FGetModuleInformation =(TFGetMo

16、duleInformation:GetProcAddress(m_hHandle,_T("GetModuleInformation"#ifdef UNICODEFGetModuleBaseName =(TFGetModuleBaseName:GetProcAddress(m_hHandle,_T("GetModuleBaseNameW"FGetModuleFileNameEx =(TFGetModuleFileNameEx:GetProcAddress(m_hHandle,_T("GetModuleFileNameExW"#elseF

17、GetModuleBaseName =(TFGetModuleBaseName:GetProcAddress(m_hHandle,_T("GetModuleBaseNameA"FGetModuleFileNameEx =(TFGetModuleFileNameEx:GetProcAddress(m_hHandle,_T("GetModuleFileNameExA"#endif/ Can also check if all pointers to functions are different from NULL.return TRUE;return FA

18、LSE;void TProcessStatusModule:Destroy(voidTDllModule:Destroy(;initAll(;BOOL TProcessStatusModule:EnumProcesses(DWORD *lpidProcess, DWORD cb, DWORD *cbNeededASSERT(FEnumProcesses != NULL;if (FEnumProcessesreturn FEnumProcesses(lpidProcess,cb,cbNeeded;return FALSE;BOOL TProcessStatusModule:EnumProcess

19、Modules(HANDLE hProcess,HMODULE *lphModule,DWORD cb,LPDWORD lpcbNeeded ASSERT(FEnumProcessModules != NULL;if (FEnumProcessModulesreturn FEnumProcessModules(hProcess, lphModule, cb, lpcbNeeded;return FALSE;DWORD TProcessStatusModule:GetModuleBaseName(HANDLE hProcess,HMODULE hModule,LPSTR lpBaseName,D

20、WORD nSize ASSERT(FGetModuleBaseName != NULL;if (FGetModuleBaseNamereturn FGetModuleBaseName(hProcess, hModule, lpBaseName, nSize;return FALSE;DWORD TProcessStatusModule:GetModuleFileNameEx(HANDLE hProcess,HMODULE hModule,LPSTR lpFilename,DWORD nSize ASSERT(FGetModuleFileNameEx != NULL;if (FGetModul

21、eFileNameExreturn FGetModuleFileNameEx(hProcess, hModule, lpFilename, nSize;return FALSE;BOOL TProcessStatusModule:GetModuleInformation(HANDLE hProcess,HMODULE hModule,LPMODULEINFO lpmodinfo,DWORD cb ASSERT(FGetModuleInformation != NULL;if (FGetModuleInformationreturn FGetModuleInformation(hProcess,

22、 hModule, lpmodinfo, cb;return FALSE;/ / CSampleDlg dialogCSampleDlg:CSampleDlg(CWnd* pParent /*=NULL*/: CDialog(CSampleDlg:IDD, pParent/AFX_DATA_INIT(CSampleDlg/ NOTE: the ClassWizard will add member initialization here /AFX_DATA_INITm_hIcon = AfxGetApp(->LoadIcon(IDR_MAINFRAME;void CSampleDlg:D

23、oDataExchange(CDataExchange* pDXCDialog:DoDataExchange(pDX;/AFX_DATA_MAP(CSampleDlgDDX_Control(pDX, IDC_LIST, m_List;/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CSampleDlg, CDialog/AFX_MSG_MAP(CSampleDlgON_WM_PAINT(ON_WM_QUERYDRAGICON(ON_BN_CLICKED(IDC_ENUM, OnEnum/AFX_MSG_MAPON_BN_CLICKED(IDOK, &CSampleDlg:

24、OnBnClickedOkEND_MESSAGE_MAP(/ / CSampleDlg message handlersBOOL CSampleDlg:OnInitDialog(CDialog:OnInitDialog(;SetIcon(m_hIcon, TRUE; / Set big iconSetIcon(m_hIcon, FALSE; / Set small icon/ TODO: Add extra initialization herem_List.InsertColumn(0, _T("Process ID", LVCFMT_LEFT, 60;m_List.In

25、sertColumn(1, _T("Process name", LVCFMT_LEFT, 400;return TRUE; / return TRUE unless you set the focus to a control/ If you add a minimize button to your dialog, you will need the code below / to draw the icon. For MFC applications using the document/view model, / this is automatically done

26、 for you by the framework.void CSampleDlg:OnPaint(if (IsIconic(CPaintDC dc(this; / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM dc.GetSafeHdc(, 0;/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON;int cyIcon = GetSystemMetrics(SM_CYICON;CRect rect;GetClientRect(▭int x = (rect.Width( - cxIcon + 1 / 2;int y = (rect.Height( - cyIcon + 1 / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon;elseCDialog:OnPaint(;HCURSOR CSampleDlg:OnQueryDragIcon(return (HCURSOR m_hIcon;void CSampleDlg

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论