Page 1 of 1

SetErrorMode not working

PostPosted: Sun Mar 17, 2019 3:22 am
by mark_c
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()) ); }
}

Re: SetErrorMode not working

PostPosted: Sun Mar 17, 2019 1:15 pm
by mark_c
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

Re: SetErrorMode not working

PostPosted: Mon Mar 18, 2019 11:19 am
by mark_c
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();
      
      ......
}

Re: SetErrorMode not working

PostPosted: Fri Mar 22, 2019 11:06 am
by mark_c
after numerous tests I discovered that by exploiting the events, not always clearly explained, it is possible to control this blessed socket

Re: SetErrorMode not working

PostPosted: Wed May 08, 2019 6:07 am
by lbrown7278
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?

Re: SetErrorMode not working

PostPosted: Wed May 08, 2019 10:13 am
by 2ffat
lbrown7278,

Is this error related to C++Builder? If so, please describe what you were doing and show us the code that caused this error.

If not, then this is the wrong forum.

Re: SetErrorMode not working

PostPosted: Wed May 08, 2019 11:18 am
by rlebeau
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.

Re: SetErrorMode not working

PostPosted: Wed May 08, 2019 11:29 am
by rlebeau
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.