I liked this small test with fbc, but can't remember, where it came.
' Example of retrieving the WAN IP adress from an external source :
'
' GetPublicIP.exe http://whatismyip.akamai.com
' GetPublicIP.exe https://ipecho.net/plain
'
' If no command line option is specified then the application
' defaults to the address http://icanhazip.com
' Source code compiled with FreeBASIC Compiler - Version 1.09.0 (2021-12-31)
#include "windows.bi"
#include "win\wininet.bi"
Declare Function __getmainargs Cdecl Alias "__getmainargs" (Byval As Integer Ptr,_
Byval As Zstring Ptr Ptr Ptr,Byval As Zstring Ptr Ptr Ptr,_
Byval As Integer,Byval As LPSTARTUPINFO) As Integer
Public Function StdOut ( Zstr As Zstring Ptr) As Integer
Dim As HANDLE hOutput
Dim As DWORD bWritten
hOutput=GetStdHandle(STD_OUTPUT_HANDLE)
WriteFile(hOutPut,Zstr,lstrlen(ZStr),@bWritten,NULL)
Function=bWritten
End Function
Function main () As Integer
Dim As Integer i,RetVal
Dim As HINTERNET hInt,hUrl
Dim As WINBOOL Irf
Dim As DWORD BuffSize
Dim As LPCSTR url
Dim As ZString * 128 App
Dim As ZString * 68 IpAddr
Dim As ZString Ptr Buffer=@IpAddr
Dim As Integer argc
Dim As ZString Ptr Ptr argv
Dim As ZString Ptr Ptr env
Dim As Integer sinfo=0
__getmainargs(@argc,@argv,@env,0,Cast(LPSTARTUPINFO,@sinfo))
If argc=1 Then
url=Cast(ZString Ptr,@"http://icanhazip.com")
Else
url=argv[1]
End If
GetModuleFileName(0,@App,128)
hInt=InternetOpen(@App,INTERNET_OPEN_TYPE_PRECONFIG,0,0,0)
If hInt=0 Then Return 1
hUrl=InternetOpenUrl(hInt,url,0,0,0,0)
If hUrl=0 Then
InternetCloseHandle(hInt)
Return 2
End If
Irf=InternetReadFile(hUrl,@IpAddr,64,@BuffSize)
InternetCloseHandle(hUrl)
If Irf=0 Then
InternetCloseHandle(hInt)
Return 3
End If
*Cast(Ubyte Ptr,Buffer+BuffSize)=0
For i=1 To BuffSize
If *Cast(Ubyte Ptr,Buffer)<32 Then *Cast(Ubyte Ptr,Buffer)=32
Buffer+=1
Next i
StdOut IpAddr
InternetCloseHandle(hInt)
Return 0
End Function
Sub mainCRTStartup Cdecl Alias "mainCRTStartup"()
ExitProcess(main())
End Sub
Hi Timo,
Here is how to build the small sized 32-bit version :
\FreeBASIC\fbc32 -nodeflibs -c GetPublicIP.bas
\FreeBASIC\bin\win32\ld -e _mainCRTStartup -subsystem console -o GetPublicIP.exe GetPublicIP.o -L\FreeBASIC\lib\win32 -lkernel32 -luser32 -lgdi32 -lWininet -lmsvcrt -s
Turning off the run-time library, you can create small FreeBASIC executables.