SetErrorMode not working

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

SetErrorMode not working

Postby mark_c » Sun Mar 17, 2019 3:22 am

Hello,
I'm trying to write a program that tests a LAN and I can't continue the test because Windows system messages always appear.
I tried setting SetErrorMode (SEM_NOGPFAULTERRORBOX); but it does not work.

please note: the address 0.0.0.0 even if it is not valid, I set it on purpose to run the test and continue in case of error.
Code: Select all
void __fastcall TForm1::Button4Click(TObject *Sender)
{
        SetErrorMode(SEM_NOGPFAULTERRORBOX);

        ClientSocket1->Host = "0.0.0.0";
        ClientSocket1->Port = 80;

        if(ClientSocket1->Active == true) ClientSocket1->Close();

        Memo1->Lines->Add("Test 1");

        try {

           ClientSocket1->Open();
        } catch(const ESocketError &error) { ShowMessage("Socket error: " + IntToStr(WSAGetLastError()) ); }
}
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 170
Joined: Thu Jun 21, 2012 1:13 am

Re: SetErrorMode not working

Postby mark_c » Sun Mar 17, 2019 1:15 pm

solved by rewriting this event:

Code: Select all
void __fastcall TForm1 :: ClientSocket1Error (TObject * Sender,
       TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode)
{
         ErrorCode = 0;
}


this event was already set but did not want to work. I deleted and rewrote it and now it works, as if it were invisible to the compiler before the deletion.

note: for this case it is useless to use SetErrorMode
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 170
Joined: Thu Jun 21, 2012 1:13 am

Re: SetErrorMode not working

Postby mark_c » Mon Mar 18, 2019 11:19 am

how to synchronize connection, download and disconnection?
If I run this program, the second connection does not wait for the entire download of the first to be terminated but to interrupt it.
Code: Select all
void __fastcall TForm1::Button4Click(TObject *Sender)
{
        ClientSocket1->Host = "1.0.0.1";
        ClientSocket1->Port = 80;
        ClientSocket1->Open();
      
      // wait, how?
      
        ClientSocket1->Host = "1.0.0.2";
        ClientSocket1->Port = 80;
        ClientSocket1->Open();
      
      ......
}


note: the two calls are correctly executed only if I add ShowMessage ("wait");

Code: Select all
void __fastcall TForm1::Button4Click(TObject *Sender)
{
        ClientSocket1->Host = "1.0.0.1";
        ClientSocket1->Port = 80;
        ClientSocket1->Open();
      
   ShowMessage ("wait");
      
        ClientSocket1->Host = "1.0.0.2";
        ClientSocket1->Port = 80;
        ClientSocket1->Open();
      
      ......
}
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 170
Joined: Thu Jun 21, 2012 1:13 am

Re: SetErrorMode not working

Postby mark_c » Fri Mar 22, 2019 11:06 am

after numerous tests I discovered that by exploiting the events, not always clearly explained, it is possible to control this blessed socket
mark_c
BCBJ Guru
BCBJ Guru
 
Posts: 170
Joined: Thu Jun 21, 2012 1:13 am

Re: SetErrorMode not working

Postby rlebeau » Wed May 08, 2019 11:18 am

mark_c wrote:I'm trying to write a program that tests a LAN and I can't continue the test because Windows system messages always appear.
I tried setting SetErrorMode (SEM_NOGPFAULTERRORBOX); but it does not work.


Of course not, because there is no system dialog being displayed in this situation, just a VCL dialog for a thrown exception. SetErrorMode() has no effect on that.

Change ESocketError to Exception in your catch block.

If you are still seeing the dialog, the only remaining way that can happen is if you are running your code inside the debugger, which of course will always display a thrown exception (even if it is caught later) unless you tell it not to.

mark_c wrote:solved by rewriting this event:

Code: Select all
void __fastcall TForm1 :: ClientSocket1Error (TObject * Sender,
       TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode)
{
         ErrorCode = 0;
}


this event was already set but did not want to work. I deleted and rewrote it and now it works, as if it were invisible to the compiler before the deletion.


More likely, it simply wasn't assigned to the TClientSocket::OnError event, like you thought it was.

mark_c wrote:how to synchronize connection, download and disconnection?
If I run this program, the second connection does not wait for the entire download of the first to be terminated but to interrupt it.


That is because TClientSocket runs in non-blocking mode by default, so you need to change it to blocking mode. And also because you are not actually implementing the HTTP protocol, otherwise you would know exactly when the 1st download is complete before then invoking the second download.

But really, why are you handling this manually at all, instead of using a pre-existing HTTP component/library instead? HTTP is not trivial to implement from scratch. There are many HTTP implementations available for C++Builder. Indy (which comes pre-installed) has a TIdHTTP client component. Other popular libraries like ICS and Synapse have their own HTTP clients, too. Or, just use Microsoft's own WinInet or WinHTTP APIs that are built into Windows itself.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1616
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: SetErrorMode not working

Postby rlebeau » Wed May 08, 2019 11:29 am

lbrown7278 wrote:This is the same problem that I have also faced while setting the Epson printer with the Windows 7 system, and it was showing epson error code 0xf4 while I connect the printer on my system. Can anyone solve the error?


That has nothing to do with this discussion, or even with sockets in general. That is a printer error, not a socket error.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1616
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: No registered users and 15 guests

cron