Microsoft KB Archive/249144: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "&" to "&")
m (Text replacement - """ to """)
 
Line 65: Line 65:


Private Sub Form_Load()
Private Sub Form_Load()
     Command1.Caption = "Get connected device name"
     Command1.Caption = "Get connected device name"
End Sub
End Sub


Line 75: Line 75:
     lcon = ConnectRapi
     lcon = ConnectRapi
     If lcon <> ERROR_SUCCESS Then
     If lcon <> ERROR_SUCCESS Then
       MsgBox &quot;Failure to initialize RAPI&quot;
       MsgBox "Failure to initialize RAPI"
     Else
     Else
       MsgBox &quot;RAPI was initialized successfully&quot;
       MsgBox "RAPI was initialized successfully"
     End If
     End If
      
      
Line 86: Line 86:
     lcon = DisconnectRapi
     lcon = DisconnectRapi
     If lcon <> ERROR_SUCCESS Then
     If lcon <> ERROR_SUCCESS Then
       MsgBox &quot;Failure to uninitialize RAPI&quot;
       MsgBox "Failure to uninitialize RAPI"
     Else
     Else
       MsgBox &quot;RAPI was uninitialized successfully&quot;
       MsgBox "RAPI was uninitialized successfully"
     End If
     End If
        
        
Line 108: Line 108:
End Type
End Type


Declare Function CeRapiUninit Lib &quot;rapi.dll&quot; () As Long
Declare Function CeRapiUninit Lib "rapi.dll" () As Long


Declare Function CeRapiInitEx Lib &quot;rapi.dll&quot; ( _
Declare Function CeRapiInitEx Lib "rapi.dll" ( _
     pRapiInit As RAPIINIT) As Long
     pRapiInit As RAPIINIT) As Long
      
      
Declare Function CeRegOpenKeyEx Lib &quot;rapi.dll&quot; ( _
Declare Function CeRegOpenKeyEx Lib "rapi.dll" ( _
     ByVal hkey As Long, _
     ByVal hkey As Long, _
     ByVal lpSubKey As Long, _
     ByVal lpSubKey As Long, _
Line 120: Line 120:
     phkResult As Long) As Long
     phkResult As Long) As Long
      
      
Declare Function CeRegQueryValueEx Lib &quot;rapi.dll&quot; ( _
Declare Function CeRegQueryValueEx Lib "rapi.dll" ( _
     ByVal hkey As Long, _
     ByVal hkey As Long, _
     ByVal lpValueName As Long, _
     ByVal lpValueName As Long, _
Line 128: Line 128:
     lpcbData As Long) As Long
     lpcbData As Long) As Long
      
      
Declare Function CeRegQueryValueExLong Lib &quot;rapi.dll&quot; _
Declare Function CeRegQueryValueExLong Lib "rapi.dll" _
     Alias &quot;CeRegQueryValueEx&quot; ( _
     Alias "CeRegQueryValueEx" ( _
     ByVal hkey As Long, _
     ByVal hkey As Long, _
     ByVal lpValueName As Long, _
     ByVal lpValueName As Long, _
Line 137: Line 137:
     lpcbData As Long) As Long
     lpcbData As Long) As Long
      
      
Declare Function CeRegQueryValueExString Lib &quot;rapi.dll&quot; _
Declare Function CeRegQueryValueExString Lib "rapi.dll" _
     Alias &quot;CeRegQueryValueEx&quot; ( _
     Alias "CeRegQueryValueEx" ( _
     ByVal hkey As Long, _
     ByVal hkey As Long, _
     ByVal lpValueName As Long, _
     ByVal lpValueName As Long, _
Line 146: Line 146:
     lpcbData As Long) As Long
     lpcbData As Long) As Long


Declare Function CeRegCloseKey Lib &quot;rapi.dll&quot; ( _
Declare Function CeRegCloseKey Lib "rapi.dll" ( _
     ByVal hkey As Long) As Long
     ByVal hkey As Long) As Long


