API函数的简单应用(一)

    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了^-^ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值