API函数的简单应用(转)

Windows的API函数为我们提供了一个强大的编程函数库。函数类别也基本涵盖了计算机应用的方方面面,网络函数、绘图函数、进程与线程函数等等。下面简单的介绍三个API函数的用法。

SearchTreeForFile函数、IsValidURL函数和URLDownloadToFile函数。在应用三个函数的同时,顺便回顾一下动态连接库DLL文件的创建和调用。

   开始,首先创建一个dll工程:File->New->DLL Wizard,在DLL Wizard对话框中选择C++和Use VCX。

保存一下工程,命名为netdll。然后可以添加导出函数和导出类。
   初始dll工程的内容如下:
//---------------------------------------------------------------------------

#include
#include
#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
#include
#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了。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14884692/viewspace-406830/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14884692/viewspace-406830/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值