Results 1 to 6 of 6

Thread: Detecting Modems(Very Urgent!!!)

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Location
    Pune
    Posts
    1

    Detecting Modems(Very Urgent!!!)

    Hi all,
    I am devloping the dialer application in VB.In that I am facing one prob. that is I require the list of modems attached to a computer in a combo box.Any help in this is highly appreciated.

    Thanx in Advance
    Sachin...

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    there is 2 way you can do it.

    1. detect the DTR with your MSComm32.OCX control
    2. Juz open all the available serial port, send a "AT" & vbCrLf command to the opened port and juz wait for the OK response from the modem. If you can get a OK, mean the modem is present, else modem is not attached to the specific port.

    regards,

  3. #3
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    There is an easier way; all modems are stored in the registry...

    Paste this into a module
    VB Code:
    1. Option Explicit
    2.  
    3. Public Const HKEY_CLASSES_ROOT = &H80000000
    4. Public Const HKEY_CURRENT_USER = &H80000001
    5. Public Const HKEY_LOCAL_MACHINE = &H80000002
    6. Public Const HKEY_USERS = &H80000003
    7. Public Const HKEY_PERFORMANCE_DATA = &H80000004
    8. Public Const HKEY_CURRENT_CONFIG = &H80000005
    9. Public Const HKEY_DYN_DATA = &H80000006
    10. Public Const REG_SZ = 1                         ' Unicode nul terminated string
    11. Public Const REG_BINARY = 3                     ' Free form binary
    12. Public Const REG_DWORD = 4                      ' 32-bit number
    13. Public Const ERROR_SUCCESS = 0&
    14.  
    15. Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    16. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    17. Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
    18. Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
    19. Public Function GetAllKeys(hKey As Long, strPath As String) As Variant
    20. ' Returns: an array in a variant of strings
    21.  
    22. Dim lRegResult As Long
    23. Dim lCounter As Long
    24. Dim hCurKey As Long
    25. Dim strBuffer As String
    26. Dim lDataBufferSize As Long
    27. Dim strNames() As String
    28. Dim intZeroPos As Integer
    29.  
    30. lCounter = 0
    31.  
    32. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    33.  
    34. Do
    35.  
    36.   'initialise buffers (longest possible length=255)
    37.   lDataBufferSize = 255
    38.   strBuffer = String(lDataBufferSize, " ")
    39.   lRegResult = RegEnumKey(hCurKey, lCounter, strBuffer, lDataBufferSize)
    40.  
    41.   If lRegResult = ERROR_SUCCESS Then
    42.  
    43.     'tidy up string and save it
    44.     ReDim Preserve strNames(lCounter) As String
    45.    
    46.     intZeroPos = InStr(strBuffer, Chr$(0))
    47.     If intZeroPos > 0 Then
    48.       strNames(UBound(strNames)) = Left$(strBuffer, intZeroPos - 1)
    49.     Else
    50.       strNames(UBound(strNames)) = strBuffer
    51.     End If
    52.  
    53.     lCounter = lCounter + 1
    54.  
    55.   Else
    56.     Exit Do
    57.   End If
    58. Loop
    59.  
    60. GetAllKeys = strNames
    61.  
    62. End Function
    63. Public Function GetSettingString(hKey As Long, strPath As String, strValue As String, Optional Default As String) As String
    64. Dim hCurKey As Long
    65. Dim lValueType As Long
    66. Dim strBuffer As String
    67. Dim lDataBufferSize As Long
    68. Dim intZeroPos As Integer
    69. Dim lRegResult As Long
    70.  
    71. ' Set up default value
    72. If Not IsEmpty(Default) Then
    73.   GetSettingString = Default
    74. Else
    75.   GetSettingString = ""
    76. End If
    77.  
    78. ' Open the key and get length of string
    79. lRegResult = RegOpenKey(hKey, strPath, hCurKey)
    80. lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, lValueType, ByVal 0&, lDataBufferSize)
    81.  
    82. If lRegResult = ERROR_SUCCESS Then
    83.  
    84.   If lValueType = REG_SZ Then
    85.     ' initialise string buffer and retrieve string
    86.     strBuffer = String(lDataBufferSize, " ")
    87.     lRegResult = RegQueryValueEx(hCurKey, strValue, 0&, 0&, ByVal strBuffer, lDataBufferSize)
    88.    
    89.     ' format string
    90.     intZeroPos = InStr(strBuffer, Chr$(0))
    91.     If intZeroPos > 0 Then
    92.       GetSettingString = Left$(strBuffer, intZeroPos - 1)
    93.     Else
    94.       GetSettingString = strBuffer
    95.     End If
    96.  
    97.   End If
    98.  
    99. Else
    100.   ' there is a problem
    101. End If
    102.  
    103. lRegResult = RegCloseKey(hCurKey)
    104. End Function
    Usage:
    VB Code:
    1. Dim SubKeys As Variant
    2. Dim KeyLoop As Integer
    3. Dim KeyName As String
    4. Dim FullName As String
    5.  
    6. SubKeys = GetAllKeys(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\Modem")
    7.  
    8. ' Get Full names of all modems installed and put in user select box
    9. If VarType(SubKeys) = vbArray + vbString Then
    10.     For KeyLoop = 0 To UBound(SubKeys)
    11.         KeyName = SubKeys(KeyLoop)
    12.         FullName = GetSettingString(HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\Modem\" & KeyName, "DriverDesc")
    13.         Combo1.AddItem FullName
    14.     Next
    15. End If

  4. #4
    Addicted Member
    Join Date
    Jul 2001
    Posts
    133
    If you would rather use the API instead of accessing the registry, which is what a lot of API calls do anyways, look into TAPI. It is alive and well in Windows.

  5. #5
    Hyperactive Member jovton's Avatar
    Join Date
    Nov 2000
    Location
    South Africa
    Posts
    266
    chrisjk, I have Win XP (SP1) with a two modems, and those registry entries does not even exists on me system. Are the paths different for different OS'es (i.e. 98 / NT 4.0)?
    jovton

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Try something like this:

    In a Standard Module:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const RAS_MaxDeviceType = 16
    4. Private Const RAS_MaxDeviceName = 128
    5.  
    6. Private Const GMEM_FIXED = &H0
    7. Private Const GMEM_ZEROINIT = &H40
    8. Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
    9.  
    10. Private Const APINULL = 0&
    11.  
    12. Private Type RASDEVINFO
    13.   dwSize As Long
    14.   szDeviceType(RAS_MaxDeviceType) As Byte
    15.   szDeviceName(RAS_MaxDeviceName) As Byte
    16. End Type
    17.  
    18. Private Declare Function RasEnumDevices Lib "rasapi32.dll" Alias "RasEnumDevicesA" (lpRasDevInfo As Any, lpcb As Long, lpcDevices As Long) As Long
    19. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    20. Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
    21. Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
    22.  
    23. Public Function GetRASDeviceList() As String()
    24.   Dim sList() As String
    25.   Dim tRasDevInfo As RASDEVINFO
    26.   Dim lCb As Long
    27.   Dim lDevices As Long
    28.   Dim lBuff As Long
    29.   Dim lRet As Long
    30.   Dim lPtr As Long
    31.   Dim lCount As Long
    32.  
    33.   lCb = 0
    34.  
    35.   tRasDevInfo.dwSize = LenB(tRasDevInfo) + (LenB(tRasDevInfo) Mod 4)
    36.  
    37.   lRet = RasEnumDevices(ByVal 0, lCb, lDevices)
    38.  
    39.   lBuff = GlobalAlloc(GPTR, lCb)
    40.  
    41.   CopyMemory ByVal lBuff, tRasDevInfo, LenB(tRasDevInfo)
    42.  
    43.   lRet = RasEnumDevices(ByVal lBuff, lCb, lCb)
    44.  
    45.   If lRet = 0 Then
    46.     lPtr = lBuff
    47.    
    48.     Do While lDevices > 0
    49.       CopyMemory tRasDevInfo, ByVal lPtr, LenB(tRasDevInfo)
    50.       If StrComp(ByteToString(tRasDevInfo.szDeviceType), "Modem", vbTextCompare) = 0 Then
    51.         ReDim Preserve sList(lCount)
    52.         sList(lCount) = ByteToString(tRasDevInfo.szDeviceName)
    53.         lCount = lCount + 1
    54.       End If
    55.       lPtr = lPtr + LenB(tRasDevInfo) + (LenB(tRasDevInfo) Mod 4)
    56.       lDevices = lDevices - 1
    57.     Loop
    58.   End If
    59.  
    60.   If lBuff <> 0 Then GlobalFree (lBuff)
    61.  
    62.   GetRASDeviceList = sList
    63. End Function
    64.  
    65. Private Function ByteToString(ByRef bByte() As Byte) As String
    66.   Dim lCount As Long
    67.   Dim sString As String
    68.  
    69.   Do While lCount < UBound(bByte) And bByte(lCount) <> 0
    70.     sString = sString & Chr$(bByte(lCount))
    71.     lCount = lCount + 1
    72.   Loop
    73.   ByteToString = sString
    74. End Function
    Example Usage:
    VB Code:
    1. Private Sub Form_Load()
    2.   Dim sList() As String
    3.   Dim lCount As Long
    4.  
    5.   sList = GetRASDeviceList()
    6.   For lCount = 0 To UBound(sList)
    7.     List1.AddItem sList(lCount)
    8.   Next
    9. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width