Windows的API函数为我们提供了一个强大的编程函数库。函数类别也基本涵盖了计算机应用的方方面
面,网络函数、绘图函数、进程与线程函数等等。下面简单的介绍三个API函数的用法。
SearchTreeForFile函数、IsValidURL函数和URLDownloadToFile函数。在应用三个函数的同时,顺便回顾
一下动态连接库DLL文件的创建和调用。呵呵...偶这个菜鸟只有这样时常温习才能有长进^-^
开始,首先创建一个dll工程:File->New->DLL Wizard,在DLL Wizard对话框中选择C++和Use VCX。
保存一下工程,命名为netdll。然后可以添加导出函数和导出类。
初始dll工程的内容如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
我们在这个netdll中封装三个函数,分别调用上面的三个API函数。
整个工程完成后如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <windows.h>
#include "Urlmon.h"
#include "imagehlp.h"
#include "malloc.h"
#include "windows.h"
#include "SysUtils.hpp"
#pragma hdrstop
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------
#pragma argsused
extern "C" __declspec(dllexport) int __stdcall getlocalfile(AnsiString);
extern "C" __declspec(dllexport) int __stdcall checkfilepath(AnsiString);
extern "C" __declspec(dllexport) int __stdcall getnetfile(AnsiString);
extern "C" __declspec(dllexport) int __stdcall downloadfile(AnsiString,AnsiString);
//-----------------------------------------------------------------------------
__declspec(dllexport) int _stdcall getlocalfile(AnsiString localpath){
AnsiString filename=ExtractFileName(localpath);
AnsiString path=ExtractFilePath(localpath);
char *p=(char *)malloc(150);
HRESULT hRet=SearchTreeForFile(path.c_str(),filename.c_str(),p);
if(hRet==S_OK) return 1;
else return 0;
}
//----------------------------------------------------------------------------
__declspec(dllexport) int _stdcall checkfilepath(AnsiString netpath){
wchar_t *dest=(wchar_t*)malloc(150);
HRESULT hRet=IsValidURL(NULL,netpath.WideChar(dest,150),0);
if(hRet==S_OK) return 1;
else return 0;
}
//-----------------------------------------------------------------------------
__declspec(dllexport) int _stdcall downloadfile(AnsiString netpath,AnsiString localpath){
LPUNKNOWN pCaller;
pCaller = NULL;
DWORD dwResv = NULL;
LPBINDSTATUSCALLBACK lpfnCB= NULL;
HRESULT hRet=URLDownloadToFile(pCaller,netpath.c_str(),localpath.c_str
(),dwResv,lpfnCB);
if(hRet==S_OK) return 1;
else return 0;
}
//------------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
红色部分是我们添加的,除了自己添加的部分外其他部分都未作修改。按F9,这样一个简单的动态连
接库就这样生成了。而且,我们可以看到,三个API函数的调用都非常简单。
第一个封装的函数getlocalfile(AnsiString localpath)调用了SearchTreeForFile函数,其功能是查
找一个文件的绝对路径;第二个封装的函数checkfilepath(AnsiString netpath)调用了IsValidURL函数
,其功能是判断一个URL路径是否有效;第三个封装的函数downloadfile(AnsiString
netpath,AnsiString localpath)调用了URLDownloadToFile函数,其功能是从网络上下载一个文件到本地
。三个API函数的具体参数和用法可以到msdn上查询。
extern "C" __declspec(dllexport) int __stdcall getlocalfile(AnsiString);
extern "C" __declspec(dllexport) int __stdcall checkfilepath(AnsiString);
extern "C" __declspec(dllexport) int __stdcall downloadfile(AnsiString,AnsiString);
这几句是导出函数的定义,也就是外部可以从这个DLL中调用的函数。
下面每个函数的实现也都是以__declspec(dllexport) (返回值类型) _stdcall形式开始。
用BCB创建的DLL文件我们也可以用其他编译环境调用。
最后就是别忘了些一个TXT文件,注明DLL文件中可以调用的函数的名称、参数以及函数的返回值,这
样方便别人调用已生成的DLL。
下面,我们就可以用VC调用这个netdll.dll了^-^