| Welcome, Guest |
You have to register before you can post on our site.
|
|
|
| WorldWeather.bas |
|
Posted by: Stanley Durham - 1 hour ago - Forum: Source Code Library
- Replies (1)
|
 |
Shows country location on map and weather, based on selection.
AI doesn’t remove your involvement; it explodes what you can do.
You can do things fast that you wouldn’t have attempted.
One prompt, DeepSeek. Got to have Python installed, AI main tool.
=======
Prompt:
=======
compiler: PowerBASIC
compiler documentation: C:\Temp\PB\PB_AI.md
compiler documentation: C:\Temp\PB\PBHelp.html
file: C:\Temp\WorldWeather\WorldWeather.bas
file: C:\Temp\WorldWeather\Countries.txt
---
use only PowerBASIC code, no external code.
in Countries.txt, build a list of all countries.
WorldWeather.bas is a Windows GUI app.
put Countries.txt in a listbox.
when a country is selected, using PowerBASIC graphic commands, draw a map of where that country is on a world map.
in a textbox, give the current weather.
=======
C:\Temp\PB\PBHelp.html is PB help converted to HTML.
C:\Temp\PB\PB_AI.md, I asked DS to build a AI reference file from C:\Temp\PB\PBHelp.html.
WorldWeather.bas
Code: #Compile Exe
#Dim All
#Include Once "DDT.INC"
%ID_LIST = 1001
%ID_MAP = 1002
%ID_TEXT = 1003
%MAX_COUNTRIES = 300
Type PolyPoint
x As Single
y As Single
End Type
Type PolyPoints
n As Long
xy(1 To 1024) As PolyPoint
End Type
Global gN As Long
Global gName() As String
Global gCap() As String
Global gLat() As Single
Global gLon() As Single
Global gMinLat() As Single
Global gMaxLat() As Single
Global gMinLon() As Single
Global gMaxLon() As Single
Function LoadCountries() As Long
Local f As Long, ln As String, n As Long, Path As String
Path = Exe.Path$
If Right$(Path, 1) <> "\" Then Path = Path & "\"
Path = Path & "Countries.txt"
If IsFile(Path) = 0 Then
Function = 0
Exit Function
End If
f = FreeFile
Open Path For Input As #f
Do While Not Eof(f)
Line Input #f, ln$
ln$ = Trim$(ln$)
If Len(ln$) > 0 Then
Incr n
gName(n) = Parse$(ln$, Chr$(9), 1)
gCap(n) = Parse$(ln$, Chr$(9), 2)
gLat(n) = Val(Parse$(ln$, Chr$(9), 3))
gLon(n) = Val(Parse$(ln$, Chr$(9), 4))
gMinLat(n) = Val(Parse$(ln$, Chr$(9), 5))
gMaxLat(n) = Val(Parse$(ln$, Chr$(9), 6))
gMinLon(n) = Val(Parse$(ln$, Chr$(9), 7))
gMaxLon(n) = Val(Parse$(ln$, Chr$(9), 8))
End If
Loop
Close #f
gN = n
Function = n
End Function
Sub DrawMap(hDlg As Dword, idx As Long)
Local pp As PolyPoints
Local i As Long, p As Long, k As Long, np As Long, ix As Long
Local lon As Single, lat As Single
Local s As String
Graphic Attach hDlg, %ID_MAP, ReDraw
Graphic Scale (-180, 90) - (180, -90)
Graphic Clear RGB(232, 244, 255)
' Latitude/longitude grid
Graphic Width 1
For lon = -150 To 150 Step 30
Graphic Line (lon, 90) - (lon, -90), RGB(205, 225, 240)
Next lon
For lat = -60 To 60 Step 30
Graphic Line (-180, lat) - (180, lat), RGB(205, 225, 240)
Next lat
Graphic Line (-180, 0) - (180, 0), RGB(165, 195, 220)
Graphic Line (0, 90) - (0, -90), RGB(165, 195, 220)
' Land polygons
ix = 1
p = Val(Read$(ix)) : Incr ix
For i = 1 To p
np = Val(Read$(ix)) : Incr ix
pp.n = np
For k = 1 To np
s = Read$(ix) : Incr ix
pp.xy(k).x = Val(Parse$(s, ",", 1))
pp.xy(k).y = Val(Parse$(s, ",", 2))
Next k
Graphic Polygon pp, RGB(90, 130, 90), RGB(170, 210, 160)
Next i
' Selected country: approximate bounds, capital marker and crosshair
If idx >= 1 And idx <= gN Then
Graphic Width 2
Graphic Box (gMinLon(idx), gMaxLat(idx)) - (gMaxLon(idx), gMinLat(idx)), 0, RGB(215, 30, 30)
Graphic Width 1
Graphic Ellipse (gLon(idx) - 2.4, gLat(idx) + 2.4) - (gLon(idx) + 2.4, gLat(idx) - 2.4), RGB(215, 30, 30), RGB(255, 80, 80)
Graphic Line (gLon(idx) - 6, gLat(idx)) - (gLon(idx) + 6, gLat(idx)), RGB(215, 30, 30)
Graphic Line (gLon(idx), gLat(idx) - 6) - (gLon(idx), gLat(idx) + 6), RGB(215, 30, 30)
End If
Graphic ReDraw
Data "69"
Data "262"
Data "106.97,76.97"
Data "114.13,75.85"
Data "109.40,74.18"
Data "126.98,73.57"
Data "131.29,70.79"
Data "132.25,71.84"
Data "139.87,71.49"
Data "139.15,72.42"
Data "140.47,72.85"
Data "159.00,70.87"
Data "160.94,69.44"
Data "178.60,69.40"
Data "180.00,68.96"
Data "180.00,64.98"
Data "177.41,64.61"
Data "179.49,62.57"
Data "173.68,61.65"
Data "170.33,59.88"
Data "163.54,59.87"
Data "162.02,58.24"
Data "163.19,57.62"
Data "162.12,54.86"
Data "156.79,51.01"
Data "155.91,56.77"
Data "164.47,62.55"
Data "160.12,60.54"
Data "159.30,61.77"
Data "156.72,61.43"
Data "154.22,59.76"
Data "155.04,59.15"
Data "142.20,59.04"
Data "135.13,54.73"
Data "139.90,54.19"
Data "141.38,52.24"
Data "138.22,46.31"
Data "127.53,39.76"
Data "129.46,36.78"
Data "129.09,35.08"
Data "126.49,34.39"
Data "126.86,36.89"
Data "124.71,38.11"
Data "125.32,39.55"
Data "121.05,38.90"
Data "121.64,40.95"
Data "118.04,39.20"
Data "118.91,37.45"
Data "122.36,37.45"
Data "119.15,34.91"
Data "121.91,31.69"
Data "121.68,28.23"
Data "115.89,22.78"
Data "110.79,21.40"
Data "110.44,20.34"
Data "108.52,21.72"
Data "105.88,19.75"
Data "109.34,13.43"
Data "109.20,11.67"
Data "105.16,8.60"
Data "105.08,9.92"
Data "100.10,13.41"
Data "99.22,9.24"
Data "102.96,5.52"
Data "104.23,1.29"
Data "101.39,2.76"
Data "100.09,6.46"
Data "98.34,7.79"
Data "98.76,11.44"
Data "97.16,16.93"
Data "94.19,16.04"
Data "94.32,18.21"
Data "91.42,22.77"
Data "86.98,21.50"
Data "80.32,15.90"
Data "79.86,10.36"
Data "77.54,7.97"
Data "73.53,15.99"
Data "72.63,21.36"
Data "70.47,20.88"
Data "66.37,25.43"
Data "57.40,25.74"
Data "56.49,27.14"
Data "54.72,26.48"
Data "51.52,27.87"
Data "50.12,30.15"
Data "47.97,29.98"
Data "50.81,24.75"
Data "51.59,25.80"
Data "51.79,24.02"
Data "54.01,24.12"
Data "56.36,26.40"
Data "56.85,24.24"
Data "59.81,22.31"
Data "55.27,17.23"
Data "43.48,12.64"
Data "42.65,16.77"
Data "34.63,28.06"
Data "34.92,29.50"
Data "33.92,27.65"
Data "32.42,29.85"
Data "36.87,22.00"
Data "37.48,18.61"
Data "43.32,12.39"
Data "42.72,11.74"
Data "44.61,10.44"
Data "51.11,12.02"
Data "51.05,10.64"
Data "47.74,4.22"
Data "39.20,-4.68"
Data "40.78,-14.69"
Data "34.79,-19.78"
Data "35.61,-23.71"
Data "32.57,-25.73"
Data "32.20,-28.75"
Data "25.78,-33.94"
Data "18.38,-34.14"
Data "18.22,-31.66"
Data "15.21,-27.09"
Data "14.26,-22.11"
Data "11.79,-18.07"
Data "13.69,-10.73"
Data "11.92,-5.04"
Data "8.80,-1.11"
Data "9.40,3.73"
Data "8.50,4.77"
Data "5.90,4.26"
Data "4.33,6.27"
Data "-1.96,4.71"
Data "-9.00,4.83"
Data "-16.61,12.17"
Data "-17.62,14.73"
Data "-16.15,18.11"
Data "-16.97,21.89"
Data "-14.44,26.25"
Data "-9.56,29.93"
Data "-9.30,32.56"
Data "-5.93,35.76"
Data "-2.17,35.17"
Data "9.51,37.35"
Data "11.10,36.90"
Data "10.34,33.79"
Data "19.09,30.27"
Data "21.54,32.84"
Data "28.91,30.87"
Data "33.77,30.97"
Data "36.16,36.65"
Data "27.64,36.66"
Data "26.17,39.46"
Data "33.51,42.02"
Data "38.35,40.95"
Data "41.70,41.96"
Data "36.68,45.24"
Data "39.12,47.26"
Data "34.96,46.27"
Data "36.33,45.11"
Data "33.88,44.36"
Data "32.45,45.33"
Data "33.30,46.08"
Data "30.75,46.58"
Data "27.67,42.58"
Data "28.81,41.05"
Data "22.63,40.26"
Data "24.04,37.66"
Data "23.12,37.92"
Data "22.49,36.41"
Data "19.41,40.25"
Data "19.54,41.72"
Data "13.14,45.74"
Data "12.59,44.09"
Data "18.48,40.17"
Data "16.87,40.44"
Data "16.10,37.99"
Data "15.41,40.05"
Data "8.89,44.37"
Data "3.10,43.08"
Data "-2.15,36.67"
Data "-5.38,35.95"
Data "-8.90,36.87"
Data "-9.39,43.03"
Data "-1.38,44.02"
Data "-1.19,46.01"
Data "-4.59,48.68"
Data "-1.62,48.64"
Data "-1.93,49.78"
Data "1.34,50.13"
Data "4.71,53.09"
Data "8.12,53.53"
Data "8.54,57.11"
Data "10.58,57.73"
Data "10.91,56.46"
Data "9.65,55.47"
Data "10.94,54.01"
Data "19.66,54.43"
Data "21.27,55.19"
Data "21.58,57.41"
Data "24.12,57.03"
Data "24.43,58.38"
Data "23.34,59.19"
Data "29.12,60.03"
Data "21.32,60.72"
Data "21.54,63.19"
Data "25.40,65.11"
Data "22.18,65.72"
Data "17.85,62.75"
Data "17.12,61.34"
Data "18.79,60.08"
Data "16.83,58.72"
Data "15.88,56.10"
Data "12.94,55.36"
Data "10.36,59.47"
Data "8.38,58.31"
Data "5.67,58.59"
Data "4.99,61.97"
Data "14.76,67.81"
Data "24.55,71.03"
Data "28.17,71.19"
Data "31.29,70.45"
Data "30.01,70.19"
Data "31.10,69.56"
Data "40.29,67.93"
Data "41.13,66.79"
Data "40.02,66.27"
Data "33.18,66.63"
Data "34.81,65.90"
Data "34.94,64.41"
Data "37.01,63.85"
Data "37.18,65.14"
Data "39.59,64.52"
Data "42.09,66.48"
Data "43.95,66.07"
Data "43.45,68.57"
Data "46.25,68.25"
Data "46.82,67.69"
Data "45.56,67.01"
Data "46.35,66.67"
Data "53.72,68.86"
Data "59.94,68.28"
Data "61.08,68.94"
Data "60.55,69.85"
Data "68.51,68.09"
Data "69.18,68.62"
Data "66.93,69.45"
Data "66.69,71.03"
Data "69.20,72.84"
Data "72.59,72.78"
Data "71.85,71.41"
Data "73.67,68.41"
Data "71.28,66.32"
Data "72.42,66.17"
Data "75.05,67.76"
Data "73.60,69.63"
Data "74.40,70.63"
Data "73.10,71.45"
Data "74.66,72.83"
Data "76.36,71.15"
Data "75.90,71.87"
Data "77.58,72.27"
Data "81.50,71.75"
Data "80.51,73.65"
Data "86.82,73.94"
Data "86.01,74.46"
Data "87.17,75.12"
Data "104.35,77.70"
Data "171"
Data "-90.55,69.50"
Data "-90.55,68.48"
Data "-89.22,69.26"
Data "-87.35,67.20"
Data "-85.52,69.88"
Data "-82.62,69.66"
Data "-81.28,69.16"
Data "-81.26,67.60"
Data "-85.77,66.56"
Data "-93.16,62.02"
Data "-94.68,58.95"
Data "-93.22,58.78"
Data "-92.30,57.09"
Data "-82.27,55.15"
Data "-82.12,53.28"
Data "-79.91,51.21"
Data "-78.60,52.56"
Data "-79.83,54.67"
Data "-76.54,56.53"
Data "-78.52,58.80"
Data "-77.34,59.85"
Data "-78.11,62.32"
Data "-73.84,62.44"
Data "-69.59,61.06"
Data "-69.29,58.96"
Data "-67.65,58.21"
Data "-64.58,60.34"
Data "-61.80,56.34"
Data "-57.33,54.63"
Data "-55.68,52.15"
Data "-60.03,50.24"
Data "-66.40,50.23"
Data "-71.10,46.82"
Data "-65.06,49.23"
Data "-64.17,48.74"
Data "-65.12,48.07"
Data "-64.47,46.24"
Data "-61.52,45.88"
Data "-60.52,47.01"
Data "-59.80,45.92"
Data "-65.36,43.55"
Data "-66.16,44.47"
Data "-64.43,45.29"
Data "-67.14,45.14"
Data "-70.69,43.03"
Data "-69.97,41.64"
Data "-75.53,39.50"
Data "-75.94,37.22"
Data "-76.35,39.15"
Data "-76.96,38.23"
Data "-75.73,35.55"
Data "-81.34,31.44"
Data "-80.38,25.21"
Data "-84.10,30.09"
Data "-93.85,29.71"
Data "-96.59,28.31"
Data "-97.87,22.44"
Data "-96.29,19.32"
Data "-94.43,18.14"
Data "-92.04,18.70"
Data "-90.28,21.00"
Data "-87.05,21.54"
Data "-88.93,15.89"
Data "-83.41,15.27"
Data "-83.81,11.10"
Data "-81.44,8.79"
Data "-79.57,9.61"
Data "-76.84,8.64"
Data "-74.91,11.08"
Data "-71.75,12.44"
Data "-71.70,9.07"
Data "-69.94,12.16"
Data "-68.19,10.55"
Data "-61.88,10.72"
Data "-62.39,9.95"
Data "-57.15,5.97"
Data "-51.32,4.20"
Data "-49.97,1.74"
Data "-50.39,-0.08"
Data "-44.91,-1.55"
Data "-44.58,-2.69"
Data "-39.98,-2.87"
Data "-35.60,-5.15"
Data "-34.73,-7.34"
Data "-38.67,-13.06"
Data "-40.94,-21.94"
Data "-47.65,-24.89"
Data "-48.89,-28.67"
Data "-53.81,-34.40"
Data "-58.43,-33.91"
Data "-56.79,-36.90"
Data "-59.23,-38.72"
Data "-62.34,-38.83"
Data "-62.75,-41.03"
Data "-65.12,-41.06"
Data "-63.46,-42.56"
Data "-67.29,-45.55"
Data "-65.99,-48.13"
Data "-69.14,-50.73"
Data "-68.15,-52.35"
Data "-70.85,-52.90"
Data "-71.01,-53.83"
Data "-74.95,-52.26"
Data "-75.61,-48.67"
Data "-74.13,-46.94"
Data "-75.64,-46.65"
Data "-72.72,-42.38"
Data "-74.33,-43.22"
Data "-73.59,-37.16"
Data "-71.44,-32.42"
Data "-70.16,-19.76"
Data "-71.46,-17.36"
Data "-76.01,-14.65"
Data "-81.25,-6.14"
Data "-79.77,-2.66"
Data "-80.93,-1.06"
Data "-77.13,3.85"
Data "-78.18,8.32"
Data "-79.56,8.93"
Data "-80.89,7.22"
Data "-85.66,9.93"
Data "-87.49,13.30"
Data "-103.50,18.29"
Data "-105.49,19.95"
Data "-106.03,22.77"
Data "-113.87,31.57"
Data "-114.78,31.80"
Data "-114.67,30.16"
Data "-109.41,23.36"
Data "-110.03,22.82"
Data "-112.18,24.74"
Data "-112.30,26.01"
Data "-115.06,27.72"
Data "-114.16,28.57"
Data "-117.30,33.05"
Data "-120.62,34.61"
Data "-124.40,40.31"
Data "-124.69,48.18"
Data "-122.59,47.10"
Data "-122.84,49.00"
Data "-127.44,50.83"
Data "-134.08,58.12"
Data "-147.11,60.88"
Data "-151.72,59.16"
Data "-150.62,61.28"
Data "-158.43,55.99"
Data "-164.79,54.40"
Data "-157.72,57.57"
Data "-157.04,58.92"
Data "-161.97,58.67"
Data "-161.87,59.63"
Data "-166.12,61.50"
Data "-164.56,63.15"
Data "-160.77,63.77"
Data "-160.78,64.79"
Data "-164.96,64.45"
Data "-168.11,65.67"
Data "-161.68,66.12"
Data "-166.76,68.36"
Data "-156.58,71.36"
Data "-136.50,68.90"
Data "-128.14,70.48"
Data "-108.88,67.38"
Data "-108.17,68.65"
Data "-106.15,68.80"
Data "-101.45,67.65"
Data "-97.67,68.58"
Data "-96.13,67.29"
Data "-94.23,69.07"
Data "-96.47,70.09"
Data "-95.21,71.92"
Data "57"
Data "-58.61,-64.15"
Data "-62.02,-64.80"
Data "-62.12,-66.19"
Data "-65.67,-67.95"
Data "-61.81,-70.72"
Data "-60.83,-73.70"
Data "-70.60,-76.63"
Data "-77.24,-76.71"
Data "-73.66,-77.91"
Data "-78.02,-79.18"
Data "-58.22,-83.22"
Data "-28.55,-80.34"
Data "-29.69,-79.26"
Data "-35.64,-79.46"
Data "-35.33,-78.12"
Data "-17.52,-75.13"
Data "-15.70,-74.50"
Data "-16.47,-73.87"
Data "-15.45,-73.15"
Data "-6.87,-70.93"
Data "-0.23,-71.64"
Data "7.74,-69.89"
Data "10.82,-70.83"
Data "13.42,-69.97"
Data "27.09,-70.46"
Data "33.87,-68.50"
Data "38.65,-69.78"
Data "54.53,-65.82"
Data "61.43,-67.95"
Data "68.89,-67.93"
Data "69.67,-69.23"
Data "67.81,-70.31"
Data "69.07,-70.68"
Data "67.95,-71.85"
Data "69.87,-72.26"
Data "73.86,-69.87"
Data "87.99,-66.21"
Data "95.78,-67.39"
Data "99.72,-67.25"
Data "102.83,-65.56"
Data "106.18,-66.93"
Data "113.60,-65.88"
Data "119.83,-67.27"
Data "134.76,-66.21"
Data "135.07,-65.31"
Data "137.46,-66.95"
Data "145.49,-66.92"
Data "148.84,-68.39"
Data "171.21,-71.70"
Data "163.57,-76.24"
Data "164.74,-78.18"
Data "167.00,-78.75"
Data "161.77,-79.16"
Data "159.79,-80.95"
Data "169.40,-83.83"
Data "180.00,-84.71"
Data "180.00,-90.00"
Data "33"
Data "143.56,-13.76"
Data "145.37,-14.98"
Data "146.39,-18.96"
Data "148.85,-20.39"
Data "153.14,-26.07"
Data "152.89,-31.64"
Data "150.00,-37.43"
Data "146.32,-39.04"
Data "145.03,-37.90"
Data "143.61,-38.81"
Data "140.64,-38.02"
Data "138.12,-35.61"
Data "138.21,-34.38"
Data "136.83,-35.26"
Data "137.81,-32.90"
Data "135.99,-34.89"
Data "134.27,-32.62"
Data "131.33,-31.50"
Data "118.02,-35.06"
Data "115.03,-34.20"
Data "115.69,-31.61"
Data "113.34,-26.12"
Data "114.23,-26.30"
Data "113.39,-24.38"
Data "114.15,-21.76"
Data "120.86,-19.68"
Data "125.69,-14.23"
Data "129.62,-14.97"
Data "132.36,-11.13"
Data "136.49,-11.86"
Data "135.50,-15.00"
Data "140.22,-17.71"
Data "142.14,-11.04"
Data "42"
Data "-27.10,83.52"
Data "-20.85,82.73"
Data "-31.40,82.02"
Data "-12.21,81.29"
Data "-20.05,80.18"
Data "-17.73,80.13"
Data "-19.70,78.75"
Data "-18.47,76.99"
Data "-21.68,76.63"
Data "-19.83,76.10"
Data "-20.67,75.16"
Data "-19.37,74.30"
Data "-23.57,73.31"
Data "-22.30,72.18"
Data "-24.79,72.33"
Data "-21.75,70.66"
Data "-25.54,71.43"
Data "-26.36,70.23"
Data "-22.35,70.13"
Data "-39.81,65.46"
Data "-42.82,62.68"
Data "-43.38,60.10"
Data "-48.26,60.86"
Data "-51.63,63.63"
Data "-53.97,67.19"
Data "-50.87,69.93"
Data "-54.68,69.61"
Data "-54.36,70.82"
Data "-51.39,70.57"
Data "-55.83,71.65"
Data "-54.72,72.59"
Data "-58.59,75.52"
Data "-68.50,76.06"
Data "-71.40,77.01"
Data "-66.76,77.38"
Data "-73.30,78.04"
Data "-65.71,79.39"
Data "-68.02,80.12"
Data "-62.65,81.77"
Data "-44.52,81.66"
Data "-46.76,82.63"
Data "-43.41,83.23"
Data "24"
Data "-180.00,-90.00"
Data "-180.00,-84.71"
Data "-179.06,-84.14"
Data "-143.11,-85.04"
Data "-153.59,-83.69"
Data "-152.86,-82.04"
Data "-156.84,-81.10"
Data "-146.42,-80.34"
Data "-155.33,-79.06"
Data "-158.37,-76.89"
Data "-151.33,-77.40"
Data "-146.10,-76.48"
Data "-146.20,-75.38"
Data "-144.91,-75.20"
Data "-113.94,-73.71"
Data "-112.30,-74.71"
Data "-100.65,-75.30"
Data "-103.68,-72.62"
Data "-74.89,-73.87"
Data "-67.37,-72.48"
Data "-68.54,-69.72"
Data "-67.25,-66.88"
Data "-63.00,-64.64"
Data "-57.22,-63.53"
Data "25"
Data "-86.56,73.16"
Data "-82.32,73.75"
Data "-80.75,72.06"
Data "-77.82,72.75"
Data "-72.24,71.56"
Data "-67.91,70.12"
Data "-66.97,69.19"
Data "-68.81,68.72"
Data "-61.85,66.86"
Data "-63.92,65.00"
Data "-68.02,66.26"
Data "-64.67,63.39"
Data "-68.78,63.75"
Data "-66.17,61.93"
Data "-74.83,64.68"
Data "-77.71,64.23"
Data "-78.56,64.57"
Data "-77.90,65.31"
Data "-73.96,65.45"
Data "-72.93,67.73"
Data "-78.96,70.17"
Data "-88.68,70.41"
Data "-90.21,72.24"
Data "-88.41,73.54"
Data "-85.83,73.80"
Data "15"
Data "-68.50,83.11"
Data "-61.89,82.36"
Data "-76.91,79.32"
Data "-75.39,78.53"
Data "-80.56,76.18"
Data "-89.49,76.47"
Data "-87.77,77.18"
Data "-88.26,77.90"
Data "-84.98,77.54"
Data "-87.96,78.37"
Data "-85.09,79.35"
Data "-86.93,80.25"
Data "-81.85,80.46"
Data "-87.60,80.52"
Data "-91.59,81.89"
Data "17"
Data "134.14,-1.15"
Data "135.46,-3.37"
Data "138.33,-1.70"
Data "144.58,-3.86"
Data "147.65,-6.08"
Data "147.19,-7.39"
Data "150.69,-10.58"
Data "147.91,-10.13"
Data "144.74,-7.63"
Data "142.63,-9.33"
Data "137.61,-8.41"
Data "138.67,-7.32"
Data "137.93,-5.39"
Data "132.98,-4.11"
Data "131.99,-2.82"
Data "133.70,-2.21"
Data "130.52,-0.94"
Data "11"
Data "117.88,1.83"
Data "119.00,0.90"
Data "117.81,0.78"
Data "116.15,-4.01"
Data "110.22,-2.93"
Data "109.09,-0.46"
Data "109.66,2.01"
Data "113.00,3.10"
Data "116.73,6.92"
Data "119.18,5.41"
Data "117.31,3.23"
Data "8"
Data "50.06,-13.56"
Data "50.38,-15.71"
Data "47.10,-24.94"
Data "44.04,-24.99"
Data "43.25,-22.06"
Data "44.45,-16.22"
Data "47.71,-14.59"
Data "49.19,-12.04"
Data "14"
Data "-114.17,73.12"
Data "-109.92,72.96"
Data "-108.19,71.65"
Data "-108.40,73.09"
Data "-106.52,73.08"
Data "-101.09,69.58"
Data "-102.73,69.50"
Data "-102.43,68.75"
Data "-116.11,69.17"
Data "-117.34,69.96"
Data "-112.42,70.37"
Data "-117.90,70.54"
Data "-116.11,71.31"
Data "-119.40,71.56"
Data "7"
Data "105.82,-5.85"
Data "102.58,-4.22"
Data "95.29,5.48"
Data "97.48,5.25"
Data "103.84,0.10"
Data "103.44,-0.71"
Data "106.11,-3.06"
Data "16"
Data "140.98,37.14"
Data "140.25,35.14"
Data "135.79,33.46"
Data "135.08,34.60"
Data "130.99,33.89"
Data "132.00,33.15"
Data "131.33,31.45"
Data "130.20,31.42"
Data "129.41,33.30"
Data "132.62,35.43"
Data "135.68,35.53"
Data "136.72,37.30"
Data "139.43,38.22"
Data "140.31,41.20"
Data "141.37,41.38"
Data "141.91,39.99"
Data "14"
Data "-3.01,58.63"
Data "-4.07,57.55"
Data "-1.96,57.68"
Data "-3.12,55.97"
Data "1.68,52.74"
Data "1.45,51.29"
Data "-5.25,49.96"
Data "-3.41,51.43"
Data "-5.27,51.99"
Data "-4.22,52.30"
Data "-4.58,53.50"
Data "-2.95,53.98"
Data "-6.15,56.79"
Data "-5.01,58.63"
Data "6"
Data "57.54,70.72"
Data "51.60,71.47"
Data "55.63,75.08"
Data "68.85,76.54"
Data "58.48,74.31"
Data "55.42,72.37"
Data "6"
Data "-120.46,71.40"
Data "-125.93,71.87"
Data "-123.94,73.68"
Data "-124.92,74.29"
Data "-117.56,74.19"
Data "-115.51,73.48"
Data "8"
Data "-14.51,66.46"
Data "-13.61,65.13"
Data "-18.66,63.50"
Data "-22.76,63.96"
Data "-21.78,64.40"
Data "-23.96,64.89"
Data "-22.23,65.38"
Data "-24.33,65.61"
Data "4"
Data "18.25,79.70"
Data "21.54,78.96"
Data "15.91,76.77"
Data "10.44,79.65"
Data "7"
Data "-175.01,66.58"
Data "-169.90,65.98"
Data "-172.53,65.44"
Data "-172.96,64.25"
Data "-178.69,66.11"
Data "-180.00,64.98"
Data "-180.00,68.96"
Data "4"
Data "-45.15,-78.05"
Data "-43.33,-80.03"
Data "-54.16,-80.63"
Data "-48.66,-78.05"
Data "5"
Data "173.02,-40.92"
Data "174.25,-41.35"
Data "173.08,-43.85"
Data "169.33,-46.64"
Data "166.68,-46.22"
Data "13"
Data "125.24,1.42"
Data "123.69,0.24"
Data "120.18,0.24"
Data "120.94,-1.41"
Data "123.34,-0.62"
Data "121.51,-1.90"
Data "123.16,-5.34"
Data "121.49,-4.57"
Data "120.97,-2.63"
Data "120.43,-5.53"
Data "119.37,-5.38"
Data "118.77,-2.80"
Data "120.04,0.57"
Data "5"
Data "-94.68,77.10"
Data "-79.83,74.92"
Data "-92.42,74.84"
Data "-93.89,76.32"
Data "-97.12,76.75"
Data "4"
Data "-87.02,79.66"
Data "-90.80,78.22"
Data "-96.71,80.16"
Data "-92.41,81.26"
Data "9"
Data "-56.13,50.69"
Data "-56.80,49.81"
Data "-53.48,49.25"
Data "-53.07,46.66"
Data "-54.24,47.75"
Data "-55.40,46.88"
Data "-59.27,47.60"
Data "-57.36,50.72"
Data "-55.87,51.63"
Data "9"
Data "-108.21,76.20"
Data "-105.88,75.97"
Data "-106.31,75.01"
Data "-113.74,74.39"
Data "-111.79,75.16"
Data "-117.71,75.22"
Data "-115.40,76.48"
Data "-109.07,75.47"
Data "-110.50,76.43"
Data "7"
Data "174.61,-36.16"
Data "176.76,-37.88"
Data "178.52,-37.70"
Data "175.24,-41.69"
Data "173.82,-39.51"
Data "174.70,-37.38"
Data "172.64,-34.53"
Data "8"
Data "143.65,50.75"
Data "144.65,48.98"
Data "143.17,49.31"
Data "142.56,47.86"
Data "143.51,46.14"
Data "142.09,45.97"
Data "141.68,53.30"
Data "142.65,54.37"
Data "7"
Data "-68.45,-70.96"
Data "-68.78,-72.17"
Data "-71.08,-72.50"
Data "-75.01,-71.66"
Data "-72.07,-71.19"
Data "-71.74,-69.51"
Data "-70.25,-68.88"
Data "5"
Data "143.91,44.17"
Data "145.32,44.38"
Data "145.54,43.26"
Data "139.96,41.57"
Data "141.97,45.55"
Data "6"
Data "108.62,-6.78"
Data "110.76,-6.47"
Data "115.71,-8.37"
Data "108.28,-7.77"
Data "105.37,-6.85"
Data "106.05,-5.90"
Data "4"
Data "99.94,78.88"
Data "91.18,80.34"
Data "95.94,81.25"
Data "100.19,79.78"
Data "6"
Data "-67.75,-53.85"
Data "-65.05,-54.70"
Data "-69.23,-55.50"
Data "-74.66,-52.84"
Data "-71.11,-54.07"
Data "-69.35,-52.52"
Data "5"
Data "-6.79,52.26"
Data "-9.98,51.82"
Data "-9.69,53.88"
Data "-6.73,55.17"
Data "-5.66,54.55"
Data "6"
Data "121.32,18.50"
Data "122.52,17.09"
Data "121.73,14.33"
Data "123.95,13.78"
Data "124.08,12.54"
Data "120.07,14.97"
Data "8"
Data "-79.68,22.77"
Data "-74.18,20.28"
Data "-77.76,19.86"
Data "-77.09,20.41"
Data "-78.72,21.60"
Data "-81.80,22.64"
Data "-84.97,21.90"
Data "-82.27,23.19"
Data "3"
Data "-85.16,65.66"
Data "-80.10,63.73"
Data "-87.22,63.54"
Data "8"
Data "-100.36,73.84"
Data "-97.38,73.76"
Data "-98.05,72.99"
Data "-96.54,72.56"
Data "-96.72,71.66"
Data "-102.50,72.51"
Data "-100.44,72.71"
Data "-101.54,73.36"
Data "5"
Data "126.38,8.41"
Data "125.40,5.58"
Data "123.61,7.83"
Data "121.92,7.19"
Data "125.41,9.76"
Data "4"
Data "145.40,-40.79"
Data "148.29,-40.88"
Data "147.91,-43.21"
Data "146.05,-43.55"
Data "4"
Data "25.45,80.41"
Data "27.41,80.06"
Data "23.02,79.40"
Data "17.37,80.32"
Data "4"
Data "-98.50,76.72"
Data "-98.16,75.00"
Data "-102.50,75.56"
Data "-102.57,76.34"
Data "6"
Data "-72.58,19.87"
Data "-68.32,18.61"
Data "-73.92,18.03"
Data "-74.37,18.66"
Data "-72.33,18.67"
Data "-73.42,19.64"
Data "4"
Data "-93.20,72.77"
Data "-95.41,72.06"
Data "-96.02,73.44"
Data "-90.51,73.86"
Data "4"
Data "81.22,6.20"
Data "79.87,6.76"
Data "80.15,9.82"
Data "81.79,7.52"
Data "3"
Data "105.08,78.31"
Data "99.44,77.92"
Data "102.09,79.35"
Data "3"
Data "145.09,75.56"
Data "136.97,75.26"
Data "138.83,76.14"
Data "3"
Data "-59.57,-80.04"
Data "-60.16,-81.00"
Data "-66.29,-80.26"
Data "3"
Data "-116.20,77.65"
Data "-117.11,76.53"
Data "-122.85,76.12"
Data "3"
Data "15.52,38.23"
Data "15.10,36.62"
Data "12.43,37.61"
Data "4"
Data "9.21,41.21"
Data "9.67,39.18"
Data "8.81,38.91"
Data "8.16,40.95"
Data "4"
Data "110.34,18.68"
Data "108.66,18.51"
Data "108.63,19.37"
Data "110.79,20.08"
Data "4"
Data "124.44,-10.14"
Data "123.46,-10.24"
Data "123.98,-9.29"
Data "127.34,-8.40"
Data "4"
Data "121.18,22.79"
Data "120.75,21.97"
Data "120.11,23.56"
Data "121.50,25.30"
Data "5"
Data "123.98,10.28"
Data "123.00,9.02"
Data "122.38,9.71"
Data "122.95,10.88"
Data "124.08,11.23"
Data "4"
Data "-100.06,78.32"
Data "-105.18,78.38"
Data "-104.21,78.68"
Data "-105.49,79.30"
Data "3"
Data "-95.65,69.11"
Data "-99.80,69.40"
Data "-98.22,70.14"
Data "3"
Data "125.50,12.16"
Data "124.80,10.13"
Data "124.27,12.56"
Data "3"
Data "128.69,1.13"
Data "128.10,-0.90"
Data "127.60,1.81"
Data "3"
Data "-58.55,-51.10"
Data "-58.05,-51.90"
Data "-61.20,-51.85"
Data "4"
Data "-75.87,67.15"
Data "-77.24,67.59"
Data "-75.90,68.29"
Data "-75.10,67.58"
Data "3"
Data "130.47,-3.09"
Data "130.83,-3.86"
Data "127.90,-3.39"
Data "4"
Data "151.98,-5.48"
Data "148.32,-5.75"
Data "150.81,-5.46"
Data "151.54,-4.17"
Data "3"
Data "-132.71,54.04"
Data "-131.75,54.12"
Data "-131.18,52.18"
Data "3"
Data "70.28,-49.71"
Data "68.75,-49.77"
Data "68.94,-48.62"
Data "3"
Data "12.69,55.61"
Data "12.09,54.80"
Data "10.90,55.78"
Data "3"
Data "-153.01,57.12"
Data "-154.67,57.46"
Data "-152.56,57.90"
Data "3"
Data "-178.69,70.89"
Data "-180.00,71.52"
Data "-177.58,71.27"
End Sub
Function GetWeather(lat As Single, lon As Single) As String
Local fnum As Long
Local req As String, resp As String, buf As String, body As String
Local p As Long
Local latstr As String, lonstr As String
latstr = Format$(lat, "0.0000")
lonstr = Format$(lon, "0.0000")
req = "/" & latstr & "," & lonstr & "?format=%C,+%t+(feels+%f),+humidity+%h,+pressure+%P&m"
fnum = FreeFile
Tcp Open "http" At "wttr.in" As #fnum TimeOut 10000
If Err Then
Function = "Weather service unavailable (" & Error$(Err) & "). Check the Internet connection."
Exit Function
End If
Tcp Print #fnum, "GET " & req & " HTTP/1.0"
Tcp Print #fnum, "Host: wttr.in"
Tcp Print #fnum, "User-Agent: WorldWeather/1.0"
Tcp Print #fnum, "Accept: */*"
Tcp Print #fnum, "Connection: close"
Tcp Print #fnum, ""
Do
Tcp Recv #fnum, 4096, buf$
If Len(buf$) = 0 Then Exit Do
resp$ = resp$ & buf$
If Len(resp$) > 65536 Then Exit Do
Loop
Tcp Close #fnum
If InStr(Left$(resp$, 32), " 200 ") = 0 Then
Function = "Weather service did not return valid data."
Exit Function
End If
p = InStr(resp$, Chr$(13, 10, 13, 10))
If p > 0 Then
body$ = Mid$(resp$, p + 4)
Else
p = InStr(resp$, Chr$(10, 10))
If p > 0 Then
body$ = Mid$(resp$, p + 2)
Else
body$ = resp$
End If
End If
body$ = Trim$(body$)
If body$ = "" Then body$ = "No weather data returned."
Function = Utf8ToChr$(body$)
End Function
Sub ShowWeather(hDlg As Dword, idx As Long)
Local latdir As String, londir As String
Local coord As String, weather As String, msg As String
latdir = "N" : If gLat(idx) < 0 Then latdir = "S"
londir = "E" : If gLon(idx) < 0 Then londir = "W"
coord = Format$(Abs(gLat(idx)), "0.0000") & " " & latdir & ", " & _
Format$(Abs(gLon(idx)), "0.0000") & " " & londir
msg = gName(idx) & $CrLf & _
"Capital: " & gCap(idx) & " (" & coord & ")" & $CrLf & $CrLf & _
"Fetching current weather from wttr.in ..."
Control Set Text hDlg, %ID_TEXT, msg
Dialog ReDraw hDlg
weather = GetWeather(gLat(idx), gLon(idx))
msg = gName(idx) & $CrLf & _
"Capital: " & gCap(idx) & " (" & coord & ")" & $CrLf & $CrLf & _
"Current weather: " & weather & $CrLf & $CrLf & _
"Source: wttr.in - live current conditions." & $CrLf & _
"Map: red outline = approximate country bounds; red circle = capital."
Control Set Text hDlg, %ID_TEXT, msg
Dialog ReDraw hDlg
End Sub
CallBack Function DlgProc() As Long
Local idx As Long
Select Case Cb.Msg
Case %WM_Command
If Cb.Ctl = %ID_LIST And Cb.CtlMsg = %LBN_SelChange Then
ListBox Get Select Cb.Hndl, %ID_LIST To idx
If idx >= 1 And idx <= gN Then
DrawMap Cb.Hndl, idx
ShowWeather Cb.Hndl, idx
End If
End If
End Select
Function = 0
End Function
Function PBMain() As Long
Local hDlg As Dword
Local i As Long
Dim gName(1 To %MAX_COUNTRIES) As String
Dim gCap(1 To %MAX_COUNTRIES) As String
Dim gLat(1 To %MAX_COUNTRIES) As Single
Dim gLon(1 To %MAX_COUNTRIES) As Single
Dim gMinLat(1 To %MAX_COUNTRIES) As Single
Dim gMaxLat(1 To %MAX_COUNTRIES) As Single
Dim gMinLon(1 To %MAX_COUNTRIES) As Single
Dim gMaxLon(1 To %MAX_COUNTRIES) As Single
Dialog Default Font "Segoe UI", 9
If LoadCountries() = 0 Then
MsgBox "Countries.txt was not found next to the program.", %MB_IconError, "World Weather"
Exit Function
End If
Dialog New Pixels, 0, "World Weather - Countries and Current Conditions", , , 1000, 700, _
%WS_Caption Or %WS_SysMenu Or %WS_MinimizeBox To hDlg
Control Add ListBox, hDlg, %ID_LIST, , 10, 10, 250, 660, _
%WS_VScroll Or %LBS_Notify Or %WS_TabStop
For i = 1 To gN
ListBox Add hDlg, %ID_LIST, gName(i)
Next i
Control Add Graphic, hDlg, %ID_MAP, "", 270, 10, 720, 360
Control Add TextBox, hDlg, %ID_TEXT, "", 270, 380, 720, 310, _
%ES_MultiLine Or %ES_AutoVScroll Or %WS_VScroll Or %ES_ReadOnly Or %WS_TabStop
Control Set Color hDlg, %ID_LIST, RGB(20, 30, 50), RGB(250, 250, 245)
Control Set Color hDlg, %ID_TEXT, RGB(20, 30, 50), RGB(250, 250, 245)
Control Set Text hDlg, %ID_TEXT, "Select a country from the list." & $CrLf & $CrLf & _
"The world map marks the country and its capital. Current weather for the capital is fetched from wttr.in."
DrawMap hDlg, 0
Dialog Show Modal hDlg, Call DlgProc
Function = 0
End Function
Countries.txt
Code: Afghanistan Kabul 34.518636 69.181314 29.3186 38.4863 60.5284 75.1580 AF
Albania Tirana 41.327541 19.818883 39.6250 42.6882 19.3045 21.0200 AL
Algeria Algiers 36.765011 3.048607 19.0574 37.1184 -8.6844 11.9995 DZ
Andorra Andorra 42.500001 1.516486 41.5000 43.5000 0.5165 2.5165 AD
Angola Luanda -8.836340 13.232481 -17.9306 -5.8642 11.6401 24.0799 AO
Antigua and Barbuda Saint John's 17.118037 -61.850034 16.1180 18.1180 -62.8500 -60.8500 AG
Argentina Buenos Aires -34.600556 -58.399477 -52.3500 -21.8323 -73.4154 -53.6283 AR
Armenia Yerevan 40.183097 44.511606 38.7412 41.2481 43.5827 46.5057 AM
Australia Canberra -35.283029 149.129026 -39.0358 -10.6682 113.3390 153.5695 AU
Austria Vienna 48.201961 16.364693 46.4318 49.0391 9.4800 16.9797 AT
Azerbaijan Baku 40.397218 49.860271 38.2704 41.8607 44.9725 50.3928 AZ
Bahamas Nassau 25.083390 -77.350044 23.7100 25.2103 -78.4085 -77.5347 BS
Bahrain Manama 26.236136 50.583052 25.2361 27.2361 49.5831 51.5831 BH
Bangladesh Dhaka 23.725006 90.406634 20.6709 26.4465 88.0844 92.6727 BD
Barbados Bridgetown 13.102003 -59.616527 12.1020 14.1020 -60.6165 -58.6165 BB
Belarus Minsk 53.901923 27.564681 51.3195 56.1691 23.1995 32.6936 BY
Belgium Brussels 50.835263 4.331371 49.5295 51.4750 2.5136 6.1567 BE
Belize Belmopan 17.252034 -88.767073 15.8869 18.5000 -89.2291 -88.1068 BZ
Benin Cotonou 6.401954 2.518045 6.1422 12.2356 0.7723 3.7971 BJ
Bhutan Thimphu 27.472986 89.639014 26.7194 28.2964 88.8142 92.1037 BT
Bolivia Sucre -19.040971 -65.259516 -22.8729 -9.7620 -69.5904 -57.4984 BO
Bosnia and Herzegovina Sarajevo 43.850022 18.383002 42.6500 45.2338 15.7500 19.5998 BA
Botswana Gaborone -24.646314 25.911948 -26.8285 -17.6618 19.8955 29.4322 BW
Brazil Brasilia -15.781394 -47.917998 -33.7684 5.2445 -73.9872 -34.7300 BR
Brunei Bandar Seri Begawan 4.883331 114.933284 4.0076 5.4477 114.2040 115.4507 BN
Bulgaria Sofia 42.685295 23.314708 41.2345 44.2349 22.3805 28.5581 BG
Burkina Faso Ouagadougou 12.372262 -1.526670 9.6108 15.1162 -5.4706 2.1771 BF
Burundi Bujumbura -3.376087 29.360006 -4.5000 -2.3485 29.0249 30.7522 BI
Cambodia Phnom Penh 11.551976 104.914689 10.4865 14.5706 102.3481 107.6145 KH
Cameroon Yaounde 3.868647 11.514705 1.7277 12.8594 8.4888 16.0129 CM
Canada Ottawa 45.418643 -75.701961 41.6751 71.9205 -140.9978 -55.6834 CA
Cape Verde Praia 14.916698 -23.516689 13.9167 15.9167 -24.5167 -22.5167 CV
Central African Republic Bangui 4.366644 18.558288 2.2676 11.1424 14.4594 27.3742 CF
Chad Ndjamena 12.115042 15.047203 7.4219 23.4097 13.5404 23.8869 TD
Chile Santiago -33.448068 -70.668987 -53.8565 -17.5800 -75.6444 -66.9852 CL
China Beijing 39.930838 116.386340 20.2825 53.4588 73.6754 135.0263 CN
Colombia Bogota 4.598369 -74.085290 -4.2982 12.4373 -78.9909 -66.8763 CO
Comoros Moroni -11.704158 43.240244 -12.7042 -10.7042 42.2402 44.2402 KM
Congo Brazzaville -4.257240 15.282744 -5.0380 3.7282 11.0938 18.4531 CG
Costa Rica San Jose 9.936958 -84.085997 8.2250 11.2171 -85.9417 -82.5462 CR
Croatia Zagreb 45.800007 15.999995 42.4800 46.5038 13.6570 19.3905 HR
Cuba Havana 23.133905 -82.366128 19.8555 23.1886 -84.9749 -74.1780 CU
Cyprus Nicosia 35.166677 33.366635 34.5719 35.1731 32.2567 34.0049 CY
Czechia Prague 50.085283 14.464034 48.5553 51.1173 12.2401 18.8531 CZ
Denmark Kobenhavn 55.680510 12.561540 54.8309 57.7300 8.0900 10.9122 DK
Djibouti Djibouti 11.595015 43.148002 10.9269 12.6996 41.6618 43.3179 DJ
Dominica Roseau 15.301016 -61.387013 14.3010 16.3010 -62.3870 -60.3870 DM
Dominican Republic Santo Domingo 18.472019 -69.902031 17.5986 19.8849 -71.9451 -68.3179 DO
DR Congo Kinshasa -4.327778 15.313026 -13.2572 5.2561 12.1823 31.1741 CD
Ecuador Quito -0.213042 -78.501997 -4.9591 1.3809 -80.9678 -75.2337 EC
Egypt Cairo 30.051906 31.248022 22.0000 31.5857 24.7001 36.8662 EG
El Salvador San Salvador 13.711948 -89.204987 13.1490 14.4241 -90.0956 -87.7235 SV
Equatorial Guinea Malabo 3.750015 8.783278 1.0101 2.2839 9.3056 11.2851 GQ
Eritrea Asmara 15.333339 38.933324 12.4554 17.9983 36.3232 43.0812 ER
Estonia Tallinn 59.433877 24.728041 57.4745 59.6111 23.3398 28.1317 EE
Eswatini Mbabane -26.316651 31.133335 -27.2859 -25.6602 30.6766 32.0717 SZ
Ethiopia Addis Ababa 9.035256 38.698059 3.4221 14.9594 32.9542 47.7894 ET
Fiji Suva -18.133016 178.441707 -18.2880 -17.3399 177.2850 178.7181 FJ
Finland Helsinki 60.177509 24.932181 59.8464 70.1642 20.6456 31.5161 FI
France Paris 48.868639 2.331390 42.3434 51.1485 -4.5923 8.0993 FR
Gabon Libreville 0.385389 9.457965 -3.9788 2.3268 8.7980 14.4255 GA
Gambia Banjul 13.453877 -16.591702 13.1303 13.8765 -16.8415 -13.8450 GM
Georgia Tbilisi 41.726956 44.788850 41.0644 43.5531 39.9550 46.6379 GE
Germany Berlin 52.523765 13.399603 47.3025 54.9831 5.9887 15.0170 DE
Ghana Accra 5.551981 -0.218662 4.7105 11.0983 -3.2444 1.0601 GH
Greece Athens 37.985272 23.731375 36.4100 41.8269 20.1500 26.6042 GR
Grenada Saint George's 12.052633 -61.741643 11.0526 13.0526 -62.7416 -60.7416 GD
Guatemala Guatemala 14.623081 -90.528911 13.7353 17.8193 -92.2292 -88.2250 GT
Guinea Conakry 9.533469 -13.682181 7.3090 12.5862 -15.1303 -7.8321 GN
Guinea-Bissau Bissau 11.865024 -15.598361 11.0404 12.6282 -16.6775 -13.7005 GW
Guyana Georgetown 6.801974 -58.167029 1.2681 8.3670 -61.4103 -56.5394 GY
Haiti Port-au-Prince 18.542971 -72.337980 18.0310 19.9157 -74.4580 -71.6249 HT
Honduras Tegucigalpa 14.103991 -87.219475 12.9847 16.0054 -89.3533 -83.1472 HN
Hungary Budapest 47.501952 19.081375 45.7595 48.6239 16.2023 22.7105 HU
Iceland Reykjavik 64.150024 -21.950015 63.4964 66.5268 -24.3262 -13.6097 IS
India New Delhi 28.600023 77.199980 7.9655 35.4940 68.1766 97.4026 IN
Indonesia Jakarta -6.172472 106.827492 -4.1070 4.3066 108.9527 118.9967 ID
Iran Tehran 35.673889 51.422398 25.0782 39.7130 44.1092 63.3166 IR
Iraq Baghdad 33.340594 44.391923 29.0990 37.3853 38.7923 48.5680 IQ
Ireland Dublin 53.335007 -6.250852 51.6693 55.1316 -9.9771 -6.0330 IE
Israel Jerusalem 31.778408 35.206626 29.5013 33.2774 34.2654 35.8364 IL
Italy Rome 41.897902 12.481313 37.9088 47.1154 6.7500 18.4802 IT
Ivory Coast Yamoussoukro 6.818381 -5.275503 4.3383 10.5241 -8.6029 -2.5622 CI
Jamaica Kingston 17.977077 -76.767434 17.7011 18.5242 -78.3377 -76.1997 JM
Japan Tokyo 35.686963 139.749462 31.0296 41.3786 129.4085 141.9143 JP
Jordan Amman 31.951971 35.931354 29.1975 33.3787 34.9226 39.1955 JO
Kazakhstan Astana 51.181125 71.427774 40.6623 55.3852 46.4664 87.3600 KZ
Kenya Nairobi -1.281401 36.814711 -4.6768 5.5060 33.8936 41.8551 KE
Kiribati Tarawa 1.338188 173.017571 0.3382 2.3382 172.0176 174.0176 KI
Kuwait Kuwait City 29.371664 47.976355 28.5261 30.0591 46.5687 48.4161 KW
Kyrgyzstan Bishkek 42.875025 74.583258 39.2795 43.2983 69.4649 80.2600 KG
Laos Vientiane 17.966693 102.599980 13.8811 22.4648 100.1160 107.5645 LA
Latvia Riga 56.950024 24.099965 55.6151 57.9702 21.0558 28.1767 LV
Lebanon Beirut 33.873921 35.507762 33.0890 34.6449 35.1261 36.6118 LB
Lesotho Maseru -29.316674 27.483273 -30.6451 -28.6475 26.9993 29.3252 LS
Liberia Monrovia 6.314582 -10.799660 4.3558 8.5411 -11.4388 -7.5397 LR
Libya Tripoli 32.892500 13.180012 19.5805 33.1370 9.3194 25.1648 LY
Liechtenstein Vaduz 47.133724 9.516670 46.1337 48.1337 8.5167 10.5167 LI
Lithuania Vilnius 54.683366 25.316635 53.9057 56.3725 21.0558 26.5883 LT
Luxembourg Luxembourg 49.611660 6.130003 49.4427 50.1281 5.6741 6.2428 LU
Madagascar Antananarivo -18.914692 47.514678 -25.6014 -12.0406 43.2542 50.4765 MG
Malawi Lilongwe -13.983295 33.783302 -16.8013 -9.2306 32.6882 35.7719 MW
Malaysia Kuala Lumpur 3.168612 101.698037 0.7731 6.9281 109.6633 119.1819 MY
Maldives Male 4.166708 73.499948 3.1667 5.1667 72.4999 74.4999 MV
Mali Bamako 12.651961 -8.001985 10.0964 24.9746 -12.1707 4.2702 ML
Malta Valletta 35.899733 14.514711 34.8997 36.8997 13.5147 15.5147 MT
Marshall Islands Majuro 7.103004 171.380000 6.1030 8.1030 170.3800 172.3800 MH
Mauritania Nouakchott 18.086427 -15.975340 14.6168 27.3957 -17.0634 -4.9233 MR
Mauritius Port Louis -20.166639 57.499994 -21.1666 -19.1666 56.5000 58.5000 MU
Mexico Mexico City 19.444388 -99.132934 14.5388 32.7208 -117.1278 -86.8120 MX
Micronesia Palikir 6.916644 158.149974 5.9166 7.9166 157.1500 159.1500 FM
Moldova Chisinau 47.005024 28.857711 45.4883 48.4671 26.6193 30.0247 MD
Monaco Monaco 43.739646 7.406913 42.7396 44.7396 6.4069 8.4069 MC
Mongolia Ulaanbaatar 47.918619 106.914670 41.5974 52.0474 87.7513 119.7728 MN
Montenegro Podgorica 42.465973 19.266307 41.8776 43.5238 18.4500 20.3398 ME
Morocco Rabat 34.025307 -6.836408 21.4207 35.7600 -17.0204 -1.1246 MA
Mozambique Maputo -25.953332 32.587217 -26.7422 -10.3171 30.1795 40.7755 MZ
Myanmar Naypyidaw 19.768503 96.116673 9.9330 28.3359 92.3032 101.1800 MM
Namibia Windhoek -22.570006 17.083546 -29.0455 -16.9413 11.7342 25.0844 NA
Nauru Yaren -0.533333 166.916667 -1.5333 0.4667 165.9167 167.9167 NR
Nepal Kathmandu 27.718638 85.314696 26.3979 30.4227 80.0884 88.1748 NP
Netherlands Amsterdam 52.351915 4.914694 50.8037 53.5104 3.3150 7.0921 NL
New Zealand Wellington -41.299988 174.783266 -46.6412 -40.4940 166.5091 174.2485 NZ
Nicaragua Managua 12.154962 -86.270438 10.7268 15.0163 -87.6685 -83.1472 NI
Niger Niamey 13.518652 2.114710 11.6602 23.4717 0.2956 15.9032 NE
Nigeria Abuja 9.085279 7.531382 4.2406 13.8659 2.6917 14.5772 NG
North Korea Pyongyang 39.021385 125.752745 37.6691 42.9854 124.2656 130.7800 KP
North Macedonia Skopje 42.000006 21.433462 40.8427 42.3203 20.4632 22.9524 MK
Norway Oslo 59.918636 10.748033 58.0789 71.1855 4.9921 31.2934 NO
Oman Muscat 23.613325 58.593312 16.6511 24.9247 52.0000 59.8081 OM
Pakistan Islamabad 33.701942 73.164689 23.6920 37.1330 60.8742 77.8375 PK
Palau Melekeok 7.487396 134.626549 6.4874 8.4874 133.6265 135.6265 PW
Palestine Ramallah 31.899600 35.204200 31.3534 32.5325 34.9274 35.5457 PS
Panama Panama City 8.969963 -79.534983 7.2205 9.6116 -82.9658 -77.2426 PA
Papua New Guinea Port Moresby -9.464708 147.192504 -10.6525 -2.6002 141.0002 150.8016 PG
Paraguay Asuncion -25.294457 -57.643451 -27.5485 -19.3427 -62.6851 -54.2930 PY
Peru Lima -12.046067 -77.052008 -18.3480 -0.0572 -81.4109 -68.6651 PE
Philippines Manila 14.606105 120.980271 12.5367 18.5052 119.8838 124.1813 PH
Poland Warsaw 52.251947 20.998054 49.0274 54.8515 14.0745 24.0300 PL
Portugal Lisbon 38.724669 -9.146812 36.8383 42.2805 -9.5266 -6.3891 PT
Qatar Doha 25.286556 51.532968 24.5563 26.1146 50.7439 51.6067 QA
Romania Bucharest 44.435318 26.098001 43.6884 48.2209 20.2202 29.6265 RO
Russia Moscow 55.754110 37.613577 41.1514 77.6979 27.2882 180.0000 RU
Rwanda Kigali -1.951644 30.058586 -2.9179 -1.1347 29.0249 30.8161 RW
Saint Kitts and Nevis Basseterre 17.302031 -62.717009 16.3020 18.3020 -63.7170 -61.7170 KN
Saint Lucia Castries 14.001974 -61.000008 13.0020 15.0020 -62.0000 -60.0000 LC
Saint Vincent and the Grenadines Kingstown 13.148279 -61.212062 12.1483 14.1483 -62.2121 -60.2121 VC
Samoa Apia -13.841545 -171.738642 -14.8415 -12.8415 -172.7386 -170.7386 WS
San Marino San Marino 43.936096 12.441770 42.9361 44.9361 11.4418 13.4418 SM
Sao Tome and Principe Sao Tome 0.333402 6.733325 -0.6666 1.3334 5.7333 7.7333 ST
Saudi Arabia Riyadh 24.642779 46.770796 16.3479 32.1610 34.6323 55.6667 SA
Senegal Dakar 14.717778 -17.475076 12.3321 16.5983 -17.6250 -11.4679 SN
Serbia Belgrade 44.820591 20.466045 42.2452 46.1717 18.8298 22.9860 RS
Seychelles Victoria -4.616632 55.449990 -5.6166 -3.6166 54.4500 56.4500 SC
Sierra Leone Freetown 8.471957 -13.236162 6.7859 10.0470 -13.2465 -10.2301 SL
Singapore Singapore 1.294979 103.853875 0.2950 2.2950 102.8539 104.8539 SG
Slovakia Bratislava 48.150018 17.116981 47.7584 49.5716 16.8800 22.5581 SK
Slovenia Ljubljana 46.055288 14.514969 45.4523 46.8524 13.6981 16.5648 SI
Solomon Islands Honiara -9.437994 159.949766 -8.5383 -7.3200 158.2112 159.9174 SB
Somalia Mogadishu 2.068627 45.364732 -1.6832 12.0246 40.9811 51.1339 SO
South Africa Bloemfontein -29.119994 26.229913 -34.8192 -22.0913 16.3450 32.8301 ZA
South Korea Seoul 37.568295 126.997785 34.3900 38.6122 126.1174 129.4683 KR
South Sudan Juba 7.000000 30.000000 3.5092 12.2480 23.8870 35.2980 SS
Spain Madrid 40.401972 -3.685298 35.9468 43.7483 -9.3929 3.0395 ES
Sri Lanka Colombo 6.931966 79.857751 5.9684 9.8241 79.6952 81.7880 LK
Sudan Khartoum 15.590024 32.532233 8.2292 22.0000 21.9368 38.4101 SD
Suriname Paramaribo 5.835030 -55.167031 1.8177 6.0253 -58.0447 -53.9580 SR
Sweden Stockholm 59.352706 18.095389 55.3617 69.1062 11.0274 23.9034 SE
Switzerland Bern 46.916683 7.466976 45.7769 47.8308 6.0226 10.4427 CH
Syria Damascus 33.501980 36.298050 32.3129 37.2299 35.7008 42.3496 SY
Tajikistan Dushanbe 38.560035 68.773879 36.7382 40.9602 67.4422 74.9800 TJ
Tanzania Dar es Salaam -6.798067 39.266396 -11.7209 -0.9500 29.3400 40.3166 TZ
Thailand Bangkok 13.751945 100.514699 5.6914 20.4179 97.3759 105.5890 TH
Timor-Leste Dili -8.559388 125.579456 -9.3932 -8.2733 124.9687 127.3359 TL
Togo Lome 6.133883 1.220811 5.9288 11.0187 -0.0498 1.8652 TG
Tonga Nukualofa -21.138512 -175.220565 -22.1385 -20.1385 -176.2206 -174.2206 TO
Trinidad and Tobago Port-of-Spain 10.651997 -61.517031 10.0000 10.8900 -61.9500 -60.8950 TT
Tunisia Tunis 36.802778 10.179678 30.3076 37.3500 7.5245 11.4888 TN
Turkiye Ankara 39.929184 32.862446 35.8215 42.0402 26.1708 44.7940 TR
Turkmenistan Ashgabat 37.949995 58.383299 35.2707 42.7516 52.5025 66.5461 TM
Tuvalu Funafuti -8.516652 179.216647 -9.5167 -7.5167 178.2166 180.2166 TV
Uganda Kampala 0.318605 32.581378 -1.4433 4.2499 29.5795 35.0360 UG
Ukraine Kiev 50.435313 30.514682 45.2933 52.3351 22.0856 40.0808 UA
United Arab Emirates Abu Dhabi 24.466684 54.366593 22.4969 26.0555 51.5795 56.3968 AE
United Kingdom London 51.501941 -0.118668 49.9600 58.6350 -6.1500 1.6815 GB
United States Washington, D.C. 38.901495 -77.011364 25.0800 49.3890 -124.6872 -66.9647 US
Uruguay Montevideo -34.856096 -56.172998 -34.9526 -30.1097 -58.4271 -53.2096 UY
Uzbekistan Tashkent 41.313648 69.292987 37.1450 45.5868 55.9289 73.0554 UZ
Vanuatu Port Vila -17.733350 168.316641 -15.7400 -14.6265 166.6291 167.2700 VU
Vatican City Vatican City 41.903282 12.453387 40.9033 42.9033 11.4534 13.4534 VA
Venezuela Caracas 10.502944 -66.918983 0.7245 12.1623 -73.3050 -59.7583 VE
Vietnam Hanoi 21.035273 105.848068 8.5998 23.3521 102.1704 109.3353 VN
Yemen Sanaa 15.356679 44.204648 12.5860 19.0000 42.6049 53.1086 YE
Zambia Lusaka -15.414698 28.281382 -17.9612 -8.2383 21.8878 33.4857 ZM
Zimbabwe Harare -17.815844 31.042764 -22.2716 -15.5078 25.2642 32.8499 ZW
|
|
|
| WinTick 1.1 |
|
Posted by: Borje Hagsten - Yesterday, 00:33 - Forum: Source Code Library
- No Replies
|
 |
