How to Implement GetLastError, for Internet functions

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

HGAutomator
Posts: 188
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

How to Implement GetLastError, for Internet functions

Post by HGAutomator »

Hi,

In c:\minigui\SAMPLES\Advanced\INET_CHECKER\InetState.prg there's a kind of Ping sample that returns true or false, depending on the return value of the the Winapi function InternetCheckConnection.

If possible, I'd like to see more detail when the connection fails.


At https://docs.microsoft.com/en-us/window ... onnectiona

, it mentions that GetLastError can return the error code.

Do we have an implementation of GetLastError, in the HMG distribution?
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

Re: How to Implement GetLastError, for Internet functions

Post by Carlos Britos »

HGAutomator wrote: Wed Aug 12, 2020 8:00 pm Do we have an implementation of GetLastError, in the HMG distribution?
Hi,
the function GetLastError() is defined in winapimisc.c
Run the after InternetCheckConnection and you'll get the error code

In https://github.com/asistex/win_err_sys_values you can get a dbf file with the 15841 sys errors.

Carlos
Regards/Saludos, Carlos (bcd12a)
HGAutomator
Posts: 188
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to Implement GetLastError, for Internet functions

Post by HGAutomator »

You mean, just check the value of the function GetLastError()? Or the method GetLastError, from an Internet connection object?
User avatar
AUGE_OHR
Posts: 2064
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: How to Implement GetLastError, for Internet functions

Post by AUGE_OHR »

hi,

GetLastError() most are used with FormatMessage()

Code: Select all

HB_FUNC( SHOWERROR )
{
   LPVOID lpMsgBuf;
   DWORD dwError  = GetLastError();

   FormatMessage( 
      FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      dwError,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL 
   );
   
   MessageBox(NULL, (LPCSTR)lpMsgBuf, "Shutdown", MB_OK | MB_ICONEXCLAMATION);
   // Free the buffer.
   LocalFree( lpMsgBuf );
}
have fun
Jimmy
HGAutomator
Posts: 188
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to Implement GetLastError, for Internet functions

Post by HGAutomator »

Ok, I'll just give it a try, empirically. Thanks, Auge.
AUGE_OHR wrote: Thu Aug 13, 2020 12:26 am hi,

GetLastError() most are used with FormatMessage()

Code: Select all

HB_FUNC( SHOWERROR )
{
   LPVOID lpMsgBuf;
   DWORD dwError  = GetLastError();

   FormatMessage( 
      FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
      NULL,
      dwError,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL 
   );
   
   MessageBox(NULL, (LPCSTR)lpMsgBuf, "Shutdown", MB_OK | MB_ICONEXCLAMATION);
   // Free the buffer.
   LocalFree( lpMsgBuf );
}
HGAutomator
Posts: 188
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to Implement GetLastError, for Internet functions

Post by HGAutomator »

Yep. GetLastError works, i.e. it returns the Error code from

https://support.microsoft.com/en-us/hel ... ough-12156


However, ShowError always does the same thing - a

ShutDown !

popup box with nothing else displayed. And the string that it returns seems to be empty.


Also, more importantly, the InternetCheckConnection API function doesn't seem to have the versatility of Ping.

So for example, we can send Ping a Server Name parameter. It will return both the IP, and the url.

ping THISSERVER

returns a sequence that starts with

Pinging THISSERVER.xx.xx.gov [99.999.99.99] with 32 bytes of data


But InternetCheckConnection doesn't understand a server name. And even if you give in the full url THISSERVER.xx.xx.gov, it doesn't always return a successful connection, as Ping does. Same thing, if you send it the IP address ; Ping returns a successful connection, while InternetCheckConnection doesn't always.
User avatar
AUGE_OHR
Posts: 2064
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: How to Implement GetLastError, for Internet functions

Post by AUGE_OHR »

hi,

have you use GetLastError() before call ShowError() :?:

you can pass GetLastError() this Way

Code: Select all

HB_FUNC( xGETSYSTEMERRORMESSAGE )
{
  DWORD  dwError = hb_parni(1);
  LPVOID lpMsgBuf;

  FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | 72,
    NULL,
    dwError,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    (LPTSTR) &lpMsgBuf,
    0,
    NULL);

  hb_retc(lpMsgBuf);

  //HeapFree(GetProcessHeap(), 0, lpMsgBuf);
  LocalFree(lpMsgBuf);
}
have fun
Jimmy
HGAutomator
Posts: 188
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to Implement GetLastError, for Internet functions

