Link to home
Start Free TrialLog in
Avatar of nike_golf
nike_golfFlag for Afghanistan

asked on

Need help finishing routine to managed dll... s/b easy for skilled programmer.. I think.

Rename attached file to gps.dll

I'm trying to implement the example of the GPS Intermediate driver using manged code from MSDN (http://msdn2.microsoft.com/en-us/library/bb158708.aspx) and I'm stuck.

I'm using VS2005 and I've attached my source as limited as it is. What I need is for someone to help me finish what pieces that I'm missing so that I can retrieve GPS data, etc.

I have a few errors showing in the IDE and not sure how to fix them:

"'System.EventHandler' is a delegate type and requires a single 'addressof' expression as the only argument to the constructor."

and

"Name 'updateDataHandler' is not declared."


Hopefully someone can help.

NG,
Imports System.Runtime.InteropServices
Imports Microsoft.WindowsMobile.Samples.Location
 
Public Class Form1
 
    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim gps As New Gps()
        Dim device As GpsDeviceState = Nothing
        Dim position As GpsPosition = Nothing
 
 
        updateDataHandler = New System.EventHandler(UpdateData)
        AddHandler gps.DeviceStateChanged, AddressOf gps_DeviceState_Changed
        AddHandler gps.LocationChanged, AddressOf gps_Location_changed
 
 
    End Sub
 
    Private Sub gps_Location_changed(ByVal sender As Object, ByVal args As LocationChangedEventArgs)
        Dim position
 
        position = args.Position
        Invoke(updateDataHandler)
 
    End Sub
 
    Private Sub gps_DeviceState_Changed(ByVal sender As Object, ByVal args As DeviceStateChangedEventArgs)
 
    End Sub

Open in new window

GPS.dll.txt
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Try this:
1. Move the declaration code (the Dims) that you have in the button click out of there into the main class.
2. Add this into the main class as well:

Dim updateDataHandler As System.EventHandler = new System.EventHandler(UpdateData)

3. You also need to add the UpdateData function that is in the article
Avatar of nike_golf

ASKER

I've moved the dims to the main class and added your suggestion also to the main class but still have the same error.

'System.EventHandler' is a delegate type and requires a single 'addressof' expression as the only argument to the constructor."

hmmm... I don't see the UpdateData function?

thx,
'Put this at the top of your module.
'Required in all cases when calling API functions
Imports System.Runtime.InteropServices
Imports Microsoft.WindowsMobile.Samples.Location
 
Public Class Form1
    'Dim updateDataHandler As EventHandler
    Dim gps As New Gps()
    Dim device As GpsDeviceState = Nothing
    Dim position As GpsPosition = Nothing
    Dim updateDataHandler As System.EventHandler = New System.EventHandler(UpdateData)
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        gps.Open()
        If gps.Opened Then
            MsgBox("Open")
            MsgBox(position.Speed + ";" + position.Longitude + ";" + position.Latitude)
        Else
            MsgBox("Failed to open")
        End If
 
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If gps.Opened Then
            gps.Close()
            MsgBox("closed")
        End If
    End Sub
 
    Private Sub gps_Location_changed(ByVal sender As Object, ByVal args As LocationChangedEventArgs)
 
        position = args.Position
        Invoke(updateDataHandler)
 
    End Sub
 
 
    Private Sub gps_DeviceState_Changed(ByVal sender As Object, ByVal args As DeviceStateChangedEventArgs)
 
        device = args.DeviceState
        Invoke(updateDataHandler)
 
    End Sub
 
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        AddHandler gps.DeviceStateChanged, AddressOf gps_DeviceState_Changed
        AddHandler gps.LocationChanged, AddressOf gps_Location_changed
    End Sub
End Class

Open in new window

UpdateData was in the link you provided.. But it is not in your code. You need that function:
http://msdn2.microsoft.com/en-us/library/bb158708.aspx
I see it was missing... funny I still have the error though?




'Put this at the top of your module.
'Required in all cases when calling API functions
Imports System.Runtime.InteropServices
Imports Microsoft.WindowsMobile.Samples.Location
 
 
Public Class Form1
    Dim updateDataHandler As EventHandler
    'Dim updateDataHandler As System.EventHandler = New System.EventHandler(UpdateData)
    Dim gps As New Gps()
    Dim device As GpsDeviceState = Nothing
    Dim position As GpsPosition = Nothing
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        gps.Open()
        If gps.Opened Then
            MsgBox("Open")
            MsgBox(position.Speed + ";" + position.Longitude + ";" + position.Latitude)
        Else
            MsgBox("Failed to open")
        End If
 
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If gps.Opened Then
            gps.Close()
            MsgBox("closed")
        End If
    End Sub
 
    Private Sub gps_Location_changed(ByVal sender As Object, ByVal args As LocationChangedEventArgs)
 
        position = args.Position
        Invoke(updateDataHandler)
 
    End Sub
 
    Private Sub gps_DeviceState_Changed(ByVal sender As Object, ByVal args As DeviceStateChangedEventArgs)
 
        device = args.DeviceState
        Invoke(updateDataHandler)
 
    End Sub
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        updateDataHandler = New System.EventHandler(UpdateData)
        AddHandler gps.DeviceStateChanged, AddressOf gps_DeviceState_Changed
        AddHandler gps.LocationChanged, AddressOf gps_Location_changed
    End Sub
 
    Public Sub UpdateData(ByVal sender As Object, ByVal args As System.EventArgs)
 
        If gps.Opened Then
            Dim str As String = ""
            If device IsNot Nothing Then
                str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "" & Chr(10) & ""
            End If
            If position IsNot Nothing Then
                If position.SeaLevelAltitudeValid AndAlso position.EllipsoidAltitudeValid AndAlso position.SpeedValid AndAlso position.LatitudeValid AndAlso position.LongitudeValid AndAlso position.SatellitesInSolutionValid AndAlso position.SatellitesInViewValid AndAlso position.SatelliteCountValid AndAlso position.TimeValid Then
                    MsgBox(position.SeaLevelAltitude + ";" + position.EllipsoidAltitude + ";" + position.Speed + ";" + position.Longitude + ";" + position.Latitude + ";" + position.GetSatellitesInSolution().Length + "/" + position.GetSatellitesInView().Length + " (" + position.SatelliteCount + ";" + position.Time.ToString())
                End If
            End If
        End If
    End Sub
 
 
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That got rid of the error message.

So I tried deploying the app. it was able to open the gps device but I'm getting some errors.

A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.FormatException' occurred in System.Windows.Forms.dll

and an error box that says "FormatException was unhandled" pointing to the Invoke(updateDataHandler) line in the sub gps_DeviceState_Changed

System.FormatException was unhandled
  Message="FormatException"
  StackTrace:
    at System.Number.ParseDouble()
    at System.Double.Parse()
    at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble()
    at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble()
    at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble()
    at DeviceApplication1.Form1.UpdateData()
    at TASK.Invoke()
    at System.Windows.Forms.Control._InvokeAll()
    at System.Windows.Forms.Control.WnProc()
    at System.Windows.Forms.ContainerControl.WnProc()
    at System.Windows.Forms.Form.WnProc()
    at System.Windows.Forms.Control._InternalWnProc()
    at Microsoft.AGL.Forms.DLG.MessageBox()
    at System.Windows.Forms.MessageBox.Show()
    at Microsoft.VisualBasic.Interaction.MsgBox()
    at DeviceApplication1.Form1.Button1_Click()
    at System.Windows.Forms.Control.OnClick()
    at System.Windows.Forms.Button.OnClick()
    at System.Windows.Forms.ButtonBase.WnProc()
    at System.Windows.Forms.Control._InternalWnProc()
    at Microsoft.AGL.Forms.EVL.EnterMainLoop()
    at System.Windows.Forms.Application.Run()
    at DeviceApplication1.Form1.Main()
Unfortunately, that is both beyond the scope of the original question and the help I can provide at the moment.. Were you unable to find an example using VB.NET for the Gps classes?
No nothing for vb.net...

Can you give me some place to start on how to figure out the problem?

I really wish I could, but I just haven't done anything with the GPS components.
I picked up some of the source from this link and it appears he was having a similar/same problem...

http://forum.xda-developers.com/showthread.php?p=1859272
I'm thinking this could be the line causing the issue for you:

MsgBox(position.Speed + ";" + position.Longitude + ";" + position.Latitude)

What happens when you remove that?
Same error, I just commented it and re-ran...

Can you explain what the Invoke() function is supposed to do?

For kicks I just inserted a msgbox above the Invoke() and it just errored before getting to the message box in the gps_DeviceState_Changed sub, strange...

    Private Sub gps_DeviceState_Changed(ByVal sender As Object, ByVal args As DeviceStateChangedEventArgs)
 
        device = args.DeviceState
        MsgBox("now")
        Invoke(updateDataHandler)
 
    End Sub

Open in new window

I've got this other syntax associated with the error "Children could not be evaluated" I attached a pic.



Error.jpg
It turned out to be a string in the UpdateData() function -

str = device.FriendlyName + " " + device.ServiceState + ", " + device.DeviceState + "" & Chr(10) & ""

I was able to pull GPS data, wahoo!!!

Thanks for the help.