Line 184: Line 184:
     Dim value As String
     Dim value As String
      
      
     key = &quot;Ident&quot;
     key = "Ident"
     lret = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, StrPtr(key), _
     lret = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, StrPtr(key), _
                           0, 0, phkResult)
                           0, 0, phkResult)
      
      
     If lret <> ERROR_SUCCESS Then
     If lret <> ERROR_SUCCESS Then
         MsgBox &quot;Failure to open key.  Error: &quot; & lret
         MsgBox "Failure to open key.  Error: " & lret
     Else
     Else
         value = &quot;Name&quot;
         value = "Name"
         lret = CeRegQueryValueEx(phkResult, StrPtr(value), _
         lret = CeRegQueryValueEx(phkResult, StrPtr(value), _
                                 0, lpType, 0, lpcbData)
                                 0, lpType, 0, lpcbData)
Line 205: Line 205:
                            
                            
                 If lret = ERROR_SUCCESS Then
                 If lret = ERROR_SUCCESS Then
                     MsgBox &quot;The name of the currently connected &quot; & _
                     MsgBox "The name of the currently connected " & _
                             &quot;device is &quot; & Left(lpdata, lpcbData - 1)
                             "device is " & Left(lpdata, lpcbData - 1)
                 Else
                 Else
                     MsgBox &quot;Your device does not have a name&quot;
                     MsgBox "Your device does not have a name"
                     lpdata = &quot;&quot;
                     lpdata = ""
                 End If
                 End If
                  
                  

Latest revision as of 13:51, 21 July 2020

Knowledge Base


How To Identify the CE Device Connected to the Desktop Machine

Article ID: 249144

Article Last Modified on 7/1/2004



APPLIES TO

  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Windows CE 2.0 for the Handheld PC
  • Microsoft Windows CE 2.11 for the Handheld PC
  • Microsoft Windows CE 2.12 for the Handheld PC
  • Microsoft Windows CE 3.0 for the Handheld PC



This article was previously published under Q249144

SUMMARY

This article demonstrates how to call the RAPI functions in Visual Basic to retrieve the identification information from the registry on the remote Windows CE device connected to the desktop. The application will run on the desktop and requires that the desktop machine communicate with the Windows CE device using either Windows CE Services version 2.x or ActiveSync version 3.x.