Post by HGAutomator »

Tried it. The error message is still empty, even though the error code is nonzero.

Code: Select all

IF .NOT. IsInternet_b
	Error_n := GetLastError()
	Error_s := GETSYSTEMERRORMESSAGE( Error_n )
	MsgBox( Error_s )
ENDIF

And, in any case, it won't accept the same types of server names that Ping does.

I already have something working as far as testing Web Connectivity, with Xailer's TInternet class. I was just hoping to do it via Minigui.

What I might end up doing, is just do a call to Ping.exe, for only hosts or database servers that we wouldn't check in a browser.



AUGE_OHR wrote: Thu Aug 13, 2020 4:47 pm hi,

have you use GetLastError() before call ShowError() :?:

you can pass GetLastError() this Way

Code: Select all

HB_FUNC( xGETSYSTEMERRORMESSAGE )
{
  DWORD  dwError = hb_parni(1);
  LPVOID lpMsgBuf;

  FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | 72,
    NULL,
    dwError,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    (LPTSTR) &lpMsgBuf,
    0,
    NULL);

  hb_retc(lpMsgBuf);

  //HeapFree(GetProcessHeap(), 0, lpMsgBuf);
  LocalFree(lpMsgBuf);
}
HGAutomator
Posts: 188
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to Implement GetLastError, for Internet functions

Post by HGAutomator »

It looks like Itamar came up with something several years ago. It seems to work equivalently to Ping:


https://groups.google.com/forum/#!searc ... wkE4WNcYYJ

Code: Select all

FUNCTION MAIN() 

    ? HB_PING( "62.149.130.254" ) 
    ? HB_PING( "www.emagsoftware.it" ) 
    ? HB_PING( "www.emagsoftware.xx" ) 

    INKEY( 0 ) 

    RETURN NIL 


#pragma BEGINDUMP 

#include <hbapi.h> 
#include <winsock2.h> 
#include <iphlpapi.h> 
#include <icmpapi.h> 

int hb_Ping( const char * cp ) 
{ 
    HANDLE hIcmpFile; 
    unsigned long ipaddr; 
    DWORD dwRetVal; 
    char SendData[32] = "Data Buffer"; 
    LPVOID ReplyBuffer; 
    DWORD ReplySize; 

    if( isalpha( cp[0] ) )      //host address is a name 
    { 
       WSADATA wsaData; 
       int     iResult; 

       iResult = WSAStartup( MAKEWORD(2, 2), &wsaData ); 

       if( iResult == 0 ) 
       { 
          struct hostent *remoteHost = gethostbyname( cp ); 

          if( remoteHost != NULL ) 
             ipaddr = *(unsigned long *) remoteHost->h_addr_list[0]; 

          WSACleanup(); 
       } 
    } 
    else 
       ipaddr = inet_addr( cp ); 

    if (ipaddr == INADDR_NONE) 
        return 1; 
    
    hIcmpFile = IcmpCreateFile(); 
    if (hIcmpFile == INVALID_HANDLE_VALUE) 
        return 2; 

    ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData); 
    ReplyBuffer = (VOID*) malloc(ReplySize); 
    if (ReplyBuffer == NULL) 
    { 
        IcmpCloseHandle(hIcmpFile); 
        return 3; 
    } 
    
    
    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), 
        NULL, ReplyBuffer, ReplySize, 1000); 

    free(ReplyBuffer); 

    IcmpCloseHandle(hIcmpFile); 

    if (dwRetVal == 0) 
        return 4; 
    
    return 0; 

} 

HB_FUNC( HB_PING ) 
{ 
   hb_retni( hb_Ping( hb_parc( 1 ) ) ); 
} 

#pragma ENDDUMP 
User avatar
bpd2000
Posts: 1207
Joined: Sat Sep 10, 2011 4:07 am
Location: India

Re: How to Implement GetLastError, for Internet functions

Post by bpd2000 »

BPD
Convert Dream into Reality through HMG
Post Reply