Help and Support
 

powered byLive Search

How To Identify the CE Device Connected to the Desktop Machine

Article ID:249144
Last Review:July 1, 2004
Revision:1.1
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.

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

Back to the top

Keywords: 
kbhowto kbapi kbtoolkit KB249144

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.