MORE INFORMATION

  1. Start a new Standard EXE in Visual Basic. Form1 is created by default.
  2. Add a CommandButton (Command1) to Form1.
  3. Paste the following into the code module of Form1:

    Option Explicit
    
    Private Sub Form_Load()
        Command1.Caption = "Get connected device name"
    End Sub
    
    Private Sub Command1_Click()
        Dim lret As Long
        Dim lcon As Long
        
        ' Initialize RAPI
        lcon = ConnectRapi
        If lcon <> ERROR_SUCCESS Then
           MsgBox "Failure to initialize RAPI"
        Else
           MsgBox "RAPI was initialized successfully"
        End If
        
        ' Retrieve the registry information on the remote device
        lret = ReadCEREgistry
        
        ' Uninitialize RAPI
        lcon = DisconnectRapi
        If lcon <> ERROR_SUCCESS Then
           MsgBox "Failure to uninitialize RAPI"
        Else
           MsgBox "RAPI was uninitialized successfully"
        End If
           
    End Sub
                        
  4. From the Project menu, choose Add Module. Module1 will be added to the project.
  5. Paste the following code into Module1:

    Public Const HKEY_CLASSES_ROOT = &H80000000
    Public Const HKEY_CURRENT_USER = &H80000001
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public Const ERROR_SUCCESS = 0
    Public Const REG_DWORD = 4        '32-bit number.
    Public Const REG_SZ = 1
    
    Type RAPIINIT
        cbsize As Long
        heRapiInit As Long
        hrRapiInit As Long
    End Type
    
    Declare Function CeRapiUninit Lib "rapi.dll" () As Long
    
    Declare Function CeRapiInitEx Lib "rapi.dll" ( _
        pRapiInit As RAPIINIT) As Long
        
    Declare Function CeRegOpenKeyEx Lib "rapi.dll" ( _
        ByVal hkey As Long, _
        ByVal lpSubKey As Long, _
        ByVal ulOptions As Long, _
        ByVal samDesired As Long, _
        phkResult As Long) As Long
        
    Declare Function CeRegQueryValueEx Lib "rapi.dll" ( _
        ByVal hkey As Long, _
        ByVal lpValueName As Long, _
        ByVal lpReserved As Long, _
        lpType As Long, _
        ByVal lpdata As Long, _
        lpcbData As Long) As Long
        
    Declare Function CeRegQueryValueExLong Lib "rapi.dll" _
        Alias "CeRegQueryValueEx" ( _
        ByVal hkey As Long, _
        ByVal lpValueName As Long, _
        ByVal lpReserved As Long, _
        lpType As Long, _
        lpdata As Long, _
        lpcbData As Long) As Long
        
    Declare Function CeRegQueryValueExString Lib "rapi.dll" _
        Alias "CeRegQueryValueEx" ( _
        ByVal hkey As Long, _
        ByVal lpValueName As Long, _
        ByVal lpReserved As Long, _
        lpType As Long, _
        ByVal lpdata As Long, _
        lpcbData As Long) As Long
    
    Declare Function CeRegCloseKey Lib "rapi.dll" ( _
        ByVal hkey As Long) As Long
    
    ' Initialize RAPI
    Function ConnectRapi() As Long
        Dim lcon As Long
        Dim lRapiInit As RAPIINIT
        
        With lRapiInit
            .cbsize = Len(lRapiInit)
            .heRapiInit = 0
            .hrRapiInit = 0
        End With
        
        lcon = CeRapiInitEx(lRapiInit)
        ConnectRapi = lcon
    End Function
    
    ' Uninitialize RAPI
    Function DisconnectRapi() As Long
        Dim lcon As Long
        lcon = CeRapiUninit
        DisconnectRapi = lcon
    End Function
    
    ' Read the registry on the CE device
    Function ReadCEREgistry() As Long
        Dim lret As Long
        Dim phkResult As Long
        Dim lpType As Long
        Dim lpdata As String
        Dim lpvalue As Long
        Dim lpcbData As Long
        Dim data As String
        Dim key As String
        Dim lpdwdisposition As Long
        Dim value As String
        
        key = "Ident"
        lret = CeRegOpenKeyEx(HKEY_LOCAL_MACHINE, StrPtr(key), _
                              0, 0, phkResult)
        
        If lret <> ERROR_SUCCESS Then
            MsgBox "Failure to open key.  Error: " & lret
        Else
            value = "Name"
            lret = CeRegQueryValueEx(phkResult, StrPtr(value), _
                                     0, lpType, 0, lpcbData)
            Select Case lpType
                ' -- For strings
                Case REG_SZ:
                    ' Allocate string space
                    lpdata = String(lpcbData, 0)
                    
                    ' Query the string value
                    lret = CeRegQueryValueExString(phkResult, StrPtr(value), _
                              CLng(0), lpType, StrPtr(lpdata), lpcbData)
                              
                    If lret = ERROR_SUCCESS Then
                        MsgBox "The name of the currently connected " & _
                                "device is " & Left(lpdata, lpcbData - 1)
                    Else
                        MsgBox "Your device does not have a name"
                        lpdata = ""
                    End If
                    
                '-- For DWORDS
                Case REG_DWORD:
                    lret = CeRegQueryValueExLong(phkResult, StrPtr(value), _
                            CLng(0), lpType, lpvalue, lpcbData)
                    If lret = ERROR_SUCCESS Then MsgBox lpvalue
                    
                ' -- All other data types not supported
                Case Else
                    lret = -1
            End Select
        End If
        
        ReadCEREgistry = lret
    End Function
                        
  6. Press F5 to run the application.
  7. Click Command1 and you should get 3 messages showing the status of RAPI initialization, the device's name, and the status of RAPI uninitialization.



Additional query words: RAPI wce vbce vbce6 eVB

Keywords: kbhowto kbapi kbtoolkit KB249144