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

Re: How to Implement GetLastError, for Internet functions

Post by HGAutomator »

Carlos,

I didn't notice your post until now, thanks.


Carlos Britos wrote: Wed Aug 12, 2020 9:25 pm
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
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 »

Damn. I wish I found this weeks ago.

Works nicely, equivalent to Ping. And it returns the error code, like a 11010 in one case.

I'm not able to get the Error description from the Error code programmatically, but Carlos provided this link, so it's not difficult:

https://github.com/asistex/win_err_sys_values

Also, here.

https://docs.microsoft.com/en-us/previo ... dfrom=MSDN


Very good. Got everything needed, thanks.

bpd2000 wrote: Fri Aug 14, 2020 4:58 am Hi
Refer this
https://www.hmgforum.com/viewtopic.php? ... ing#p51833
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: How to Implement GetLastError, for Internet functions

Post by srvet_claudio »

Hi,
It's not a good idea to call getlasterror () outside of C code, that's why it's never used in HMG.

Getlasterror () must always be called immediately after the function causing the error, otherwise there is a risk that the call to another function will overwrite the error value. Many functions change the error value returned from getlasterror both when they fail and when they succeed.
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

That's fine. In the last sample that bpd provided, GetLastError() is executed inside the C segment. And that's working fine.

At this point, I'm just saying that there doesn't seem to be a way of getting the error description, without searching an explicit table or array.

Auge wrote something up, but it always returns a blank, even when we send it a nonzero error code.

Anyway, it's no big deal. I can get this from the table of error codes & descriptions.


srvet_claudio wrote: Fri Aug 14, 2020 3:07 pm Hi,
It's not a good idea to call getlasterror () outside of C code, that's why it's never used in HMG.

Getlasterror () must always be called immediately after the function causing the error, otherwise there is a risk that the call to another function will overwrite the error value. Many functions change the error value returned from getlasterror both when they fail and when they succeed.
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: How to Implement GetLastError, for Internet functions

Post by srvet_claudio »

Hi,
see:

Code: Select all

#include "hmg.ch"

Function Main
LOCAL nError := 33

   msginfo( { nError, " --> ", HMG_GetSystemErrorMsg( nError ) } );

Return



#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"


#include <windows.h>
#include <tchar.h>
#include "hbapi.h"


HB_FUNC( HMG_GETSYSTEMERRORMSG ){ 
   DWORD dw = hb_parnl( 1 );
   LPVOID lpMsgBuf;
   TCHAR n = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
   if( n > 0 ){
      TCHAR * cBuffer = hb_xgrab( (n + 1)* sizeof( TCHAR ) );
      lstrcpy( cBuffer, (TCHAR*) lpMsgBuf );
      HMG_retc( cBuffer );
      hb_xfree( cBuffer );
   }
   else
      hb_retc( NULL );
   LocalFree(lpMsgBuf);
}

#pragma ENDDUMP
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

Thanks srvet, I'll give it a try this weekend.

Regards,


srvet_claudio wrote: Sat Aug 15, 2020 4:33 pm Hi,
see:

Code: Select all

#include "hmg.ch"

Function Main
LOCAL nError := 33

   msginfo( { nError, " --> ", HMG_GetSystemErrorMsg( nError ) } );

Return



#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"


#include <windows.h>
#include <tchar.h>
#include "hbapi.h"


HB_FUNC( HMG_GETSYSTEMERRORMSG ){ 
   DWORD dw = hb_parnl( 1 );
   LPVOID lpMsgBuf;
   TCHAR n = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
   if( n > 0 ){
      TCHAR * cBuffer = hb_xgrab( (n + 1)* sizeof( TCHAR ) );
      lstrcpy( cBuffer, (TCHAR*) lpMsgBuf );
      HMG_retc( cBuffer );
      hb_xfree( cBuffer );
   }
   else
      hb_retc( NULL );
   LocalFree(lpMsgBuf);
}

#pragma ENDDUMP
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 »

Unfortunately, I'm not able to compile this. The first time, I was using the non-hmg minigui distribution, which is missing the HMG include files.

So I went ahead and installed the hmg.3.4.4 download, and set a path to c:\hmg.3.4.4\MINGW\bin, since the hbmk2.exe didn't recognize the existing path to c:\bc5\bin.

But compiling returns an error:

Code: Select all

call ..\..\HARBOUR\bin\hbmk2 -b OurPing.prg -oOurPing -ic:\hmg.3.4.4\INCLUDE

Harbour 3.2.0dev (r1703241902)
Copyright (c) 1999-2016, http://harbour-project.org/
Compiling 'OurPing.prg'...
Lines 157, Functions/Procedures 1
Generating C source output to 'C:\Users\UserName\AppData\Local\Temp\hbmk_3bj9zf.dir\OurPing.c'... Done.
In file included from OurPing.prg:55:0:
OurPing.prg: In function 'HB_FUN_HMG_GETSYSTEMERRORMSG':
OurPing.prg:144:17: warning: passing argument 1 of 'hb_osStrU16Decode' from incompatible pointer type [-Wincompatible-pointer-types]
c:/hmg.3.4.4/INCLUDE/HMG_UNICODE.h:77:56: note: in definition of macro 'HMG_WCHAR_TO_CHAR'
    #define HMG_WCHAR_TO_CHAR(c)      hb_osStrU16Decode(c)                       // return CHAR
                                                        ^
OurPing.prg:144:7: note: in expansion of macro 'HMG_retc'
In file included from OurPing.prg:49:0:
C:/hmg.3.4.4/HARBOUR/include/hbapi.h:1226:31: note: expected 'const HB_WCHAR * {aka const short unsigned int *}' but argument is of type 'TCHAR * {aka char *}'
 extern HB_EXPORT char *       hb_osStrU16Decode( const HB_WCHAR * pszNameW );
                               ^
C:/Users/UserName/AppData/Local/Temp/hbmk_3bj9zf.dir/OurPing.o:OurPing.c:(.data+0x78): undefined reference to `HB_FUN_GETSYSTEMERRORMSG'
collect2.exe: error: ld returned 1 exit status
hbmk2: Error: Running linker. 1
gcc.exe C:/Users/UserName/AppData/Local/Temp/hbmk_3bj9zf.dir/OurPing.o C:/Users/UserName/AppData/Local/Temp/hbmk_3bj9zf.dir/hbmk_8h94x3.o    -mconsole -Wl,--start-group -lhbextern -lhbdebug -lhbvm -lhb
rtl -lhblang -lhbcpage -lgtcgi -lgtpca -lgtstd -lgtwin -lgtwvt -lgtgui -lhbrdd -lhbuddall -lhbusrrdd -lrddntx -lrddcdx -lrddnsx -lrddfpt -lhbrdd -lhbhsx -lhbsix -lhbmacro -lhbcplr -lhbpp -lhbcommon
 -lhbmainstd -lkernel32 -luser32 -lgdi32 -ladvapi32 -lws2_32 -liphlpapi -lwinspool -lcomctl32 -lcomdlg32 -lshell32 -luuid -lole32 -loleaut32 -lmpr -lwinmm -lmapi32 -limm32 -lmsimg32 -lwininet -lhbp
cre -lhbzlib   -Wl,--end-group -oOurPing.exe  -LC:/hmg.3.4.4/HARBOUR/lib/win/mingw

hbmk2: Error: Referenced, missing, but unknown function(s): GETSYSTEMERRORMSG()

It's referring to this, at line 144:

Code: Select all

      HMG_retc( cBuffer );


It's possible that the multiple paths for c:\minigui, c:\bc5, c:\hb32, and c:\hmg.3.4.4 are screwing up the compiler's search for the approrpriate include file. But I wanted to get an opinion about it, before removing those paths with Rapid Environment Editor.


srvet_claudio wrote: Sat Aug 15, 2020 4:33 pm Hi,
see:

Code: Select all

#include "hmg.ch"

Function Main
LOCAL nError := 33

   msginfo( { nError, " --> ", HMG_GetSystemErrorMsg( nError ) } );

Return



#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"


#include <windows.h>
#include <tchar.h>
#include "hbapi.h"


HB_FUNC( HMG_GETSYSTEMERRORMSG ){ 
   DWORD dw = hb_parnl( 1 );
   LPVOID lpMsgBuf;
   TCHAR n = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
   if( n > 0 ){
      TCHAR * cBuffer = hb_xgrab( (n + 1)* sizeof( TCHAR ) );
      lstrcpy( cBuffer, (TCHAR*) lpMsgBuf );
      HMG_retc( cBuffer );
      hb_xfree( cBuffer );
   }
   else
      hb_retc( NULL );
   LocalFree(lpMsgBuf);
}

#pragma ENDDUMP
User avatar
srvet_claudio
Posts: 2193
Joined: Thu Feb 25, 2010 8:43 pm
Location: Uruguay
Contact:

Re: How to Implement GetLastError, for Internet functions

Post by srvet_claudio »

Hi,
If you don't use HMG official, replace the lines:
#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

for the line:
#define HMG_retc hb_retc
Best regards.
Dr. Claudio Soto
(from Uruguay)
http://srvet.blogspot.com
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 »

Sorry this is taking so long. I tried removing those lines, and replacing with the def, but received other errors.

So I unset any paths to c:\hb32 and c:\minigui, and am working with the C:\hmg.3.4.4 now. It's not registering the HMG_GETSYSTEMERRORMSG function:

Code: Select all

..\..\HARBOUR\bin\hbmk2 -b -comp=mingw -warn=no OurPing.prg -oOurPing -ic:\hmg.3.4.4\INCLUDE

Code: Select all

Harbour 3.2.0dev (r1703241902)
Copyright (c) 1999-2016, http://harbour-project.org/
Compiling 'OurPing.prg'...
Lines 157, Functions/Procedures 1
Generating C source output to 'C:\Users\UserName\AppData\Local\Temp\hbmk_kilk27.dir\OurPing.c'... Done.
C:/Users/UserName/AppData/Local/Temp/hbmk_kilk27.dir/OurPing.o:OurPing.c:(.data+0x78): undefined reference to `HB_FUN_GETSYSTEMERRORMSG'
collect2.exe: error: ld returned 1 exit status
hbmk2: Error: Running linker. 1
gcc.exe C:/Users/UserName/AppData/Local/Temp/hbmk_kilk27.dir/OurPing.o C:/Users/UserName/AppData/Local/Temp/hbmk_kilk27.dir/hbmk_yfb1s1.o    -mconsole -Wl,--start-group -lhbextern -lhbdebug -lhbvm -lhb
rtl -lhblang -lhbcpage -lgtcgi -lgtpca -lgtstd -lgtwin -lgtwvt -lgtgui -lhbrdd -lhbuddall -lhbusrrdd -lrddntx -lrddcdx -lrddnsx -lrddfpt -lhbrdd -lhbhsx -lhbsix -lhbmacro -lhbcplr -lhbpp -lhbcommon
 -lhbmainstd -lkernel32 -luser32 -lgdi32 -ladvapi32 -lws2_32 -liphlpapi -lwinspool -lcomctl32 -lcomdlg32 -lshell32 -luuid -lole32 -loleaut32 -lmpr -lwinmm -lmapi32 -limm32 -lmsimg32 -lwininet -lhbp
cre -lhbzlib   -Wl,--end-group -oOurPing.exe  -LC:/hmg.3.4.4/HARBOUR/lib/win/mingw

hbmk2: Error: Referenced, missing, but unknown function(s): GETSYSTEMERRORMSG()

srvet_claudio wrote: Thu Aug 20, 2020 3:59 pm Hi,
If you don't use HMG official, replace the lines:
#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

for the line:
#define HMG_retc hb_retc
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 »

Thanks everyone for their assistance. I'll close the thread, now. It's very simple to get the error descriptions from an array or table, so it's not necessary to refine this any further.

Thanks & have a good weekend,


HGAutomator wrote: Thu Aug 20, 2020 7:23 pm Sorry this is taking so long. I tried removing those lines, and replacing with the def, but received other errors.

So I unset any paths to c:\hb32 and c:\minigui, and am working with the C:\hmg.3.4.4 now. It's not registering the HMG_GETSYSTEMERRORMSG function:

Code: Select all

..\..\HARBOUR\bin\hbmk2 -b -comp=mingw -warn=no OurPing.prg -oOurPing -ic:\hmg.3.4.4\INCLUDE

Code: Select all

Harbour 3.2.0dev (r1703241902)
Copyright (c) 1999-2016, http://harbour-project.org/
Compiling 'OurPing.prg'...
Lines 157, Functions/Procedures 1
Generating C source output to 'C:\Users\UserName\AppData\Local\Temp\hbmk_kilk27.dir\OurPing.c'... Done.
C:/Users/UserName/AppData/Local/Temp/hbmk_kilk27.dir/OurPing.o:OurPing.c:(.data+0x78): undefined reference to `HB_FUN_GETSYSTEMERRORMSG'
collect2.exe: error: ld returned 1 exit status
hbmk2: Error: Running linker. 1
gcc.exe C:/Users/UserName/AppData/Local/Temp/hbmk_kilk27.dir/OurPing.o C:/Users/UserName/AppData/Local/Temp/hbmk_kilk27.dir/hbmk_yfb1s1.o    -mconsole -Wl,--start-group -lhbextern -lhbdebug -lhbvm -lhb
rtl -lhblang -lhbcpage -lgtcgi -lgtpca -lgtstd -lgtwin -lgtwvt -lgtgui -lhbrdd -lhbuddall -lhbusrrdd -lrddntx -lrddcdx -lrddnsx -lrddfpt -lhbrdd -lhbhsx -lhbsix -lhbmacro -lhbcplr -lhbpp -lhbcommon
 -lhbmainstd -lkernel32 -luser32 -lgdi32 -ladvapi32 -lws2_32 -liphlpapi -lwinspool -lcomctl32 -lcomdlg32 -lshell32 -luuid -lole32 -loleaut32 -lmpr -lwinmm -lmapi32 -limm32 -lmsimg32 -lwininet -lhbp
cre -lhbzlib   -Wl,--end-group -oOurPing.exe  -LC:/hmg.3.4.4/HARBOUR/lib/win/mingw

hbmk2: Error: Referenced, missing, but unknown function(s): GETSYSTEMERRORMSG()

srvet_claudio wrote: Thu Aug 20, 2020 3:59 pm Hi,
If you don't use HMG official, replace the lines:
#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

for the line:
#define HMG_retc hb_retc
Post Reply