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

API函数的简单应用(一)相关推荐

  1. Windows API函数大全

    1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAddConnection3 创建同 ...

  2. Delphi 常用API 函数(好多都没见过)

    2019独角兽企业重金招聘Python工程师标准>>> AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在 ...

  3. 用Debug函数实现API函数的跟踪

    用Debug函数实现API函数的跟踪 如果我们能自己编写一个类似调试器的功能,这个调试器需要实现我们对于跟踪监视工具的要求,即自动记录输入输出参数,自动让目标进程继续运行.下面我们就来介绍在不知道函数 ...

  4. C#通过WMI的wind32 的API函数实现msinfo32的本地和远程计算机的系统摘要信息查看功能...

    最近做一个项目碰到要实现查看本地和远程计算机的摘要信息,采用命令行msinfo32可以很快查看到,如下图: 需要在用C#来实现类似信息查看.尤其远程计算机的..因此通过MSDN查询到.win32的AP ...

  5. [置顶]       【Visual C++】游戏开发笔记之一——API函数、DirectX的关键系统...

    本系列文章由zhmxy555(毛星云)编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7318264 作者:毛星云    邮箱: h ...

  6. Linux 编程中的API函数和系统调用的关系【转】

    转自:http://blog.chinaunix.net/uid-25968088-id-3426027.html 原文地址:Linux 编程中的API函数和系统调用的关系 作者:up哥小号 API: ...

  7. 在C#中调用windows API函数

    Api函数是构筑Windws应用程序的基石,每一种Windows应用程序开发工具,它提供的底层函数都间接或直接地调用了Windows API函数,同时为了实现功能扩展,一般也都提供了调用Windows ...

  8. Windows2000下Api函数的拦截分析

    Windows2000下Api函数的拦截分析 来源:网络 作者: 查看:[大字体 中字体 小字体] 编辑:napl 简介: Api拦截并不是一个新的技术,很多商业软件都采用这种技术.对windows的 ...

  9. 剪贴板所有api函数

    编写剪贴板相关程序是收集的相关API函数. (一)ChangeClipboardChain 将剪贴的连接从一个句柄转到下一个句柄. BOOL ChangeClipboardChain( HWND hW ...

最新文章

  1. 基于深度学习识别模型的缺陷检测
  2. android 隐藏输入法时自动关闭弹窗,Android监听输入法弹窗和关闭的实现方法
  3. hive udf开发超详细手把手教程
  4. C++Builder 2010深入TForm类之属性
  5. 仿京东商城html网页源码
  6. 基于java水果网店管理系统计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
  7. 计算机病毒教学评课,计算机病毒评课稿.pdf
  8. SG3525芯片简介
  9. Camera 图像处理原理分析- 色彩篇 二
  10. 非负数正则表达式 js jquery demo
  11. Android 屏幕适配扫盲、教程
  12. 全国计算机二级考试报名入口河南,计算机等级考试报名入口河南(英语四级报名官网入口)...
  13. 天理-数据结构(考研)
  14. 前端知识点整理(待续)
  15. Windows 10系统如何添加网络打印机?
  16. 实例简述Spring AOP之对AspectJ语法的支持
  17. JAVA C~K的班级
  18. Win10没有安全选项卡怎么办 安全选项卡在哪里
  19. Android CompressImage图片压缩工具类介绍
  20. echarts圆环象性图,实现从低向上渲染颜色

热门文章

  1. 互联网数字营销广告管理平台应用
  2. ISDB-T DVB-T DTV ATV
  3. 【Arduino使用旋转编码器模块】
  4. 纯CSS实现弹幕效果
  5. html页面漏斗图,漏斗图的详细解读
  6. CRNN中英文字符识别
  7. Android 6.0 JNI原理分析 和 Linux系统调用(syscall)原理
  8. 液晶面板里面有些什么配件_液晶模组LCM和液晶面板有什么区别
  9. 【 MATLAB 】poly 函数介绍
  10. genl_ops结构分析