Author Topic: Inspired by ERRORMESSAGE$: Human readable message from GetLastError (Kernel32)  (Read 4063 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
This is something I know practically no one will use because, frankly, no one uses WinAPI around here. It probably won't end up in my API collection as its own file for this reason but I think it is neat enough for its own post, at least. I was inspired by the new ERRORMESSAGE$ keyword and how it displays the readable message for an error code in QB64. I decided to do some digging and found WinAPI has its own version of this with FormatMessage from Kernel32. You can also see I'm using a function from C++, written by Galleon, which I obtained from the MySQL Wiki page. Excellent little function. Anyways, this takes the error code from GetLastError and translates it into the actual message you would find on the MSDN. I've also made a special version of the below code (for myself and any other interested party). You can look at the second screenshot to see it.

Code: C++: [Select]
  1.   void *offset_to_offset(void* offset){
  2.    return offset;
  3.    }

Code: QB64: [Select]
  1.  
  2.  
  3. Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H00000100
  4. Const FORMAT_MESSAGE_FROM_SYSTEM = &H00001000
  5. Const FORMAT_MESSAGE_IGNORE_INSERTS = &H00000200
  6.  
  7. Const LANG_NEUTRAL = &H00
  8. Const SUBLANG_DEFAULT = &H01
  9.  
  10.     Sub SetLastError (ByVal dwErrCode As Long)
  11.     Function GetLastError& ()
  12.     Function FormatMessage& Alias FormatMessageA (ByVal dwFlags As Long, Byval lpSource As Long, Byval dwMessageId As Long, Byval dwLanguageId As Long, Byval lpBuffer As _Offset, Byval nSize As Long, Byval Arguments As _Offset)
  13.  
  14. Declare CustomType Library ".\offsettostring"
  15.     Function offset_to_string$ Alias offset_to_offset (ByVal offset As _Offset)
  16.  
  17.     Function MAKELANGID& (ByVal p As Long, Byval s As Long)
  18.  
  19.  
  20. For i = 50 To 60
  21.     SetLastError i
  22.  
  23.     Dim As _Offset lpMsgBuf
  24.     Dim As Long dw: dw = GetLastError
  25.     Dim As Long msg
  26.  
  27. msg = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER Or _
  28.                     FORMAT_MESSAGE_FROM_SYSTEM Or _
  29.                     FORMAT_MESSAGE_IGNORE_INSERTS, _
  30.                    0, _
  31.                     dw, _
  32.                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _
  33.                     _Offset(lpMsgBuf), _
  34.                    0, 0)
  35.  
  36.  
  37.     Print offset_to_string(lpMsgBuf)
  38.  

 
Screenshot 2021-02-07 001104.png

 
unknown.png
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
@SpriggsySpriggs

Working on code with simple APIs to get used to how it works. Maybe some file READ / WRITE APIS to start with.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
@SpriggsySpriggs

Working on code with simple APIs to get used to how it works. Maybe some file READ / WRITE APIS to start with.

I'm pretty sure you mentioned doing read and write with API in the past and I advised against it. I wouldn't use the API to read from or write to a file. Just use the built-in functions UNLESS you are using an API that requires a file handle.
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Just an example to learn API . What do you suggest for a simple API to get started. Maybe something from kernel32

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Just an example to learn API . What do you suggest for a simple API to get started. Maybe something from kernel32

Well, it depends. What are you trying to accomplish?
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
I'm not sure. That is the same question I came across when I was learning the DOS interrupts.  Eventually that is all I coded in and I used QB45 mainly as an IDE with A86 assembler.

Of course assembler is rarely used now.  so for me to learn APIs is to upgrade to better features of 32 / 64 bit programming..

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Alright. Yeah, just read the MSDN pages here:

https://docs.microsoft.com/en-us/windows/win32/api/

Find something that looks easy and go from there.
Shuwatch!