编程技术、软件应用与系统模拟

(Programming, Applicaiton and Simulation)



本站目录

 

首页
ASP/Access/IIS
DELPHI/PASCAL
PASCAL高级编程
C语言编程实例
WORD
Excel
MATLAB
MINITAB讲座
Windows
DOS
SAS
生物系统模拟
土壤水分剖析器
其他



镜像站点

 

主站
北美镜象站
欧洲镜象站(1)
欧洲镜象站(2)

本站 Google

[搜索]  [站内导航]
座右铭:
只做有益人类的事
不做有害人类的事


An ASP COM component that can fetch a file from the internet and save on your server automatically


Zhanshan Dong
Two weeks ago I migrated my webserver from a machine with Windows 98 and PWS to a machine with Windows 2000 Professional and IIS 5.0. Somehow one COM component did not work anymore in IIS 5.0. But the same COM component works very well under PWS and Windows 98. I wrote that COM component two years ago by using Delphi. I searched the help file in delphi for attempt to sovle the problem, I failed to do that. I searched the web through Google. I saw the same problem somebody already asked in the web. But there is no answer to this question. I was a little disappointed about this. But I got to solve the problem and let my web site work properly. In one article, I found that one guy suggested another way to download files from the internet without using UrlMon.DLL. I followed his suggestion and started to use WinINet.DLL to create a sample program. At the very beginning, the program did not work either. I was confused. I tried very hard in the late night. Dubugging the program step by step. Finally, I sovled the problem and downloaded a file to my hard disk. I knew that I found the answer. The source code is pretty simple (listed below). But the idea is very important. The COM component written in DELPHI 6.0. IF you wnant to use the source code, you need create a new ActiveX Libray project, and add one Automation Class to your project and add a method to the class, then copy and paste the code to your function implementation part.
Here is the source code.
=======================
unit uMain;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, AspTlb, ReadUrl_TLB, StdVcl,sysutils,classes;

type
  TReadUrl = class(TASPMTSObject, IReadUrl)
  protected
    function DownloadFile(Source, Dest: OleVariant): OleVariant; safecall;
  end;

implementation

uses windows, ComServ, Wininet;

function TReadUrl.DownloadFile(Source, Dest: OleVariant): OleVariant;
var
  BSource, ADest : string;
  ASource : Pchar;
  hSession: HINTERNET;
  hService: HINTERNET;
  lpBuffer: array[0..1024 + 1] of Char;
  dwBytesRead: LongWord;
  myfile : file;
begin
  BSource := Source;
  ASource := pchar(BSource);
  ADest := Dest;
  assignfile(myFile,ADest);
  rewrite(myFile,1);
  Result := False;
  hSession := InternetOpen('ReadUrl', INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
  try
    if Assigned(hSession) then
    begin
      hService := InternetOpenUrl(hSession, ASource, nil, 0, INTERNET_FLAG_RELOAD, 0);
      if Assigned(hService) then
        try
          while True do
          begin
            dwBytesRead := 1024;
            InternetReadFile(hService, @lpBuffer, 1024, dwBytesRead);
            if dwBytesRead = 0 then break;
            lpBuffer[dwBytesRead] := #0;
            blockwrite(myfile,lpBuffer,dwBytesRead);
          end;
          Result := True;
        finally
          InternetCloseHandle(hService);
        end;
    end;
  finally
    InternetCloseHandle(hSession);
  end;
  closefile(myfile);
end;

initialization
  TAutoObjectFactory.Create(ComServer, TReadUrl, CLASS_ReadUrl,
    ciMultiInstance, tmApartment);
end.
====================================
The source code file include all you need, even the compiled DLL file.
     Download source code

© 1998-, 董占山, 版权所有。
转载文章请注明出处(www.sunfinedata.com/articles)。