Instantiate new OpenNetCF.Desktop.Communication objects

Jan 25, 2010 at 5:56 PM

I have downloaded the project and built the DLL.  I have added a reference to the DLL to my VS2008 VB.Net project.  Both my project and the OpenNetCF DLL are targeting .NET Framework 3.5.  I have an Imports OpenNetCF.DeskTop.Communication statement at the top of my code file.

I can declare a variable as one of the OpenNetCF objects: Dim rmsAS as ActiveSync

However, the variable is Nothing.

If I try to: Dim rmsAS as New ActiveSync

I get an error saying that something is inaccessible because it is declared as Friend.

I looked at the OpenNetCF project and I don't see anything declared as Friend.

What I am I doing wrong?


Jan 25, 2010 at 6:17 PM

What are you trying to so with the Active Sync declaration?

I use the following four subs to connect, disconnect, "verify File exists" and "copy from device to PC".

Public Sub CopySDFfromIpaqToPC()
        Me.lblStatus.Text = "Copying database from iPAQ"

        'Create the InstallFilesPath (declare up top) if it doesn't already exist.
        If Not Directory.Exists(InstallFilesPath) Then Directory.CreateDirectory(InstallFilesPath)

        Me.DeleteSDFonPC() 'Delete SDF located on the PC, if it already exists


        If Me.VerifyFileExistsOnIpaq() = True Then
                myrapi.CopyFileFromDevice(InstallFilesPath & SDFfile, PathiPAQ)
            Catch ex As Exception
                Throw New Exception("Error copying SDF from iPAQ: " & ex.Message)
            End Try
            Throw New Exception("Cannot continue. SDF does not exist on iPAQ.")
        End If

End Sub


Public Sub DisconnectFromIpaq()
End Sub

Public Sub DeleteSDFonPC()
            If File.Exists(InstallFilesPath & SDFfile) Then
                Dim fi As FileInfo = New FileInfo(InstallFilesPath & SDFfile)
            End If
        Catch x As Exception
            Dim ErrMsg As String = ""
            If Err.Number = 57 Then
                ErrMsg = "Unable to delete the existing temporary mdb file. It is possible that you currently have it open. Please close the file and try again"
                ErrMsg = Err.Number & " " & Err.Description
            End If
            Throw New Exception(ErrMsg)
        End Try
End Sub


Public Function VerifyFileExistsOnIpaq() As Boolean
        If myrapi.DeviceFileExists(PathiPAQ) = True Then 'If the SDF file exists, check further (conditions have been found where an empty field is left)
            If myrapi.GetDeviceFileSize(PathiPAQ) > 0 Then 'Make sure the file is bigger than 0kb, otherwise we will error out later on
                Return True 'If the device is >0kb, all should be good, return a 'True'
                If myrapi.DeviceFileExists(NewFileName) = True Then 'Check to make sure last sync didn't rollback correctly by checking for temp copy
                    If myrapi.GetDeviceFileSize(NewFileName) > 0 Then ' If the temp copy does exist, make sure it is larger than 0kb
                        myrapi.DeleteDeviceFile(PathiPAQ) 'Delete the 0kb file
                        myrapi.MoveDeviceFile(NewFileName, PathiPAQ) 'If it does exist and is >0kb, rename the temp file with the actual filename
                        Return True
                        Return False
                    End If
                End If
           End If
            Return False
        End If
End Function



Public Sub ConnectToiPAQ()
            ' Connect to the iPAQ.
            Do While Not myrapi.DevicePresent
                MessageBox.Show("Please connect your device to your PC using " & Me.SyncMethod() & " before clicking the OK button.", "No Device Present")

        Catch ex As Exception
            MessageBox.Show("The following error occurred while attempting to connect to your device - " & ex.Message, "Connection Error")
            Application.Exit() '<- Do we leave this in?
        End Try
End Sub


Jan 25, 2010 at 9:15 PM

Hi DaveyMac.

Thanks for the reply.

I want to start an application on the Windows Mobile device after ActiveSync has completed.  I was looking at AutoStartOnConnect and AutoStartOnDisconnect, but I see that those trigger an app on the PC.  It looks like the right answer is going to be CERunAppAtEvent in the coredll.dll.

When I try to set up the event:


 <DllImport("coredll.dll")> _
    Public Shared Function CERunAppAtEvent(ByVal appname As String, ByVal whichEvent As Integer) As Integer
    End Function


    End Try

I get an error message from the app: Can't find an Entry Point 'CERunAppAtEvent' in a PInvoke DLL 'coredll.dll'.



Jan 26, 2010 at 8:55 AM
Edited Jan 26, 2010 at 9:09 AM

I think you'll find it's 'CeRunAppAtEvent' and not 'CERunAppAtEvent'

Jan 26, 2010 at 11:38 AM

Thanks NeilC.

I discovered that it was case-sensitive late in the day.

I am still trying to figure out how to properly instantiate the OpenNetCF.Desktop.Communication objects.  Any help would be appreciated.

This declaration produces the message below: Dim rmsAS as New ActiveSync
'OpenNetCF.Desktop.Communication.ActiveSync.Friend Sub New()' is not accessible in this context because it is 'Friend'.

When I look at the OpenNetCF project code - there is no New() defined for the ActiveSync class.

Jan 26, 2010 at 11:54 AM

There won't be a New() method in the ActiveSync as "New()" is the VB syntax for the class constructor. In C#, the syntax is a method with the same name as the class. Also, "Friend" in VB is "protected" in C#.

You gain access to an instance of the ActiveSync class through the ActiveSync property of the RAPI class, like so:

' Define and instantiate the RAPI class
Dim _rapi As New RAPI()
' Connect synchronously
' Get a reference to the ActiveSync object
Dim _activeSync As ActiveSync = _rapi.ActiveSync

Jan 26, 2010 at 12:24 PM

Thanks a lot NeilC!

When I was looking the OpenNetCF DLL with the Object Browser, I didn't pick up on the fact that the ActiveSync Class/Object was the definition for a property of another class/object.  Even when I look as the Class Diagram for the OpenNetCF project, I don't see anything that would help to point out the relationships between classes.

All that said, a lesson has been learned.  If VB balks at instantiating a New object, then I should look for the class containing a property that the object/class defines.

Thanks again!