Results 1 to 5 of 5

Thread: GetLastError feature

  1. #1

    Question GetLastError feature

    Following on from https://www.thinbasic.com/community/...ile-operations,

    many windows functions return an error code that should be accessible via the GetLastError function. That is the case by example of CopyFileExW function or the DeviceIoControl function.

    I've found nothing about this in the samples nor the help file: how can a thinBasic script get this error code and relevant messages ?
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,083
    Blog Entries
    5
    Rep Power
    725
    Hi DirectuX,

    I think as of now, you need to use:
    DECLARE FUNCTION GetLastError IMPORT "KERNEL32.DLL" ALIAS "GetLastError" () AS DWORD
    
    As this is super useful function, I think Eros could consider adding it to its WIN_ wrappers as WIN_GetLastError?

    It would be also nice to have all the errors from WinError.h, I can help with that if needed.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Thanks Petr !

    + I agree for the wrapper
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    Problem with GetLastError API is that it return ... last error of a Windows API call.

    But inside an interpreted language like thinBasic ... between you call to a Window Api and GetLastError call there could be other thousands of Windows Api calling by the interpreter.
    So GetLastError is, for the most cases, useless in thinBasic. It depends how you write code, when GetLastError is called compared to the API calling.
    thinBasic has to do a lot of things just after a Window API calling so when thinBasic parser has reached GetLastError ... it may be late.

    Instead GetLastError is useful in a compiled language where the code call exactly what the programmer write (well, most of the time)
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #5
    Quote Originally Posted by ErosOlmi View Post
    Problem with GetLastError API is that it return ... last error of a Windows API call.

    But inside an interpreted language like thinBasic ... between you call to a Window Api and GetLastError call there could be other thousands of Windows Api calling by the interpreter.
    So GetLastError is, for the most cases, useless in thinBasic. It depends how you write code, when GetLastError is called compared to the API calling.
    thinBasic has to do a lot of things just after a Window API calling so when thinBasic parser has reached GetLastError ... it may be late.

    Instead GetLastError is useful in a compiled language where the code call exactly what the programmer write (well, most of the time)

    Not that way

    Note : nf-errhandlingapi-getlasterror 'The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.



    In help file:

    DECLARE

    Syntax
    DECLARE {SUB | FUNCTION} ProcName LIB "LibName" ALIAS "AliasName" [([Arguments])] [AS ReturnType] [bLastErrorAware] *

    Parameters
    Name Type Optional Meaning
    bLastErrorAware Boolean Yes Enable immediate GetLastError retrieving


    WIN_GetLastError

    Syntax
    n = WIN_GetLastError

    Parameters
    no parameter

    Returns
    number
    n = the error code number set by the last Windows API function/sub or 3rd party external DLL function call


    WIN_FormatMessage

    Syntax
    s = WIN_FormatMessage(nSysErrorCode)

    Parameters
    Name Type Optional Meaning
    nSysErrorCode number No System error code number

    Returns
    string
    s = the message definition corresponding to the error code number provided as parameter



    In thinBasic core (thinCore.dll):

    ... Pseudo script-interpretation ...
    IF (LastErrorAware is %TRUE) detected THEN ' Could be pre-parsing but not compulsorily
    STATIC lastExternalErrorCode as NUMBER
    DECLARE ONCE FUNCTION GetLastError IMPORT "KERNEL32.DLL" ALIAS "GetLastError" () AS DWORD ' Retrieves the calling thread's last-error code value.
    DECLARE ONCE FUNCTION FormatMessage IMPORT "KERNEL32.DLL" ALIAS "FormatMessage" () AS NUMBER
    ENDIF

    IF some (Windows API function/sub or 3rd party external DLL function) is called by script THEN
    ... make the call ...
    ... right after the call, ... GetLastError ' Now lastExternalErrorCode contains the last error code
    ENDIF

    IF WIN_GetLastError is called in script THEN
    Return lastExternalErrorCode as result
    ENDIF

    (...)


    * Or even simpler, it can be a global modifier :

    DECLARE GetLastError

    Syntax
    #DEFAULT DECLARE GetLastError{%TRUE | %FALSE}

    Parameters
    no parameter

    Returns
    Nothing




    Eros, this is oriented towards a solution for this thread. I hoped for a word on this at least from you
    ThinBasic 1.11.6.0 ALPHA - Windows 8.1 x64

Similar Threads

  1. is it a feature ? RAWTEXT
    By DirectuX in forum thinBasic General
    Replies: 2
    Last Post: 09-01-2020, 22:02
  2. New forum feature: Blogs
    By ErosOlmi in forum Web and Forum
    Replies: 2
    Last Post: 11-11-2010, 11:06
  3. New feature very closed to be released
    By ErosOlmi in forum thinBasic General
    Replies: 6
    Last Post: 13-09-2006, 15:35

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •