WIN-API without any #Include "..."

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

WIN-API without any #Include "..."

Post by MrSwiss »

Hi all,

especially if we're doing a simple console application, it is not necessary to always #Include "Windows.bi".
The very same applies of course also, to Frameworks of any 'colour or shape'.
Some of us (including myself) simply don't want those approx. 1.5 MB of predefinitions.

There is a way to do it DIY-style, which offers, among other *goodies, the possibility to:
  • see the proper definition of internally used structs (Type in FB)
  • see how "Aliases" are defined/used
  • declaring such procedures (to later, just call them)
  • directly calling a procedure from a "system library" aka: *.DLL (kernel32, user32 etc.)
  • to tune things, with straight FB procedures
*goodies: we can internally redefine every used name, be it a struct and its members, or the procedures
name (mostly shortening for ease of use in Main-code).

Proof of concept code:

Code: Select all

' Without_Windows_Bi.bas -- (c) 2020-03-07, MrSwiss
'
' uses: WIN-API without #Include's (obviously: WIN only!)
' compile: -s console
'

' whenever a WIN-API call needs a return-struct, we'll have to define it first
Type SysTime                            ' size 8 x 2 bytes (WORD) = 16 bytes
    As UShort   _yr, _mth, _dow, _dy, _ ' date-parts & day_of_week (0 = Sunday)
                _hr, _min, _sec, _ms    ' time-parts
End Type
Type pSysTime   As SysTime Ptr          ' ptr alias (struct ptr) 

Type FileTime   As ULongInt             ' data-type alias (no struct needed)
Type pFileTime  As ULongInt Ptr         ' ptr alias

' the obvious advantage is here: we freely declare our own procedure names _
' instead of, being forced to use what M$ has decided to call them !!!
Declare Sub      GetSysTime Lib "kernel32" Alias "GetSystemTime"(ByVal As pSysTime)
Declare Sub      GetLocTime Lib "kernel32" Alias "GetLocalTime" (ByVal As pSysTime)
Declare Function SysT2FileT Lib "kernel32" Alias "SystemTimeToFileTime" _
                        (ByVal As pSysTime, ByVal As pFileTime) As Boolean
' Windows bool-type = Long in FB, however, FB's Boolean can also be used!

' FB procedures for convenience: numeric to string conversions
Declare Function DOW2Strg(ByVal DOW As UShort, ByVal sDOW As Boolean = FALSE) ByRef As Const ZString
Declare Function Mth2Strg(ByVal mth As UShort, ByVal smth As Boolean = FALSE) ByRef As Const ZString

' ===== DEMO code =====
' mandatory variables
Dim As SysTime      st, lt              ' 2 instances of type = struct in C/CPP
Dim As ULongInt     ft                  ' unsigned 64 bit int-type (fixed size)
  
GetSysTime(@st)                         ' UTC-Time
Sleep(1, 1)                             ' force minimal delay (msec check)
GetLocTime(@lt)                         ' LocalTime

If SysT2FileT(@st, @ft) Then            ' using return for ERROR checking
    ? "FileTime : "; ft : ?             ' OK = return result
Else
    ? "SystemTimeToFileTime() failure!" ' NOK = deal with ERROR (show/quit)
    ? : ? "press a key, to QUIT ";      ' wait for user action, then _
    Sleep : End 1                       ' return ErrorLevel = 1 (0 = OK)
End If

? "SystemTime UDT in 'UTC' raw: numerical"  ' show obtained results
With st
    ? "Year     : "; ._yr
    ? "Month    : "; ._mth
    ? "DayOfWeek: "; ._dow
    ? "Day      : "; ._dy
    ? "Hour     : "; ._hr
    ? "Minute   : "; ._min
    ? "Second   : "; ._sec
    ? "MilliSec.: "; ._ms
End With
?
? "SystemTime UDT in 'Local Time'"
? "(tuned with String-Converter Functions)"
With lt
    ? "Year     : "; ._yr
    ? "Month    : "; Mth2Strg(._mth, TRUE)  ' conversion to short-cut string
    ? "DayOfWeek: "; DOW2Strg(._dow)    ' conversion to full string (default)
    ? "Day      : "; ._dy
    ? "Hour     : "; ._hr
    ? "Minute   : "; ._min
    ? "Second   : "; ._sec
    ? "MilliSec.: "; ._ms
End With

? : ? : ? "... done ... ";
Sleep
' ===== end DEMO code =====

' implementation(s) ...
Function DOW2Strg( _                    ' DOW to string conversion
    ByVal DOW   As UShort,         _    ' as returned in SysTime._dow
    ByVal sDOW  As Boolean = FALSE _    ' if TRUE then: short-cut week-day
    ) ByRef As Const ZString            ' just string-data (no header)
    Static As ZString * 10 _
    DOWl(...)  => { "Sunday", "Monday", "Tuesday", "Wednesday", _
                    "Thursday", "Friday", "Saturday" }
    Static As ZString * 4 _
    DOWs(...)  => { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }
    
    If sDOW Then Return DOWs(DOW) _
        Else Return DOWl(DOW)
End Function

Function Mth2Strg( _                    ' month to string conversion
    ByVal mth   As UShort,         _    ' as returned in SysTime._mt
    ByVal smth  As Boolean = FALSE _    ' if TRUE then: short-cut month
    ) ByRef As Const ZString            ' just string-data (no header)
    Static As ZString * 10 _
    mthl(1 To ...) => { "January", "February", "March", "April", _
                        "May", "June", "July", "August", "September", _
                        "October", "November", "December" }
    Static As ZString * 4 _
    mths(1 To ...) => { "Jan", "Feb", "Mar", "Apr", "May", "Jun", _
                        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
    
    If smth Then Return mths(mth) _
        Else Return mthl(mth)
End Function
' ----- EOF -----
Added: all data-type definitions are FB-native (none of M$'s data-types).
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: WIN-API without any #Include "..."

Post by marcov »

And how do you validate it? Specially if you need non trivial pieces?

P.s. if you like wasting time, consider making lace by hand. It is particularly pointless.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: WIN-API without any #Include "..."

Post by D.J.Peters »

"dirty Tip of the week" :-)

The Windows API is well documented and used the right code for you !
(ASCII vs. UNICODE and more important 32-bit vs. 64-bit ...)

Joshy
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: WIN-API without any #Include "..."

Post by paul doe »

MrSwiss wrote:...
Some of us (including myself) simply don't want those approx. 1.5 MB of predefinitions.
...
...or do anything particularly meaningful, for that matter.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: WIN-API without any #Include "..."

Post by dafhi »

I for one find this post interesting. Thank you Mr. Swiss
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: WIN-API without any #Include "..."

Post by caseih »

MrSwiss wrote:Some of us (including myself) simply don't want those approx. 1.5 MB of predefinitions.
Yes that will slow the compiler down, especially if working with multiple modules. The good news is it doesn't affect the executable size in any way.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: WIN-API without any #Include "..."

Post by badidea »

I am not much of a Windows-API user, but there are some crazy function names indeed.
E.g. GetDynamicTimeZoneInformationEffectiveYears()
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: WIN-API without any #Include "..."

Post by jj2007 »

caseih wrote:Yes that will slow the compiler down
Absolutely! With my old Core i5, it's about 0.13 seconds more.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: WIN-API without any #Include "..."

Post by dodicat »

All the winapi routines reside in a .dll file, so only those written down in code will be called of course.
I agree with Mr Swiss to sometimes only define those you need (a subset of the .bi includes).
There are many not in any of the .bi files.
Here are a couple:

Code: Select all


Declare Function SetWindowTheme Lib "UxTheme.dll" Alias "SetWindowTheme"(As Any Ptr,As zstring Ptr,As zstring Ptr) As Long
declare function msgbox lib "user32.dll" alias "MessageBoxTimeoutA"(as any ptr,as zstring,as zstring,as long,as long,as long)  as long
msgbox(0,"Wait 2 seconds","Hello",0,0,2000)

screen 8
dim as any ptr win
Var Ip = Cptr(Integer Ptr,@Win )
    Screencontrol 2,*Ip
    SetWindowTheme(win,"","")
   
    msgbox(0,"Wait 5 seconds or click OK to end . . . ","Ending themed window test . . .",0,0,5000)
   
    

  
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: WIN-API without any #Include "..."

Post by UEZ »

@MrSwiss: just for my curiosity - why have you changed your paradigm?
MrSwiss wrote:
...
Without external libraries, which always introduce dependencies ...
(keeping it nice, and multi OS)

Just pure and simple FB code, using internals only!
MrSwiss wrote:
You may want to become a "Programmer" and, not just stay put as "Library Linker".
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: WIN-API without any #Include "..."

Post by deltarho[1859] »

@dodicat

With regard the 'time out' function if we use it in forum published code we should mention that MessageBoxTimeoutA, and the other two variants, are undocumented functions.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: WIN-API without any #Include "..."

Post by jj2007 »

deltarho[1859] wrote:we should mention that MessageBoxTimeoutA, and the other two variants, are undocumented functions.
Correct. I had some weird problems with this function, too. In the end I decided to use the normal MessageBox in a thread that I could kill after a while.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: WIN-API without any #Include "..."

Post by deltarho[1859] »

Over at the PowerBASIC forums there have been many message box timeout procedures using various methods other than the undocumented functions. Eventually, I chipped in with a version which uses the least amount of code. It was frowned upon by one of the PB members until I mentioned that I got the idea from a Microsoft technician at MSDN. The PB member went quiet.

I have used it in many applications for quite a few years and found it to be robust.

I will dig it out and see if I can port it to FreeBASIC.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: WIN-API without any #Include "..."

Post by deltarho[1859] »

I have ported it to FreeBASIC: Messagebox timeout
Post Reply