Com Ports question

New to FreeBASIC? Post your questions here.
Post Reply
aurbo99
Posts: 3
Joined: Dec 11, 2019 5:05

Com Ports question

Post by aurbo99 »

Greetings
With the example below, How would I convert the

Code: Select all

c(n) = Val(k)
portname(n) = "COM" & Str(c(n)) & ":"
print portname(n)
into a ComboBox list for use in a WinFBE form?

Code: Select all

#include "windows.bi"
#include once "win\setupapi.bi"
dim shared portname(99) as string
dim shared c(99) as integer

Function getport() As Integer
    Dim As HDEVINFO hDevInfo
    Dim As SP_DEVINFO_DATA DeviceInfoData
    Dim As Integer buffersize, DataT,i,n
    Dim As String buffer,buffer1,k
    
    
    hDevInfo=SetupDiGetClassDevs(NULL,0,0,DIGCF_PRESENT _
    Or DIGCF_ALLCLASSES)
    
    If hDevInfo=INVALID_HANDLE_VALUE Then
        Print "SetupDiGet failed, sleeping to exit.."
        Sleep
        End
    End If
    
    DeviceInfoData.cbSize=Sizeof(SP_DEVINFO_DATA)
    SetupDiEnumDeviceInfo(hDevInfo,i,@DeviceInfoData)
    For i=1 To 25000
        
        SetLastError(0)
        SetupDiEnumDeviceInfo(hDevInfo,i,@DeviceInfoData)
        If GetLastError()=ERROR_NO_MORE_ITEMS Then Exit For
        
        buffer=Space(1024)
        buffersize=1024
        '
        SetupDiGetDeviceRegistryProperty(_
        hDevInfo,_
        @DeviceInfoData,_
        SPDRP_FRIENDLYNAME,_ 'SPDRP_DEVICEDESC
        @DataT,_
        Strptr(buffer),_
        buffersize,_
        @buffersize)
        ' 
        buffer=Trim(buffer)           
        If Instr(buffer,"(COM") Then
            print buffer
            k = Mid$(buffer,Instr(buffer,"(COM")+4,7)
            
            c(n) = Val(k)
            portname(n) = "COM" & Str(c(n)) & ":"
            print portname(n)
             
            n+=1
        End If
        
    Next i
    Return n
End Function
dim a as integer
a = getport
print "Number of serial ports = ";a
sleep
bfuller
Posts: 362
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

Re: Com Ports question

Post by bfuller »

I have found this
https://en.wikipedia.org/wiki/PowerShell
I guess many users are already familiar, but it is recently discovered by me. I haven't played around in depth, but I bet there is a way to use PowerShell to get what you want. The docs say it can be invoked from within another application so I suppose you could call the appropriate script from FreeBasic. Seems it can do almost anything with windows under the hood----no wonder hacking is so easy!
Post Reply