The DllMain function is an optional entry point into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary functions.
DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.
当我们的动态链接库不再使用时可以调用FreeLibrary使动态链接库使用计数减1,当使用计数为零时,系统会收回模块资源 FreeLibrary The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid.
int _stdcall add(int a,int b) ...{ return a+b; } int _stdcall subtract(int a,int b) ...{ return a-b; }
TEST
void CDllTestDlg::OnPaint() ...{ if (IsIconic()) ...{ CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon +1) /2; int y = (rect.Height() - cyIcon +1) /2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else ...{ CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDllTestDlg::OnQueryDragIcon() ...{ return (HCURSOR) m_hIcon; } //extern int add(int a,int b); //extern int subtract(int a,int b); //_declspec(dllimport) int add(int a,int b); //_declspec(dllimport) int subtract(int a,int b); void CDllTestDlg::OnBtnAdd() ...{ // TODO: Add your control notification handler code here /**//* CString str; str.Format("5+3=%d",add(5,3)); MessageBox(str);*/ HINSTANCE hInst; hInst=LoadLibrary("Dll3.dll"); typedef int (/**//*_stdcall*/*ADDPROC)(int a,int b); //ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"?add@@YAHHH@Z"); ADDPROC Add=(ADDPROC)GetProcAddress(hInst,MAKEINTRESOURCE(1)); if(!Add) ...{ MessageBox("获取函数地址失败!"); return; } CString str; str.Format("5+3=%d",Add(5,3)); MessageBox(str); FreeLibrary(hInst); } void CDllTestDlg::OnBtnSubtract() ...{ // TODO: Add your control notification handler code here /**//* CString str; str.Format("5-3=%d",subtract(5,3)); MessageBox(str);*/ } void CDllTestDlg::OnBtnOutput() ...{ // TODO: Add your control notification handler code here /**//* Point pt; pt.output(5,3);*/
_declspec(dllexport) int add(int a,int b) ...{ return a+b; }