🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4795

Re: closing form

#4795
From: Remy Lebeau (TeamB)
Date: Wed, 23 Nov 2005 03:26
113 lines
4365 bytes
"Alfred" <alf@no.spam.not.valid.email> wrote in message
news:4383d048$1@newsgroups.borland.com...

> void __fastcall TForm1::FormCreate(TObject *Sender)

Do not use the OnCreate event in C++.  It is a Delphi idiom that can produce
illegal behavior in C++ as it can be executed before the constructor.  Use
the actual constructor instead.

>           Form1->Close();

You cannot use Close() while the form is still creating itself.  If you want
the application is end, then use the Application->Terminate() method
instead.

> bool __fastcall TForm1::ExecuteFile(String FileName)

You should always pass strings by const reference.

>         AnsiString Path = ExtractFilePath( ParamStr(0) ) + "\\" +
FileName;

ExtractFilePath() includes the trailing backslash, so you are effectly
formatting an invalid path by adding your own backslash in front of the
filename.

>         int rc = (int) ShellExecute( Application->Handle, "open",
Path.c_str(), NULL, NULL, SW_SHOWDEFAULT ) ;

WinExec() or CreateProcess() would be better to use for executable files.
ShellExecute() is designed primarily for documents and such.

With that said, try this code instead:


    __fastcall TForm1::TForm1(TComponent *Owner)
    {
        if( ExecuteFile("Downloader.exe") )
        {
            Application->ShowMainForm = false;
            Application->Terminate();
        }
    }

    bool __fastcall TForm1::ExecuteFile(const AnsiString &FileName)
    {
        AnsiString Path = ExtractFilePath(Application->ExeName) + FileName;

        STARTUPINFO si = {sizeof(STARTUPINFO), 0};
        PROCESS_INFORMATION pi = {0};

        if( CreateProcess(NULL, Path.c_str(), NULL, NULL, FALSE, 0, NULL,
NULL, &si, &pi) )
        {
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
            return true;
        }

        MemoNews->Lines->Add("Errore di autoaggiornamento numero : " +
AnsiString(GetLastError()));
        return false;
    }


A better approach would be to not even create the TForm at all unless you
are actually going to use it.  You can do that moving the code into the
WinMain() function for the project, ie:

    #include "Unit1.h"

    bool __fastcall ExecuteFile(const AnsiString &FileName)
    {
        AnsiString Path = ExtractFilePath(Application->ExeName) + FileName;

        STARTUPINFO si = {sizeof(STARTUPINFO), 0};
        PROCESS_INFORMATION pi = {0};

        if( CreateProcess(NULL, Path.c_str(), NULL, NULL, FALSE, 0, NULL,
NULL, &si, &pi) )
        {
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
            return true;
        }

        return false;
    }

    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
        if( !ExecuteFile("Downloader.exe") )
        {
            AnsiString s = "Errore di autoaggiornamento numero : " +
AnsiString(GetLastError());
            try
            {
                Application->Initialize();
                Application->CreateForm(__classid(TForm1), &Form1);
                Form1->MemoNews->Lines->Add(s);
                Application->Run();
            }
            catch(Exception &exception)
            {
                Application::ShowException(&exception);
            }
        }
        return 0;
    }


Gambit
Technical Details
Message-ID: <4383e16c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4383d048$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,9d517b38f0628b82
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4383d048$1@newsgroups.borland.com>
Subject: Re: closing form
Date: Tue, 22 Nov 2005 19:26:45 -0800
Lines: 115
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4383e16c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1132716396 68.69.17.41 (22 Nov 2005 19:26:36 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail