Microsoft (R) Library Manager Version 6.00.8168 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. Creating library SQLITE.lib and object SQLITE.exp q样成功地创徏了在WIN32E序中访问sqlite所需要的?可以用于链接WIN32E序. 到此所有用sqlite的准备工作已告罄.现在在MSVC6中新Z个Win32 Console Application工程,把sqlite.dll,sqlite.h和sqlite.lib文g复制到工E文件夹?把sqlite.h文g加入到项 目中,然后在Project Setting的Link中的对象库模块中增加sqlite.lib文g. 或者project->add to project->filesQ选择q个lib文g 然后修改加入如下代码卛_:
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); }下面具体介绍昄加蝲 昄加蝲 VC++~译器在~译DLL的时候函C发生名字改编Q主要在非C++环境中就不能识别 该函CQ所以这里应该定义模块文件类型DEFQ主要就方便了非C++E序可以调用 该DLL里面的函? 再用显C加载前必须要注意的是名字的改编问题Q因为再动态加载中名字改编? 在加载就得不原来的函数名字了Q这样加载就会失败。但是可以用另外一U方法加 载:MSDN上对GetProAddress中的W二个参数是q样说明的Pointer to a null-ter minated string that specifies the function or variable name, or the func tion's ordinal value.也就是说可以使用函数的序h调用该函敎ͼ具体使用Ҏ 是ProcAdd = (MYPROC) GetProcAddress(hinstLib, MakeIntResource(i)); (i代表 函数在DLL中的序号Q可以用DUMPBIN工具查看)Q但是一般的都不用这U{换序L 方式来取得函数的地址Q因样非常的不直观!下面q模块定义文gQDEFQ来 避免DLL中函数的名字的改~问?
昄加蝲DLL //MSDN上的对DLLq程昄加蝲的DEMO Using Run-Time Dynamic Linking You can use the same DLL in both load-time and run-time dynamic linking. The following example uses the LoadLibrary function to get a handle to the Myputs DLL (see Creating a Simple Dynamic-Link Library). If LoadLibr ary succeeds, the program uses the returned handle in the GetProcAddress function to get the address of the DLL's myPuts function. After calling the DLL function, the program calls the FreeLibrary function to unload the DLL. Because the program uses run-time dynamic linking, it is not necessary t o link the module with an import library for the DLL. This example illustrates an important difference between run-time and lo ad-time dynamic linking. If the DLL is not available, the application us ing load-time dynamic linking must simply terminate. The run-time dynami c linking example, however, can respond to the error. // A simple program that uses LoadLibrary and // GetProcAddress to access myPuts from Myputs.dll.
#include <stdio.h> #include <windows.h> typedef int (*MYPROC)(LPTSTR); VOID main(VOID) { HINSTANCE hinstLib; MYPROC ProcAdd; BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; // Get a handle to the DLL module. hinstLib = LoadLibrary(TEXT("DllTest")); // If the handle is valid, try to get the function address. if (hinstLib != NULL) { ProcAdd = (MYPROC) GetProcAddress(hinstLib, TEXT("Proc")); // If the function address is valid, call the function. if (NULL != ProcAdd) { fRunTimeLinkSuccess = TRUE; (ProcAdd) (TEXT("Message via DLL function\n")); } // Free the DLL module. fFreeResult = FreeLibrary(hinstLib); } // If unable to call the DLL function, use an alternative. if (! fRunTimeLinkSuccess) printf("Message via alternative method\n"); }
对以上的几个函数作一些必要的说明Q? LoadLibraryQ加载指定的DLLQ加载方式是先在当前目录中查找,如果找不到再? 环境变量目录下查找; q是看MSDN上的说明 The LoadLibrary function maps the specified executable module into the a ddress space of the calling process. HMODULE LoadLibrary( LPCTSTR lpFileName ); Parameters lpFileName [in] Pointer to a null-terminated string that names the executable modul e (either a .dll or .exe file). The name specified is the file name of t he module and is not related to the name stored in the library module it self, as specified by the LIBRARY keyword in the module-definition (.def ) file.
GetProcAddressQ是取得已知的DLL中的函数Q返回指定函数的地址 MSDN上的说明 This function returns the address of the specified exported DLL function . FARPROC GetProcAddress( HMODULE hModule, LPCWSTR lpProcName ); Parameters hModule [in] Handle to the DLL module that contains the function. The LoadLibrary or GetModuleHandle function returns this handle. lpProcName [out] Pointer to a null-terminated string containing the function name, or specifies the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word; the high-order word must be zero. The lpProcName parameter must be in Unicode. Remark: The GetProcAddress function is used to retrieve addresses of exported fu nctions in DLLs. The spelling and case of the function name pointed to by lpProcName must be identical to that in the EXPORTS statement of the source DLL's modul e-definition (.def) file. The exported names of Win32 APIs might differ from the names you use whe n calling these functions in your code. This difference is hidden by mac ros used in the SDK header files.