![[Image: o9LlJN1.jpg]](https://iili.io/o9LlJN1.jpg)
Code: '====================================================================
' WinTick.bas, a timer driven Window information grabber.
' Original code published in February 2003
' Public Domain by Borje Hagsten, November 2026
'
' WinTick looks at window under cursor with 200 ms intervals, grabs and
' shows hWnd, id and class name, size, position and unicode status.
' Now enhanced with drawing a rectangle round pointed widow (DrawRect)
'
' A note: It fails on disabled windows. Ms says ChildWindowFromPoint
' should work with disabled windows, but I couldn't get it to work so
' I decided it was not worth spending more time on, so skipped that.
'
' Why WinTick? Because I sometimes need window info for whatever reason.
' Why a version update after 23 years? Because it is fun to do..
'====================================================================
#COMPILE EXE
%UNICODE = 1
#INCLUDE "WIN32API.INC"
'--------------------------------------------------------------------
' For old/new dialog design - thank you Jules Marchildon for this tip!
DECLARE FUNCTION DwmSetWindowAttribute LIB "dwmapi.dll" ALIAS "DwmSetWindowAttribute" _
(BYVAL hWnd AS DWORD,BYVAL dwAttribute AS DWORD, _
BYREF pvAttribute AS DWORD, BYVAL cbAttribute AS DWORD) AS LONG
%DWMWA_NCRENDERING_POLICY = 2 '-Set, who should draw the frame
%DWMNCRP_DISABLED = 1 '-DWM does not draw the frame gives the legacy look
%DWMNCRP_ENABLED = 2 '-DWM draws the modern frame
'--------------------------------------------------------------------
%IDC_TEXBOX1 = 501
GLOBAL ghDlg AS LONG
'====================================================================
' Main entrance
'--------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
LOCAL hDlg AS DWORD
'------------------------------------------------------------------
SetProcessDPIAware ' I do this, because I use 150% screen scaling
'------------------------------------------------------------------
DIALOG FONT "Tahoma", 9
DIALOG NEW 0, "WinTick v1.1",,, 180, 90, %WS_CAPTION OR %WS_SYSMENU OR _
%WS_MINIMIZEBOX, 0 TO ghDlg
DIALOG SET COLOR ghDlg, -1, RGB(196, 196, 196)
'------------------------------------------------------------------
' I like the old windows design better
DwmSetWindowAttribute ghDlg, %DWMWA_NCRENDERING_POLICY, %DWMNCRP_DISABLED, 4
'------------------------------------------------------------------
CONTROL ADD LABEL, ghDlg, %IDOK, "Click here and drag to target", 1, 1, 60, 20, %SS_CENTER OR %WS_DLGFRAME OR %SS_NOTIFY
CONTROL SET COLOR ghDlg, %IDOK, -1, RGB(10, 175, 175)
CONTROL ADD TEXTBOX, ghDlg, %IDC_TEXBOX1, "", 5, 25, 169, 60, _
%ES_MULTILINE, %WS_EX_CLIENTEDGE
'------------------------------------------------------------------
DIALOG SHOW MODELESS ghDlg, CALL DlgProc
DO
DIALOG DOEVENTS
LOOP WHILE ISWIN(ghDlg)
END FUNCTION
'====================================================================
' Main dialog's callback procedure
'--------------------------------------------------------------------
CALLBACK FUNCTION DlgProc() AS LONG
LOCAL hWnd, cId AS LONG, rc, rc2 AS RECT, _
wStr AS WSTRING, wzStr AS WSTRINGZ * %MAX_PATH, _
pt AS POINTAPI
STATIC hTimer, iDraging, sthWnd AS LONG
SELECT CASE CBMSG
CASE %WM_INITDIALOG 'set topmost zorder on screen from start
SetWindowPos CB.HNDL, %HWND_TOPMOST, 0, 0, 0, 0, _
%SWP_NOSIZE OR %SWP_NOMOVE
CASE %WM_TIMER
IF iDraging AND _ ' if left mouse button is released during action
(GetAsyncKeyState(%VK_LBUTTON) AND &H8000) = 0 THEN
KillTimer CB.HNDL, hTimer : hTimer = 0 ' clean up, refresh and exit
ReleaseCapture
MOUSEPTR 1 : DrawRect 0 : iDraging = 0 : sthWnd = 0
CONTROL REDRAW CB.HNDL, %IDC_TEXBOX1
EXIT FUNCTION
END IF
GetCursorPos pt 'grab cursor pos on screen
hWnd = WindowFromPoint(pt) 'and look what window is under cursor
IF hWnd AND hWnd <> sthWnd THEN 'if we got a handle
sthWnd = hWnd 'store it in a static variable
DrawRect hWnd 'draw a rectangle around window under cursor
'---------------------------------------------------------
'collect and show some useful data abour the window under cursor
GetClassName hWnd, wzStr, SIZEOF(wzStr) 'grab class name
'z-order trick (fixes problem of not seeing inside FRAMES/TABs etc)
IF UCASE$(wzStr) = "SYSTABCONTROL32" OR _
(UCASE$(wzStr) = "BUTTON" AND (GetWindowLong(hWnd, %GWL_STYLE) AND %BS_GROUPBOX) = %BS_GROUPBOX) THEN
SetWindowPos hWnd, %HWND_BOTTOM, 0, 0, 0, 0, %SWP_NOSIZE OR %SWP_NOMOVE
hWnd = WindowFromPoint(Pt)
END IF
'---------------------------------------------------------
cId = GetDlgCtrlID(hWnd) 'try to get id
GetWindowRect hWnd, rc2 'get coordiantes on screen
GetClientRect hWnd, rc 'get client size
wStr = "Class:" + $TAB + " " + wzStr + $CRLF + _
"hWnd:" + $TAB + STR$(hWnd) + " (&H" + HEX$(hWnd) + ")" + $CRLF + _ ' build string
"Id:" + $TAB + STR$(cId) + $CRLF + _
"Rect:" + $TAB + "(" + FORMAT$(rc2.nLeft) + ", " + FORMAT$(rc2.nTop) + ")-(" + _
FORMAT$(rc2.Right) + ", " + FORMAT$(rc2.nBottom) + ") " + $CRLF + _
"Client:" + $TAB + FORMAT$(rc.Right) + "x" + FORMAT$(rc.nBottom) + $CRLF + _
"Unicode:" + $TAB + FORMAT$(IsWindowUnicode(hWnd)) + $CRLF 'is it unicode?
CONTROL SET TEXT CB.HNDL, %IDC_TEXBOX1, wStr
'---------------------------------------------------------
END IF
CASE %WM_DESTROY ' in case a timer is running at exit..
IF hTimer THEN KillTimer CB.HNDL, hTimer
CASE %WM_COMMAND
SELECT CASE CB.CTL
CASE %IDOK 'start timer action
IF CB.CTLMSG = %STN_CLICKED THEN
SetCapture CB.HNDL 'set capture to parent dialog
iDraging = 1 'set static flag
MOUSEPTR 2 'good enough mouse ptr
hTimer = SetTimer(CB.HNDL, 1, 200, 0) 'start timer
END IF
CASE %IDCANCEL 'also responds to Esc key...
IF CB.CTLMSG = %BN_CLICKED THEN
DIALOG END CB.HNDL
END IF
END SELECT
END SELECT
END FUNCTION
'====================================================================
' Draw a rectangle on screen around given window
'--------------------------------------------------------------------
SUB DrawRect(hWnd AS DWORD)
LOCAL hDC, hBrush, hPen AS DWORD, rc AS RECT
STATIC memDC, hBit AS DWORD
hDC = GetWindowDC(GetDesktopWindow) 'get desktop DC
GetWindowRect GetDesktopWindow, rc 'get desktop size
IF memDC = 0 THEN 'if not yet done, grab and store a fresh copy of the desktop
CONTROL SET TEXT ghDlg, %IDC_TEXBOX1, "" 'clear Textbox
memDC = CreateCompatibleDC(hDC) 'create a memory DC
hbit = CreateCompatibleBitmap(hDC, rc.nRight, rc.nBottom)
hBit = SelectObject (memDC, hBit)
FillRect memDC, rc, GetStockObject(%WHITE_BRUSH)
BitBlt memDC, 0, 0, rc.nRight, rc.nBottom, hDC, 0, 0, %SRCCOPY 'clean copy of screen to memDC
ELSE
RedrawWindow GetDesktopWindow, BYVAL %NULL, 0, %RDW_FRAME OR %RDW_ERASE OR %RDW_INVALIDATE OR %RDW_UPDATENOW
END IF
BitBlt hDC, 0, 0, rc.nRight, rc.nBottom, memDC, 0, 0, %SRCCOPY 'copy memDC to screen (wipe out previous rect)
hPen = CreatePen(%PS_SOLID, 4, %RED) 'create a pen for given color (here red)
hPen = SelectObject(hDC, hPen) 'select the new pen into the DC
hBrush = SelectObject(hDC, GetStockObject(%NULL_BRUSH)) 'use stock null brush for hollow rect
IF hWnd THEN 'draw a rectangle around given window
GetWindowRect hWnd, rc 'get coordinates on screen
Rectangle hDC, rc.nLeft, rc.nTop, rc.nRight, rc.nBottom 'draw the rectangle
ELSE
DeleteObject SelectObject(memDC, hBit) 'if no hWnd, delete what we created
DeleteDC memDC 'to avoid GDI memory leaks
memDC = 0
END IF
SelectObject hDC, hBrush 'return original brush, delete create pen and release DC
DeleteObject SelectObject(hDC, hPen)
ReleaseDC(GetDesktopWindow, hDC)
END SUB
|
|
|
| RSA2048 encryption |
|
Posted by: Mannish Bhandari - 13.11.2026, 20:29 - Forum: Programming
- Replies (4)
|
 |
https://pump.richheimer.de/showthread.ph...15#pid1115
is reference
Thank you Stephen
I converted it to PBWin10 as below :
Pros : Good that it does not contain Windows crypto API
Cons : Quite slow -- it takes 30 secs to encrypt and decrypt a simple msg = "Test this 123456 789*)($#@!"
and its second run it takes 56 secs !
Need more work to hasten this prog
Code: ' ==============================================================================
' RSA2048.bas - RSA-2048 in pure PowerBasic (no Windows Crypto API)
'
' https://pump.richheimer.de/showthread.php?tid=150&pid=1115#pid1115
' - Big numbers: arrays of QUAD, each element holds one 16-bit limb
' (little-endian: element 0 = least significant 16 bits).
' 16-bit limbs keep every intermediate product well inside a signed QUAD.
' - Modular exponentiation: Montgomery multiplication (no big division needed)
' - Key generation: random 1024-bit primes, trial division + Miller-Rabin
' - Private exponent: derived without big division (e is small, see below)
' - Padding: PKCS#1 v1.5 (type 2) for text messages, max 245 bytes
'
' Written as a PBCC console program. For PBWin, replace PRINT/WAITKEY$ with
' MSGBOX or file output.
'
' WARNING - educational code:
' * RND is NOT a cryptographically secure random source. For real keys use
' an OS entropy source.
' * Not constant-time (timing side channels), no CRT, no blinding.
' * PKCS#1 v1.5 padding is legacy; OAEP would need a hash function.
'==============================================================================
#COMPILE EXE
#DIM ALL
#INCLUDE "Win32Api.Inc" ' only for QueryPerformanceCounter (seeding)
%LIMBS_MAX = 136 ' 128 limbs = 2048 bits, plus guard limbs
%KEY_LIMBS = 128 ' 2048 bits
%HALF_LIMBS = 64 ' 1024 bits (p and q)
%E_PUBLIC = 65537
%MR_ROUNDS = 10 ' Miller-Rabin rounds (bases = first primes)
GLOBAL g_L AS LONG ' limbs in current modulus
GLOBAL g_Ninv AS QUAD ' -n^-1 mod 2^16
GLOBAL g_N() AS QUAD ' current modulus
GLOBAL g_R2() AS QUAD ' R^2 mod n, R = 2^(16*L)
GLOBAL g_T() AS QUAD ' scratch for MontMul
GLOBAL g_SP() AS LONG ' small primes
GLOBAL g_SPCount AS LONG
'------------------------------------------------------------------------------
' Basic big-number helpers
'------------------------------------------------------------------------------
SUB BnClear(a() AS QUAD)
LOCAL i AS LONG
FOR i = 0 TO %LIMBS_MAX - 1
a(i) = 0
NEXT
END SUB
SUB BnCopy(src() AS QUAD, dst() AS QUAD)
LOCAL i AS LONG
FOR i = 0 TO %LIMBS_MAX - 1
dst(i) = src(i)
NEXT
END SUB
' compare over n limbs: -1, 0, 1
FUNCTION BnCmp(a() AS QUAD, b() AS QUAD, BYVAL n AS LONG) AS LONG
LOCAL i AS LONG
FOR i = n - 1 TO 0 STEP -1
IF a(i) > b(i) THEN FUNCTION = 1 : EXIT FUNCTION
IF a(i) < b(i) THEN FUNCTION = -1 : EXIT FUNCTION
NEXT
FUNCTION = 0
END FUNCTION
' a = a - b over n limbs (a >= b assumed, or the borrow is dropped)
SUB BnSubInPlace(a() AS QUAD, b() AS QUAD, BYVAL n AS LONG)
LOCAL i AS LONG
LOCAL d AS QUAD, borrow AS QUAD
borrow = 0
FOR i = 0 TO n - 1
d = a(i) - b(i) - borrow
IF d < 0 THEN
d = d + 65536 : borrow = 1
ELSE
borrow = 0
END IF
a(i) = d
NEXT
END SUB
' a = a >> 1 over n limbs
SUB BnShr1(a() AS QUAD, BYVAL n AS LONG)
LOCAL i AS LONG
LOCAL t AS QUAD, carry AS QUAD
carry = 0
FOR i = n - 1 TO 0 STEP -1
t = a(i)
a(i) = (t + carry * 65536) \ 2
carry = t AND 1
NEXT
END SUB
FUNCTION BnBitLength(a() AS QUAD, BYVAL n AS LONG) AS LONG
LOCAL i AS LONG, b AS LONG
LOCAL v AS QUAD
FOR i = n - 1 TO 0 STEP -1
IF a(i) <> 0 THEN
v = a(i) : b = 0
DO WHILE v > 0
INCR b
v = v \ 2
LOOP
FUNCTION = i * 16 + b
EXIT FUNCTION
END IF
NEXT
FUNCTION = 0
END FUNCTION
FUNCTION BnGetBit(a() AS QUAD, BYVAL bitNo AS LONG) AS LONG
LOCAL k AS LONG
LOCAL v AS QUAD
v = a(bitNo \ 16)
FOR k = 1 TO (bitNo MOD 16)
v = v \ 2
NEXT
FUNCTION = (v AND 1)
END FUNCTION
' remainder of a (n limbs) divided by small m (m < 2^31)
FUNCTION BnModSmall(a() AS QUAD, BYVAL n AS LONG, BYVAL m AS LONG) AS LONG
LOCAL i AS LONG
LOCAL r AS QUAD
r = 0
FOR i = n - 1 TO 0 STEP -1
r = (r * 65536 + a(i)) MOD m
NEXT
FUNCTION = r
END FUNCTION
' a = a \ m over n limbs, returns remainder
FUNCTION BnDivSmall(a() AS QUAD, BYVAL n AS LONG, BYVAL m AS LONG) AS LONG
LOCAL i AS LONG
LOCAL r AS QUAD, Qcur AS QUAD
r = 0
FOR i = n - 1 TO 0 STEP -1
Qcur = r * 65536 + a(i)
a(i) = Qcur \ m
r = Qcur MOD m
NEXT
FUNCTION = r
END FUNCTION
' a = a * m + addv over n limbs (m <= 65536, caller guarantees no overflow)
SUB BnMulSmallAdd(a() AS QUAD, BYVAL n AS LONG, BYVAL m AS LONG, BYVAL addv AS LONG)
LOCAL i AS LONG
LOCAL s AS QUAD, c AS QUAD
c = addv
FOR i = 0 TO n - 1
s = a(i) * m + c
a(i) = s AND 65535
c = s \ 65536
NEXT
END SUB
' r = a * b (schoolbook). r must be a different array than a and b.
SUB BnMul(a() AS QUAD, BYVAL na AS LONG, b() AS QUAD, BYVAL nb AS LONG, r() AS QUAD)
LOCAL i AS LONG, j AS LONG
LOCAL s AS QUAD, c AS QUAD
BnClear(r())
FOR i = 0 TO na - 1
c = 0
FOR j = 0 TO nb - 1
s = r(i + j) + a(i) * b(j) + c
r(i + j) = s AND 65535
c = s \ 65536
NEXT
r(i + nb) = c
NEXT
END SUB
' inverse of a modulo m (small numbers, extended Euclid), 0 if none
FUNCTION ModInvSmall(BYVAL a AS QUAD, BYVAL m AS QUAD) AS QUAD
LOCAL t AS QUAD, newt AS QUAD, r AS QUAD, newr AS QUAD
LOCAL q AS QUAD, tmp AS QUAD
t = 0 : newt = 1 : r = m : newr = a
DO WHILE newr <> 0
q = r \ newr
tmp = t - q * newt : t = newt : newt = tmp
tmp = r - q * newr : r = newr : newr = tmp
LOOP
IF r > 1 THEN FUNCTION = 0 : EXIT FUNCTION
IF t < 0 THEN t = t + m
FUNCTION = t
END FUNCTION
'------------------------------------------------------------------------------
' Montgomery arithmetic
'------------------------------------------------------------------------------
' Select the (odd) modulus n of L limbs and precompute constants.
SUB SetModulus(n() AS QUAD, BYVAL L AS LONG)
LOCAL i AS LONG, k AS LONG
LOCAL Qinv AS QUAD, n0 AS QUAD, s AS QUAD, carry AS QUAD
DIM x(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
g_L = L
FOR i = 0 TO %LIMBS_MAX - 1
g_N(i) = 0 : g_R2(i) = 0 : x(i) = 0
NEXT
FOR i = 0 TO L - 1
g_N(i) = n(i)
NEXT
' n0^-1 mod 2^16 by Newton iteration, then negate
n0 = n(0) : Qinv = n0
FOR i = 1 TO 4
Qinv = (Qinv * (2 - n0 * Qinv)) AND 65535
NEXT
g_Ninv = (65536 - Qinv) AND 65535
' R^2 mod n = 2^(32*L) mod n : start at 1, double 32*L times with reduction
x(0) = 1
FOR k = 1 TO 32 * L
carry = 0
FOR i = 0 TO L - 1
s = x(i) * 2 + carry
x(i) = s AND 65535
carry = s \ 65536
NEXT
IF carry <> 0 THEN
BnSubInPlace(x(), g_N(), L)
ELSEIF BnCmp(x(), g_N(), L) >= 0 THEN
BnSubInPlace(x(), g_N(), L)
END IF
NEXT
FOR i = 0 TO L - 1
g_R2(i) = x(i)
NEXT
END SUB
' r = a * b * R^-1 mod n (CIOS method). r may be the same array as a or b.
SUB MontMul(a() AS QUAD, b() AS QUAD, r() AS QUAD)
LOCAL i AS LONG, j AS LONG, L AS LONG
LOCAL s AS QUAD, c AS QUAD, m AS QUAD, bi AS QUAD
L = g_L
FOR i = 0 TO L + 1
g_T(i) = 0
NEXT
FOR i = 0 TO L - 1
bi = b(i) : c = 0
FOR j = 0 TO L - 1
s = g_T(j) + a(j) * bi + c
g_T(j) = s AND 65535
c = s \ 65536
NEXT
s = g_T(L) + c
g_T(L) = s AND 65535
g_T(L + 1) = s \ 65536
m = (g_T(0) * g_Ninv) AND 65535
s = g_T(0) + m * g_N(0)
c = s \ 65536
FOR j = 1 TO L - 1
s = g_T(j) + m * g_N(j) + c
g_T(j - 1) = s AND 65535
c = s \ 65536
NEXT
s = g_T(L) + c
g_T(L - 1) = s AND 65535
g_T(L) = g_T(L + 1) + (s \ 65536)
g_T(L + 1) = 0
NEXT
FOR i = 0 TO L - 1
r(i) = g_T(i)
NEXT
FOR i = L TO %LIMBS_MAX - 1
r(i) = 0
NEXT
IF g_T(L) <> 0 THEN
BnSubInPlace(r(), g_N(), L)
ELSEIF BnCmp(r(), g_N(), L) >= 0 THEN
BnSubInPlace(r(), g_N(), L)
END IF
END SUB
' res = a * b mod n (plain, non-Montgomery in/out)
SUB ModMulPlain(a() AS QUAD, b() AS QUAD, r() AS QUAD)
DIM tmp(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
MontMul(a(), b(), tmp())
MontMul(tmp(), g_R2(), r())
END SUB
' res = base ^ ex mod n (base < n; modulus must be set with SetModulus)
SUB ModPow(Qbase() AS QUAD, ex() AS QUAD, BYVAL exLimbs AS LONG, RES() AS QUAD)
LOCAL bitNo AS LONG, nbits AS LONG
DIM bm(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM acc(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM one(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
BnClear(one())
one(0) = 1
MontMul(Qbase(), g_R2(), bm()) ' base in Montgomery form
MontMul(one(), g_R2(), acc()) ' 1 in Montgomery form
nbits = BnBitLength(ex(), exLimbs)
FOR bitNo = nbits - 1 TO 0 STEP -1
MontMul(acc(), acc(), acc())
IF BnGetBit(ex(), bitNo) = 1 THEN
MontMul(acc(), bm(), acc())
END IF
NEXT
MontMul(acc(), one(), RES()) ' back to normal form
END SUB
'------------------------------------------------------------------------------
' Random numbers, small primes, primality test
'------------------------------------------------------------------------------
SUB InitSmallPrimes
LOCAL i AS LONG, j AS LONG
DIM comp(0 TO 2000) AS LOCAL BYTE
g_SPCount = 0
FOR i = 2 TO 2000
IF comp(i) = 0 THEN
g_SP(g_SPCount) = i
INCR g_SPCount
FOR j = i * i TO 2000 STEP i
comp(j) = 1
NEXT
END IF
NEXT
END SUB
SUB SeedRandom
LOCAL q AS QUAD, seed AS LONG, i AS LONG, dummy AS LONG
' Mixes several high-resolution timer samples. This is NOT strong entropy.
seed = 0
FOR i = 1 TO 8
dummy = QueryPerformanceCounter(q)
seed = seed XOR CLNG(q AND &H7FFFFFFF)
seed = (seed * 31 + i) AND &H7FFFFFFF
SLEEP 1
NEXT
RANDOMIZE seed
END SUB
FUNCTION RndLimb() AS QUAD
FUNCTION = INT(RND * 65536)
END FUNCTION
' Miller-Rabin using the first %MR_ROUNDS small primes as bases
FUNCTION IsProbablePrime(n() AS QUAD, BYVAL L AS LONG) AS LONG
LOCAL i AS LONG, s AS LONG, r AS LONG, rounds AS LONG, passed AS LONG
DIM d(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM nm1(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM x(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM a(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM one(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
SetModulus(n(), L)
FOR i = 0 TO %LIMBS_MAX - 1
d(i) = n(i) : nm1(i) = n(i) : one(i) = 0
NEXT
d(0) = d(0) - 1 ' n is odd, so no borrow
nm1(0) = nm1(0) - 1
one(0) = 1
s = 0
DO WHILE (d(0) AND 1) = 0
BnShr1(d(), L)
INCR s
LOOP
FOR rounds = 0 TO %MR_ROUNDS - 1
BnClear(a())
a(0) = g_SP(rounds)
ModPow(a(), d(), L, x())
IF BnCmp(x(), one(), L) <> 0 AND BnCmp(x(), nm1(), L) <> 0 THEN
passed = 0
FOR r = 1 TO s - 1
ModMulPlain(x(), x(), x())
IF BnCmp(x(), nm1(), L) = 0 THEN
passed = 1
EXIT FOR
END IF
NEXT
IF passed = 0 THEN FUNCTION = 0 : EXIT FUNCTION
END IF
NEXT
FUNCTION = 1
END FUNCTION
' random prime of L limbs: top two bits set, odd, (p-1) not divisible by e
SUB GenPrime(p() AS QUAD, BYVAL L AS LONG)
LOCAL i AS LONG, k AS LONG, ok AS LONG
DO
BnClear(p())
FOR i = 0 TO L - 1
p(i) = RndLimb()
NEXT
p(0) = p(0) OR 1
p(L - 1) = p(L - 1) OR 49152 ' &HC000
ok = 1
IF BnModSmall(p(), L, %E_PUBLIC) = 1 THEN ok = 0 ' p-1 divisible by e
IF ok = 1 THEN
FOR k = 0 TO g_SPCount - 1
IF BnModSmall(p(), L, g_SP(k)) = 0 THEN ok = 0 : EXIT FOR
NEXT
END IF
IF ok = 1 THEN
IF IsProbablePrime(p(), L) = 1 THEN EXIT SUB
END IF
LOOP
END SUB
'------------------------------------------------------------------------------
' Key generation
' n = p*q, e = 65537, d = e^-1 mod (p-1)(q-1)
' d without big division: choose k in [1,e-1] with (1 + k*phi) divisible by e
' (k = -phi^-1 mod e, computed with small integers), then d = (1 + k*phi)/e.
'------------------------------------------------------------------------------
SUB RsaGenerateKey(n() AS QUAD, d() AS QUAD)
LOCAL r AS LONG, k AS LONG, i AS LONG
DIM p(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM q(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM p1(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM q1(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM phi(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DO
GenPrime(p(), %HALF_LIMBS)
DO
GenPrime(q(), %HALF_LIMBS)
LOOP WHILE BnCmp(p(), q(), %HALF_LIMBS) = 0
BnMul(p(), %HALF_LIMBS, q(), %HALF_LIMBS, n())
BnCopy(p(), p1()) : p1(0) = p1(0) - 1 ' primes are odd
BnCopy(q(), q1()) : q1(0) = q1(0) - 1
BnMul(p1(), %HALF_LIMBS, q1(), %HALF_LIMBS, phi())
r = BnModSmall(phi(), %KEY_LIMBS, %E_PUBLIC)
IF r <> 0 THEN EXIT DO ' gcd(e, phi) = 1
LOOP
k = (%E_PUBLIC - ModInvSmall(r, %E_PUBLIC)) MOD %E_PUBLIC
BnMulSmallAdd(phi(), %KEY_LIMBS + 2, k, 1) ' phi = 1 + k*phi
BnDivSmall(phi(), %KEY_LIMBS + 2, %E_PUBLIC) ' exact division
BnClear(d())
FOR i = 0 TO %KEY_LIMBS - 1
d(i) = phi(i)
NEXT
END SUB
'------------------------------------------------------------------------------
' Conversions
'------------------------------------------------------------------------------
' big-endian byte string (even length) -> big number
SUB BytesToBn(s AS STRING, a() AS QUAD)
LOCAL i AS LONG, ln AS LONG
ln = LEN(s)
BnClear(a())
FOR i = 0 TO (ln \ 2) - 1
a(i) = ASC(s, ln - 2 * i) + 256 * ASC(s, ln - 2 * i - 1)
NEXT
END SUB
' big number -> big-endian byte string of nbytes (even)
FUNCTION BnToBytes(a() AS QUAD, BYVAL nbytes AS LONG) AS STRING
LOCAL i AS LONG
LOCAL s AS STRING
s = STRING$(nbytes, 0)
FOR i = 0 TO (nbytes \ 2) - 1
ASC(s, nbytes - 2 * i) = CLNG(a(i) AND 255)
ASC(s, nbytes - 2 * i - 1) = CLNG((a(i) \ 256) AND 255)
NEXT
FUNCTION = s
END FUNCTION
FUNCTION BnToHex(a() AS QUAD, BYVAL n AS LONG) AS STRING
LOCAL i AS LONG
LOCAL s AS STRING
FOR i = n - 1 TO 0 STEP -1
s = s + HEX$(CLNG(a(i)), 4)
NEXT
FUNCTION = s
END FUNCTION
FUNCTION BytesToHex(s AS STRING) AS STRING
LOCAL i AS LONG
LOCAL h AS STRING
FOR i = 1 TO LEN(s)
h = h + HEX$(ASC(s, i), 2)
NEXT
FUNCTION = h
END FUNCTION
'------------------------------------------------------------------------------
' Encrypt / decrypt with PKCS#1 v1.5 (type 2) padding
' EM = 00 02 PS(>=8 random nonzero bytes) 00 M (256 bytes)
'------------------------------------------------------------------------------
FUNCTION RsaEncrypt(msg AS STRING, n() AS QUAD) AS STRING
LOCAL mlen AS LONG, i AS LONG
LOCAL em AS STRING
DIM m(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM c(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM ex(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
mlen = LEN(msg)
IF mlen > 245 THEN FUNCTION = "" : EXIT FUNCTION
em = STRING$(256, 0)
ASC(em, 2) = 2
FOR i = 3 TO 256 - mlen - 1
ASC(em, i) = 1 + INT(RND * 255)
NEXT
IF mlen > 0 THEN MID$(em, 257 - mlen, mlen) = msg
BytesToBn(em, m())
SetModulus(n(), %KEY_LIMBS)
BnClear(ex())
ex(0) = 1 : ex(1) = 1 ' 65537 = &H10001
ModPow(m(), ex(), 2, c())
FUNCTION = BnToBytes(c(), 256)
END FUNCTION
FUNCTION RsaDecrypt(cipher AS STRING, n() AS QUAD, d() AS QUAD) AS STRING
LOCAL i AS LONG
LOCAL em AS STRING
DIM c(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
DIM m(0 TO %LIMBS_MAX - 1) AS LOCAL QUAD
IF LEN(cipher) <> 256 THEN FUNCTION = "" : EXIT FUNCTION
BytesToBn(cipher, c())
IF BnCmp(c(), n(), %KEY_LIMBS) >= 0 THEN FUNCTION = "" : EXIT FUNCTION
SetModulus(n(), %KEY_LIMBS)
ModPow(c(), d(), %KEY_LIMBS, m())
em = BnToBytes(m(), 256)
IF ASC(em, 1) <> 0 OR ASC(em, 2) <> 2 THEN FUNCTION = "" : EXIT FUNCTION
FOR i = 3 TO 256
IF ASC(em, i) = 0 THEN EXIT FOR
NEXT
IF i < 11 OR i > 256 THEN FUNCTION = "" : EXIT FUNCTION ' bad padding
FUNCTION = MID$(em, i + 1)
END FUNCTION
'------------------------------------------------------------------------------
' Demo
'------------------------------------------------------------------------------
FUNCTION PBMAIN() AS LONG
LOCAL msg AS STRING, cipher AS STRING, plain AS STRING
LOCAL t0 AS SINGLE
DIM n(0 TO %LIMBS_MAX - 1) AS QUAD
DIM d(0 TO %LIMBS_MAX - 1) AS QUAD
REDIM g_N(0 TO %LIMBS_MAX - 1)
REDIM g_R2(0 TO %LIMBS_MAX - 1)
REDIM g_T(0 TO %LIMBS_MAX + 1)
REDIM g_SP(0 TO 399)
InitSmallPrimes
SeedRandom
? "Generating RSA-2048 key (two 1024-bit primes), please wait..."
msg = "Test this 123456 789*)($#@!"
t0 = TIMER
RsaGenerateKey(n(), d())
LOCAL dspmsg AS STRING
dspmsg = "Done in "+ USING$("##.#", TIMER - t0)+ " s" + $CRLF
dspmsg += "Modulus n (hex):" + _
BnToHex(n(), %KEY_LIMBS) + $CRLF
dspmsg += "Public exponent e = "+ STR$( %E_PUBLIC)
dspmsg += $CRLF + "Message : "+ msg + $CRLF
cipher = RsaEncrypt(msg, n())
dspmsg += "Cipher : "+ BytesToHex(cipher) + $CRLF
plain = RsaDecrypt(cipher, n(), d())
dspmsg += "Decrypted: "+ plain + $CRLF
IF plain = msg THEN
dspmsg += "Round trip OK."
ELSE
dspmsg += "Round trip FAILED."
END IF
? dspmsg
END FUNCTION
|
|
|
| RSA-2048 in PowerBasic (no Crypto API) |
|
Posted by: Steven Pringels - 12.11.2026, 20:28 - Forum: Source Code Library
- No Replies
|
 |
I asked Claude to generate the RSA-2048 algorithm (encryption/decryption) and here it is in PBCC 6.0.
No Crypto API calls. The generation of the key takes quite some time. Be patient.
Code: ' ==============================================================================
' RSA2048.bas - RSA-2048 in pure PowerBasic (no Windows Crypto API)
'
' - Big numbers: arrays of QUAD, each element holds one 16-bit limb
' (little-endian: element 0 = least significant 16 bits).
' 16-bit limbs keep every intermediate product well inside a signed QUAD.
' - Modular exponentiation: Montgomery multiplication (no big division needed)
' - Key generation: random 1024-bit primes, trial division + Miller-Rabin
' - Private exponent: derived without big division (e is small, see below)
' - Padding: PKCS#1 v1.5 (type 2) for text messages, max 245 bytes
'
' Written as a PBCC console program. For PBWin, replace PRINT/WAITKEY$ with
' MSGBOX or file output.
'
' WARNING - educational code:
' * RND is NOT a cryptographically secure random source. For real keys use
' an OS entropy source.
' * Not constant-time (timing side channels), no CRT, no blinding.
' * PKCS#1 v1.5 padding is legacy; OAEP would need a hash function.
'==============================================================================
#Compile Exe
#Dim All
#Include "Win32Api.Inc" ' only for QueryPerformanceCounter (seeding)
%LIMBS_MAX = 136 ' 128 limbs = 2048 bits, plus guard limbs
%KEY_LIMBS = 128 ' 2048 bits
%HALF_LIMBS = 64 ' 1024 bits (p and q)
%E_PUBLIC = 65537
%MR_ROUNDS = 10 ' Miller-Rabin rounds (bases = first primes)
Global g_L As Long ' limbs in current modulus
Global g_Ninv As Quad ' -n^-1 mod 2^16
Global g_N() As Quad ' current modulus
Global g_R2() As Quad ' R^2 mod n, R = 2^(16*L)
Global g_T() As Quad ' scratch for MontMul
Global g_SP() As Long ' small primes
Global g_SPCount As Long
'------------------------------------------------------------------------------
' Basic big-number helpers
'------------------------------------------------------------------------------
Sub BnClear(a() As Quad)
Local i As Long
For i = 0 To %LIMBS_MAX - 1
a(i) = 0
Next
End Sub
Sub BnCopy(src() As Quad, dst() As Quad)
Local i As Long
For i = 0 To %LIMBS_MAX - 1
dst(i) = src(i)
Next
End Sub
' compare over n limbs: -1, 0, 1
Function BnCmp(a() As Quad, b() As Quad, ByVal n As Long) As Long
Local i As Long
For i = n - 1 To 0 Step -1
If a(i) > b(i) Then Function = 1 : Exit Function
If a(i) < b(i) Then Function = -1 : Exit Function
Next
Function = 0
End Function
' a = a - b over n limbs (a >= b assumed, or the borrow is dropped)
Sub BnSubInPlace(a() As Quad, b() As Quad, ByVal n As Long)
Local i As Long
Local d As Quad, borrow As Quad
borrow = 0
For i = 0 To n - 1
d = a(i) - b(i) - borrow
If d < 0 Then
d = d + 65536 : borrow = 1
Else
borrow = 0
End If
a(i) = d
Next
End Sub
' a = a >> 1 over n limbs
Sub BnShr1(a() As Quad, ByVal n As Long)
Local i As Long
Local t As Quad, carry As Quad
carry = 0
For i = n - 1 To 0 Step -1
t = a(i)
a(i) = (t + carry * 65536) \ 2
carry = t And 1
Next
End Sub
Function BnBitLength(a() As Quad, ByVal n As Long) As Long
Local i As Long, b As Long
Local v As Quad
For i = n - 1 To 0 Step -1
If a(i) <> 0 Then
v = a(i) : b = 0
Do While v > 0
Incr b
v = v \ 2
Loop
Function = i * 16 + b
Exit Function
End If
Next
Function = 0
End Function
Function BnGetBit(a() As Quad, ByVal bitNo As Long) As Long
Local k As Long
Local v As Quad
v = a(bitNo \ 16)
For k = 1 To (bitNo Mod 16)
v = v \ 2
Next
Function = (v And 1)
End Function
' remainder of a (n limbs) divided by small m (m < 2^31)
Function BnModSmall(a() As Quad, ByVal n As Long, ByVal m As Long) As Long
Local i As Long
Local r As Quad
r = 0
For i = n - 1 To 0 Step -1
r = (r * 65536 + a(i)) Mod m
Next
Function = r
End Function
' a = a \ m over n limbs, returns remainder
Function BnDivSmall(a() As Quad, ByVal n As Long, ByVal m As Long) As Long
Local i As Long
Local r As Quad, Qcur As Quad
r = 0
For i = n - 1 To 0 Step -1
Qcur = r * 65536 + a(i)
a(i) = Qcur \ m
r = Qcur Mod m
Next
Function = r
End Function
' a = a * m + addv over n limbs (m <= 65536, caller guarantees no overflow)
Sub BnMulSmallAdd(a() As Quad, ByVal n As Long, ByVal m As Long, ByVal addv As Long)
Local i As Long
Local s As Quad, c As Quad
c = addv
For i = 0 To n - 1
s = a(i) * m + c
a(i) = s And 65535
c = s \ 65536
Next
End Sub
' r = a * b (schoolbook). r must be a different array than a and b.
Sub BnMul(a() As Quad, ByVal na As Long, b() As Quad, ByVal nb As Long, r() As Quad)
Local i As Long, j As Long
Local s As Quad, c As Quad
Call BnClear(r())
For i = 0 To na - 1
c = 0
For j = 0 To nb - 1
s = r(i + j) + a(i) * b(j) + c
r(i + j) = s And 65535
c = s \ 65536
Next
r(i + nb) = c
Next
End Sub
' inverse of a modulo m (small numbers, extended Euclid), 0 if none
Function ModInvSmall(ByVal a As Quad, ByVal m As Quad) As Quad
Local t As Quad, newt As Quad, r As Quad, newr As Quad
Local q As Quad, tmp As Quad
t = 0 : newt = 1 : r = m : newr = a
Do While newr <> 0
q = r \ newr
tmp = t - q * newt : t = newt : newt = tmp
tmp = r - q * newr : r = newr : newr = tmp
Loop
If r > 1 Then Function = 0 : Exit Function
If t < 0 Then t = t + m
Function = t
End Function
'------------------------------------------------------------------------------
' Montgomery arithmetic
'------------------------------------------------------------------------------
' Select the (odd) modulus n of L limbs and precompute constants.
Sub SetModulus(n() As Quad, ByVal L As Long)
Local i As Long, k As Long
Local Qinv As Quad, n0 As Quad, s As Quad, carry As Quad
Dim x(0 To %LIMBS_MAX - 1) As Local Quad
g_L = L
For i = 0 To %LIMBS_MAX - 1
g_N(i) = 0 : g_R2(i) = 0 : x(i) = 0
Next
For i = 0 To L - 1
g_N(i) = n(i)
Next
' n0^-1 mod 2^16 by Newton iteration, then negate
n0 = n(0) : Qinv = n0
For i = 1 To 4
Qinv = (Qinv * (2 - n0 * Qinv)) And 65535
Next
g_Ninv = (65536 - Qinv) And 65535
' R^2 mod n = 2^(32*L) mod n : start at 1, double 32*L times with reduction
x(0) = 1
For k = 1 To 32 * L
carry = 0
For i = 0 To L - 1
s = x(i) * 2 + carry
x(i) = s And 65535
carry = s \ 65536
Next
If carry <> 0 Then
Call BnSubInPlace(x(), g_N(), L)
ElseIf BnCmp(x(), g_N(), L) >= 0 Then
Call BnSubInPlace(x(), g_N(), L)
End If
Next
For i = 0 To L - 1
g_R2(i) = x(i)
Next
End Sub
' r = a * b * R^-1 mod n (CIOS method). r may be the same array as a or b.
Sub MontMul(a() As Quad, b() As Quad, r() As Quad)
Local i As Long, j As Long, L As Long
Local s As Quad, c As Quad, m As Quad, bi As Quad
L = g_L
For i = 0 To L + 1
g_T(i) = 0
Next
For i = 0 To L - 1
bi = b(i) : c = 0
For j = 0 To L - 1
s = g_T(j) + a(j) * bi + c
g_T(j) = s And 65535
c = s \ 65536
Next
s = g_T(L) + c
g_T(L) = s And 65535
g_T(L + 1) = s \ 65536
m = (g_T(0) * g_Ninv) And 65535
s = g_T(0) + m * g_N(0)
c = s \ 65536
For j = 1 To L - 1
s = g_T(j) + m * g_N(j) + c
g_T(j - 1) = s And 65535
c = s \ 65536
Next
s = g_T(L) + c
g_T(L - 1) = s And 65535
g_T(L) = g_T(L + 1) + (s \ 65536)
g_T(L + 1) = 0
Next
For i = 0 To L - 1
r(i) = g_T(i)
Next
For i = L To %LIMBS_MAX - 1
r(i) = 0
Next
If g_T(L) <> 0 Then
Call BnSubInPlace(r(), g_N(), L)
ElseIf BnCmp(r(), g_N(), L) >= 0 Then
Call BnSubInPlace(r(), g_N(), L)
End If
End Sub
' res = a * b mod n (plain, non-Montgomery in/out)
Sub ModMulPlain(a() As Quad, b() As Quad, r() As Quad)
Dim tmp(0 To %LIMBS_MAX - 1) As Local Quad
Call MontMul(a(), b(), tmp())
Call MontMul(tmp(), g_R2(), r())
End Sub
' res = base ^ ex mod n (base < n; modulus must be set with SetModulus)
Sub ModPow(Qbase() As Quad, ex() As Quad, ByVal exLimbs As Long, Res() As Quad)
Local bitNo As Long, nbits As Long
Dim bm(0 To %LIMBS_MAX - 1) As Local Quad
Dim acc(0 To %LIMBS_MAX - 1) As Local Quad
Dim one(0 To %LIMBS_MAX - 1) As Local Quad
Call BnClear(one())
one(0) = 1
Call MontMul(Qbase(), g_R2(), bm()) ' base in Montgomery form
Call MontMul(one(), g_R2(), acc()) ' 1 in Montgomery form
nbits = BnBitLength(ex(), exLimbs)
For bitNo = nbits - 1 To 0 Step -1
Call MontMul(acc(), acc(), acc())
If BnGetBit(ex(), bitNo) = 1 Then
Call MontMul(acc(), bm(), acc())
End If
Next
Call MontMul(acc(), one(), Res()) ' back to normal form
End Sub
'------------------------------------------------------------------------------
' Random numbers, small primes, primality test
'------------------------------------------------------------------------------
Sub InitSmallPrimes
Local i As Long, j As Long
Dim comp(0 To 2000) As Local Byte
g_SPCount = 0
For i = 2 To 2000
If comp(i) = 0 Then
g_SP(g_SPCount) = i
Incr g_SPCount
For j = i * i To 2000 Step i
comp(j) = 1
Next
End If
Next
End Sub
Sub SeedRandom
Local q As Quad, seed As Long, i As Long, dummy As Long
' Mixes several high-resolution timer samples. This is NOT strong entropy.
seed = 0
For i = 1 To 8
dummy = QueryPerformanceCounter(q)
seed = seed Xor CLng(q And &H7FFFFFFF)
seed = (seed * 31 + i) And &H7FFFFFFF
Sleep 1
Next
Randomize seed
End Sub
Function RndLimb() As Quad
Function = Int(Rnd * 65536)
End Function
' Miller-Rabin using the first %MR_ROUNDS small primes as bases
Function IsProbablePrime(n() As Quad, ByVal L As Long) As Long
Local i As Long, s As Long, r As Long, rounds As Long, passed As Long
Dim d(0 To %LIMBS_MAX - 1) As Local Quad
Dim nm1(0 To %LIMBS_MAX - 1) As Local Quad
Dim x(0 To %LIMBS_MAX - 1) As Local Quad
Dim a(0 To %LIMBS_MAX - 1) As Local Quad
Dim one(0 To %LIMBS_MAX - 1) As Local Quad
Call SetModulus(n(), L)
For i = 0 To %LIMBS_MAX - 1
d(i) = n(i) : nm1(i) = n(i) : one(i) = 0
Next
d(0) = d(0) - 1 ' n is odd, so no borrow
nm1(0) = nm1(0) - 1
one(0) = 1
s = 0
Do While (d(0) And 1) = 0
Call BnShr1(d(), L)
Incr s
Loop
For rounds = 0 To %MR_ROUNDS - 1
Call BnClear(a())
a(0) = g_SP(rounds)
Call ModPow(a(), d(), L, x())
If BnCmp(x(), one(), L) <> 0 And BnCmp(x(), nm1(), L) <> 0 Then
passed = 0
For r = 1 To s - 1
Call ModMulPlain(x(), x(), x())
If BnCmp(x(), nm1(), L) = 0 Then
passed = 1
Exit For
End If
Next
If passed = 0 Then Function = 0 : Exit Function
End If
Next
Function = 1
End Function
' random prime of L limbs: top two bits set, odd, (p-1) not divisible by e
Sub GenPrime(p() As Quad, ByVal L As Long)
Local i As Long, k As Long, ok As Long
Do
Call BnClear(p())
For i = 0 To L - 1
p(i) = RndLimb()
Next
p(0) = p(0) Or 1
p(L - 1) = p(L - 1) Or 49152 ' &HC000
ok = 1
If BnModSmall(p(), L, %E_PUBLIC) = 1 Then ok = 0 ' p-1 divisible by e
If ok = 1 Then
For k = 0 To g_SPCount - 1
If BnModSmall(p(), L, g_SP(k)) = 0 Then ok = 0 : Exit For
Next
End If
If ok = 1 Then
If IsProbablePrime(p(), L) = 1 Then Exit Sub
End If
Loop
End Sub
'------------------------------------------------------------------------------
' Key generation
' n = p*q, e = 65537, d = e^-1 mod (p-1)(q-1)
' d without big division: choose k in [1,e-1] with (1 + k*phi) divisible by e
' (k = -phi^-1 mod e, computed with small integers), then d = (1 + k*phi)/e.
'------------------------------------------------------------------------------
Sub RsaGenerateKey(n() As Quad, d() As Quad)
Local r As Long, k As Long, i As Long
Dim p(0 To %LIMBS_MAX - 1) As Local Quad
Dim q(0 To %LIMBS_MAX - 1) As Local Quad
Dim p1(0 To %LIMBS_MAX - 1) As Local Quad
Dim q1(0 To %LIMBS_MAX - 1) As Local Quad
Dim phi(0 To %LIMBS_MAX - 1) As Local Quad
Do
Call GenPrime(p(), %HALF_LIMBS)
Do
Call GenPrime(q(), %HALF_LIMBS)
Loop While BnCmp(p(), q(), %HALF_LIMBS) = 0
Call BnMul(p(), %HALF_LIMBS, q(), %HALF_LIMBS, n())
Call BnCopy(p(), p1()) : p1(0) = p1(0) - 1 ' primes are odd
Call BnCopy(q(), q1()) : q1(0) = q1(0) - 1
Call BnMul(p1(), %HALF_LIMBS, q1(), %HALF_LIMBS, phi())
r = BnModSmall(phi(), %KEY_LIMBS, %E_PUBLIC)
If r <> 0 Then Exit Do ' gcd(e, phi) = 1
Loop
k = (%E_PUBLIC - ModInvSmall(r, %E_PUBLIC)) Mod %E_PUBLIC
Call BnMulSmallAdd(phi(), %KEY_LIMBS + 2, k, 1) ' phi = 1 + k*phi
Call BnDivSmall(phi(), %KEY_LIMBS + 2, %E_PUBLIC) ' exact division
Call BnClear(d())
For i = 0 To %KEY_LIMBS - 1
d(i) = phi(i)
Next
End Sub
'------------------------------------------------------------------------------
' Conversions
'------------------------------------------------------------------------------
' big-endian byte string (even length) -> big number
Sub BytesToBn(s As String, a() As Quad)
Local i As Long, ln As Long
ln = Len(s)
Call BnClear(a())
For i = 0 To (ln \ 2) - 1
a(i) = Asc(s, ln - 2 * i) + 256 * Asc(s, ln - 2 * i - 1)
Next
End Sub
' big number -> big-endian byte string of nbytes (even)
Function BnToBytes(a() As Quad, ByVal nbytes As Long) As String
Local i As Long
Local s As String
s = String$(nbytes, 0)
For i = 0 To (nbytes \ 2) - 1
Asc(s, nbytes - 2 * i) = CLng(a(i) And 255)
Asc(s, nbytes - 2 * i - 1) = CLng((a(i) \ 256) And 255)
Next
Function = s
End Function
Function BnToHex(a() As Quad, ByVal n As Long) As String
Local i As Long
Local s As String
For i = n - 1 To 0 Step -1
s = s + Hex$(CLng(a(i)), 4)
Next
Function = s
End Function
Function BytesToHex(s As String) As String
Local i As Long
Local h As String
For i = 1 To Len(s)
h = h + Hex$(Asc(s, i), 2)
Next
Function = h
End Function
'------------------------------------------------------------------------------
' Encrypt / decrypt with PKCS#1 v1.5 (type 2) padding
' EM = 00 02 PS(>=8 random nonzero bytes) 00 M (256 bytes)
'------------------------------------------------------------------------------
Function RsaEncrypt(msg As String, n() As Quad) As String
Local mlen As Long, i As Long
Local em As String
Dim m(0 To %LIMBS_MAX - 1) As Local Quad
Dim c(0 To %LIMBS_MAX - 1) As Local Quad
Dim ex(0 To %LIMBS_MAX - 1) As Local Quad
mlen = Len(msg)
If mlen > 245 Then Function = "" : Exit Function
em = String$(256, 0)
Asc(em, 2) = 2
For i = 3 To 256 - mlen - 1
Asc(em, i) = 1 + Int(Rnd * 255)
Next
If mlen > 0 Then Mid$(em, 257 - mlen, mlen) = msg
Call BytesToBn(em, m())
Call SetModulus(n(), %KEY_LIMBS)
Call BnClear(ex())
ex(0) = 1 : ex(1) = 1 ' 65537 = &H10001
Call ModPow(m(), ex(), 2, c())
Function = BnToBytes(c(), 256)
End Function
Function RsaDecrypt(cipher As String, n() As Quad, d() As Quad) As String
Local i As Long
Local em As String
Dim c(0 To %LIMBS_MAX - 1) As Local Quad
Dim m(0 To %LIMBS_MAX - 1) As Local Quad
If Len(cipher) <> 256 Then Function = "" : Exit Function
Call BytesToBn(cipher, c())
If BnCmp(c(), n(), %KEY_LIMBS) >= 0 Then Function = "" : Exit Function
Call SetModulus(n(), %KEY_LIMBS)
Call ModPow(c(), d(), %KEY_LIMBS, m())
em = BnToBytes(m(), 256)
If Asc(em, 1) <> 0 Or Asc(em, 2) <> 2 Then Function = "" : Exit Function
For i = 3 To 256
If Asc(em, i) = 0 Then Exit For
Next
If i < 11 Or i > 256 Then Function = "" : Exit Function ' bad padding
Function = Mid$(em, i + 1)
End Function
'------------------------------------------------------------------------------
' Demo
'------------------------------------------------------------------------------
Function PBMain() As Long
Local msg As String, cipher As String, plain As String
Local t0 As Single
Dim n(0 To %LIMBS_MAX - 1) As Quad
Dim d(0 To %LIMBS_MAX - 1) As Quad
ReDim g_N(0 To %LIMBS_MAX - 1)
ReDim g_R2(0 To %LIMBS_MAX - 1)
ReDim g_T(0 To %LIMBS_MAX + 1)
ReDim g_SP(0 To 399)
Call InitSmallPrimes
Call SeedRandom
Print "Generating RSA-2048 key (two 1024-bit primes), please wait..."
t0 = Timer
Call RsaGenerateKey(n(), d())
Print "Done in "; Using$("##.#", Timer - t0); " s"
Print
Print "Modulus n (hex):"
Print BnToHex(n(), %KEY_LIMBS)
Print
Print "Public exponent e = "; %E_PUBLIC
Print
msg = "Hello from pure PowerBasic RSA-2048!"
Print "Message : "; msg
cipher = RsaEncrypt(msg, n())
Print "Cipher : "; BytesToHex(cipher)
Print
plain = RsaDecrypt(cipher, n(), d())
Print "Decrypted: "; plain
If plain = msg Then
Print "Round trip OK."
Else
Print "Round trip FAILED."
End If
Print
Print "Press any key..."
WaitKey$
End Function
|
|
|
| Program that won't close |
|
Posted by: Giuseppe Belziti - 25.10.2026, 12:37 - Forum: PowerBASIC for Windows
- Replies (6)
|
 |
I have a program that I can't close; I've tried everything. Here is the current situation:
Code: CALLBACK FUNCTION AggiornaXProc()
SELECT CASE CB.MSG
'...........................................chiudi con X
CASE %WM_SYSCOMMAND
IF (CB.WPARAM AND &HFFF0) = %SC_CLOSE THEN
PID = SHELL("PAY.EXE", 1)
DIALOG END CB.HNDL, 0 ' Chiude hDlg2
DIALOG END hDlg, 0 ' Chiude hDlg
CLOSE ' Rilascia gli handle file
END ' Termina definitivamente PAY_START.EXE
END IF
END SELECT
END FUNCTION
Code: ' --- RICHIAMA PAY.EXE ---
PID = SHELL("PAY.EXE", 1)
' --- CHIUSURA ORDINATA IN SEQUENZA CORRETTA ---
DIALOG END hDlg2, 0 ' Chiude prima il dialogo figlio (Aggiornamenti)
DIALOG END hDlg, 0 ' Poi chiude la finestra principale (AgriPag)
CLOSE ' Chiude tutti i canali file aperti
END ' Forza la terminazione del processo
I also tried using taskkill, but it doesn't work.
If I try from the CMD, on the other hand, I get this—and you can see that the program was open:
C:\PAG>cmd.exe /c taskkill /F /IM PAY_START.EXE
OPERAZIONE RIUSCITA: il processo "PAY_START.EXE" con il PID 8960 è stato terminato.
Do you have a solution for closing this program safely? P.S. This only happens with this one...
|
|
|
| EnableVisualStyles.bas |
|
Posted by: Borje Hagsten - 24.10.2026, 17:13 - Forum: Source Code Library
- Replies (5)
|
 |
Don't remember if anyone posted this kind of function in any of the old - now gone - forums, so here it is. Works for me in Windows 10 - hopefully also in later versions. To be compiled with PB/Win 10. Some comments explain.
Code: '======================================================================
' Small example of how to enable visual styles in CommCtrl.dll v.6 via
' code instead of using manifest file in resource. Maybe not so useful,
' since doing it via xptheme.xml manifest in resource is easy, but here
' it is anyway. Public Domain by Borje Hagsten October 24, 2026.
'
' Added: Made a correction to deactivation via SUB DisableVisualStyles,
' where FreeeLibrary and ReleaseActCtx was added.
' Thank you Stanley Durham for pointing this out.
'======================================================================
#COMPILE EXE
#DIM ALL
'--------------------------------------------------------------------
%UNICODE = 1
#INCLUDE "WIN32API.INC"
'--------------------------------------------------------------------
%IDC_TEXTBOX1 = 531
GLOBAL ghActCtx, gCookie, ghShell32 AS LONG ' need to store these as global.
'======================================================================
FUNCTION PBMAIN () AS LONG
LOCAL hDlg AS LONG
EnableVisualStyles() 'Activate at entry
DIALOG NEW 0, "EnableVisualStyles",,, 185, 65, %WS_CAPTION OR %WS_SYSMENU OR _
%WS_MINIMIZEBOX, 0 TO hDlg
DIALOG SET COLOR hDlg, -1, RGB(196, 196, 196)
'------------------------------------------------------------------
CONTROL ADD TEXTBOX, hDlg, %IDC_TEXTBOX1, "TextBox1", 5, 5, 100, 55
CONTROL ADD BUTTON, hDlg, %IDCANCEL, "&Ok", 110, 46, 70, 14
'------------------------------------------------------------------
DIALOG SHOW MODAL hDlg ' no callback - just a demo of how to..
IF gCookie THEN DisableVisualStyles ' Deactivate at exit
END FUNCTION
'=================================================================================
' Enable visual styles in CommCtrl.dll v.6, using manifest resource in shell32.dll
'---------------------------------------------------------------------------------
FUNCTION EnableVisualStyles() AS DWORD
LOCAL aCtx AS ACTCTX
#IF %DEF(%UNICODE)
LOCAL szSys AS WSTRINGZ * %MAX_PATH
#ELSE
LOCAL szSys AS ASCIIZ * %MAX_PATH
#ENDIF
GetSystemDirectory szSys, SIZEOF(szSys) ' Need path to shell32.dll
aCtx.cbSize = SIZEOF(ACTCTX)
aCtx.dwFlags = %ACTCTX_FLAG_RESOURCE_NAME_VALID OR _
%ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID OR _
%ACTCTX_FLAG_HMODULE_VALID
aCtx.hModule = LoadLibrary("shell32.dll") ' Use the manifest resource in shell32.dll
aCtx.lpResourceName = 124 ' Manifest resource name
aCtx.lpAssemblyDirectory = VARPTR(szSys)
ghShell32 = aCtx.hModule
ghActCtx = CreateActCtx(aCtx) 'push the specified activation context to the top of the activation stack
IF ghActCtx <> %INVALID_HANDLE_VALUE THEN 'vs.hActCtx = dwRes, &vs.cookie = dwCookie, vs.hShell32 = aCtx.hModule
ActivateActCtx(ghActCtx, gCookie)
END IF
FUNCTION = gCookie
END FUNCTION
'=================================================================================
' Disable visual styles
'---------------------------------------------------------------------------------
SUB DisableVisualStyles
IF gCookie THEN DeactivateActCtx(0, gCookie)
IF NOT ghActCtx = %INVALID_HANDLE_VALUE THEN ReleaseActCtx(ghActCtx)
IF ghShell32 THEN FreeLibrary(ghShell32)
END SUB
|
|
|
|