Click here to Skip to main content
13,299,298 members (62,904 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 16 Aug 2005

Modifying Wininet,dll to implement Pause & Resume at FTP Protocol leve

, 16 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Wininet API can not support the function of resume but we can modify wininet.dll to support it.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->


I have read the article “Pause & Resume at Protocol level. (You can implement it in any language)”  By Narendra Chandel . But his work does not work on many server. I sniffed the TCP packages and found the commands ,which Wininet API sent, does not match W<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:chmetcnv w:st="on" TCSC="0" NumberType="1" Negative="False" HasSpace="False" SourceValue="3" UnitName="C">3C standard. So I modified Wininet.dll and add a new function.

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> 



Programmatically starting download pausing and then restarting again

Let say for example you are downloading a file:

  1. Typically send a RETRIEVE (RETR) command for file to the FTP server to start download. Store data received from server to a temp file for example So any time download is aborted, size of temp file is the actual useful data transferred so far of
  2. Let say all of a sudden ISP connection is broken or FTP server is down or your FTP client crashed or you manually stopped the download.
  3. Now Next time when you want to restart download, read the size of temp file (i.e. useful data downloaded so far), and seek to the end of the temp file so further download will append the data from end.
  4. Now send a REST command to FTP server and pass the file size of temp file as parameter. (a typical syntax would be REST 250 if size of temp file was 250). FTP server will send a response stating something like "Restarting from byte 250..".
  5. Now you can send RETRIEVE (RETR) again for file and FTP server will start sending data from byte offset 251 onwards. 
  6. Now it is your responsibility to append the data coming from server to the incomplete file and you are all set.
  7. Once the transfer is complete rename the temp file to the actual file name on WINDOWS using C++ it should typically look like <FONT size=2>RenameFile(,; </FONT>Done.

Programmatically determining if a particular FTP server supports Pause & Resume

Three simple steps

  1. Once connected to FTP server try sending a REST command with 0 ( zero ) as argument.
  2. If commands fail or server return something like command is not supported. That means this server doesn't support Pause and Resume.
  3. If FTP server return a response stating something like "Restarting from byte 0" that means server supports Pause & Resume

Now if you just started writing code you may have question, how to send these commands the FTP  server. Answer is so many ways. Raw socket is one, WinInet is another. In my sample code I have utilized WinInet's API FtpCommand to send direct commands to FTP server.

How Wininet API work

LPCTSTR lpszFileName,
DWORD dwAccess,
DWORD dwFlags,
DWORD dwContext);

Three simple steps

1,Type to I PASV
2,SIZE filename
3, some nouse action

If we change SIZE filename command to REST pos ,it would resume from pos.

so I modify the function like this:

LPCTSTR lpszFileName,
DWORD dwAccess,
DWORD dwFlags,
LPCTSTR *plpszSeekPos );

the argument  plpszSeekPos  is used as follow:

CString sSeekPos;

Modify Wininet.dll

Debug into FTPOpenFile function ,F11 F11 ....

then you would find three function entry to format string:

761BB256   and         ebx,80000000h
761BB25C   mov         eax,761DE8F0h
761BB261   jne         761BB268
761BB263   mov         eax,761DE8E8h
761BB276   call        761BC0A2 
                          format("RETR %s");

761BC213   push        edi
761BC214   mov         edi,dword ptr [ebp+70h]
761BC217   push        eax
761BC218   push        761DE9ECh
761BC21D   push        edi
761BC21E   push        0
761BC225   call        761BC1C3
                format("TYPE %s");

761BC7F9   test        eax,eax  //没有用
761BC7FB   jne         761BC89D //没有用
761BC801   push        dword ptr [ebp+78h]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lpstrFileAtServer
761BC804   push        761DE93Ch
761BC809   push        dword ptr [ebp+58h]
761BC80C   call        761BC003
                format("SIZE %s",lpstrFileAtServer)

Replace string resource first. change SIZE %s to REST %s.

At address 761BC7F9:

761BC7F9  85 C0 0F 85 9C 00 00  吚.厹..
761BC800  00 FF 75 78

761BC7F9   test        eax,eax 
761BC7FB   jne         761BC89D
761BC801   push        dword ptr [ebp+78h]

Modify the 11 bytes to:

761BC7F9  90 8B C5 05 50 01 00  悑..P..
761BC800  00 50 90 90

      mov eax, ebp    ;
      add eax, 0x150  ;
      push eax ;

that's DONE!.


        char lpPos[64];//note: lpPos must in stack not in heap

        m_hOpenFile = ::FtpOpenFile(m_hOpenUrlHandle,sFileNameAtServer



You should put this modified dll in current directory.













This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
China China
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralSolved this 2 years ago Pin
sdfarmer18-Sep-05 6:50
membersdfarmer18-Sep-05 6:50 
GeneralCan't do the steps . . . Pin
kasparovooo1-Sep-05 9:52
memberkasparovooo1-Sep-05 9:52 
GeneralRe: Can't do the steps . . . Pin
bbinn7-Sep-05 22:15
memberbbinn7-Sep-05 22:15 
GeneralNot recommended Pin
norm.net17-Aug-05 0:10
membernorm.net17-Aug-05 0:10 
GeneralRe: Not recommended Pin
mwilliamson18-Aug-05 6:03
sussmwilliamson18-Aug-05 6:03 
GeneralRe: Not recommended Pin
norm.net21-Aug-05 21:43
membernorm.net21-Aug-05 21:43 
GeneralRe: Not recommended Pin
mwilliamson22-Aug-05 4:10
sussmwilliamson22-Aug-05 4:10 
GeneralGood work! Pin
bbinn16-Aug-05 16:51
memberbbinn16-Aug-05 16:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 16 Aug 2005
Article Copyright 2005 by bbinn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid