Auto app update using Internet download

Hi there!!

I need help, I'm quite new with Delphi, so I need ALL the help I can get.

I've got an app, sitting on a user's PC 1000km away from, the user is not bright at all!!
I am tired of having to explain to her everytime how to update the manually, this is painful process, because I have to explain to her: Double-click on 'My Computer', double-click 'C:'.

So you get the picture. I need to automate this process by automatically downloading the updated version from a web location, then updating the app without any prompts whatsover.

Please, any help will be gladly appreciated.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maybe not the smartest answer, but I would write another app that checks and downloads updates, and when the user isn't using the program, applies updates. I would put updates on FTP server also.
rynosterAuthor Commented:
Using another app is the way I'm planning to do it, I just need the code to do it...

Don't have an ftp server.

                    function DownloadURL(const aUrl: string): Boolean;
                      hSession: HINTERNET;
                      hService: HINTERNET;
                      lpBuffer: array[0..1024 + 1] of Char;
                      dwBytesRead: DWORD;
                      Result := False;
                      // hSession := InternetOpen( 'MyApp', INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
                      hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);

                        if Assigned(hSession) then
                          hService := InternetOpenUrl(hSession, PChar(aUrl), nil, 0, 0, 0);
                          if Assigned(hService) then
                              while True do
                                dwBytesRead := 1024;
                                InternetReadFile(hService, @lpBuffer, 1024, dwBytesRead);
                                if dwBytesRead = 0 then break;
                                lpBuffer[dwBytesRead] := #0;
                              Result := True;

not tested but i assume it works.....

just upload your program as an exe, if you want zip then there's ways to solve that as well...
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

rynosterAuthor Commented:
Ran the code, doesn't work.

I stepped thru it, but when it gets here (the if)

hService := InternetOpenUrl(hSession, PChar(aUrl), nil, 0, 0, 0);
if Assigned(hService) then (not true),

so the rest of the code does not run...

Try this (requires Windows 95 and IE 3.0 as a minimum):

uses URLMon;

procedure TForm1.Button1Click(Sender: TObject);
  if URLDownloadToFile(nil, '', 'c:\NewApp.exe', 0, nil) <> 0 then
    MessageBox(Handle, 'Cannott download the file.', PChar(Application.Title), MB_ICONERROR or MB_OK);

Regards, Geo

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
A while back I wrote a little demo for a guy that wanted his delphi
program to update live from a new version on his website.

It works well and is really simple... the user does not need to know
how to do anything except be on the internet and click a button.

The exe just changes into the new version... the user does not
have to save a file or restart the program or anything...

here is the old demo... please right-click the link and choose
'save target as' because geocities does not allow direct external
links to files.

It includes a compiled exe so you can quickly test it by just adding exemod
to any delphi app and recompiling it... then placing it onto your website..easy :-)

here is the readme from this demo
This demo uses the freeware Synapse lib internet code
to get the new exe from your can use any code
you wish instead... just so long as it can get a file and place
it into a stream.

Just place any delphi exe that has been compiled with the
ExeMod.pas unit included onto your website and then type the
url into the edit box on the form... when you click the
[update exe from url below] button the program will grab
the new exe from your site and place it into a memstream
and swap the new version for the old version... this may take
a few moments so be patient... the button will stay depressed
while the update is in progress..

I included a button to update the exe from a local file... this is
for testing purposes and works identically to the url method except
the new exe is on your local hd.  Just select the exe you wish to swap
in in place of the original and it will exchange and come up running
very quickly :-)

Appologies for the crudity of this demo... I was in a hurry.
also I did not get to test the url method as I have no web space
where I can place an exe to test with... Geocities does not allow
for direct lnking to files :-(  but it should work OK...
rynosterAuthor Commented:
Hi Gwena

First off, thanx ALOT for the cool demo. This works great, the only problem is that the exe I need to update is a VB app. So what I need is something that can just download from the net(without any prompts). Then overwrite the EXE file currently in use.

This will be a seperate app, and one of the conditions will be that the VB app must be closed before this one can run.

Hi :-)

Ok... I'm clueless about VB so I can't help out with your problem...good luck!
rynosterAuthor Commented:
I got the following code from scalabium, but it only download an htm and puts the source in the memo box...

  Memo1.Lines.Text := DownloadFile('')

uses WinInet;

function DownloadFile(const Url: string): string;
  NetHandle: HINTERNET;
  UrlHandle: HINTERNET;
  Buffer: array[0..1024] of Char;
  BytesRead: dWord;
  Result := '';
  NetHandle := InternetOpen('Delphi 5.x', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);

  if Assigned(NetHandle) then
    UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);

    if Assigned(UrlHandle) then
      { UrlHandle valid? Proceed with download }
      FillChar(Buffer, SizeOf(Buffer), 0);
        Result := Result + Buffer;
        FillChar(Buffer, SizeOf(Buffer), 0);
        InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
      until BytesRead = 0;
      { UrlHandle is not valid. Raise an exception. }
      raise Exception.CreateFmt('Cannot open URL %s', [Url]);

    { NetHandle is not valid. Raise an exception }
    raise Exception.Create('Unable to initialize Wininet');
rynoster, have you tried my proposal?
rynosterAuthor Commented:
Oh yes, I forgot.

It's looking for the function/procedure 'URLDownloadFile', it's not an internal Delphi function.
It's WinAPI function defined in URLMon unit. The same applies for InternetOpen, InternetOpenUrl, InternetReadFile and InternetCloseHandle functions defined in WinInet unit. They are not internal Delphi functions also.
rynosterAuthor Commented:
Thanks to geobul.

I forgot to put in the 'uses URLMon' ...

Thanks to all the other guys who helped.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.