none
How to call GetSystemPowerStatusEx2 with CeRapiInvoke RRS feed

  • Question

  • The windows core.dll has the function “GetSystemPowerStatusEx2” I can call it from managed code written in vb.net, running on the mobile devices. 

    I have a RAPI wrapper class that vb.net uses running on a desktop PC.  I will include the code from it at the bottom. When I call the GetDeviceSystemPowerStatus function ie:
    Dim tmp As SYSTEM_POWER_STATUS_EX = myRapi.GetDeviceSystemPowerStatus()
    If I put a break point inside the function on:
    Dim tmp As Integer = CeGetSystemPowerStatusEx(tmpPowerStatus, ForceUpdate)
    Tmp returns 0 indicating an error.Does rapi.dll expose the function? Do I need to use 

         <DllImport("rapi.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
            Friend Shared Function CeRapiInvoke(ByVal pDllPath As String, ByVal pFunctionName As String, ByVal cbInput As UInteger, ByVal pInput As IntPtr, ByVal pcbOutput As UInteger, ByVal ppOutput As IntPtr, _
       ByVal ppIRAPIStream As IntPtr, ByVal dwReserved As UInteger) As Integer
            End Function
    

    And call GetSystemPowerStatusEx2 that in the core.dll? If so can I get a simple example of loading and receiving the structure for the call

    Thanks...

    Desktop Code:

        Public Enum ACLineStatus As Byte
            AC_LINE_OFFLINE = 0
            AC_LINE_ONLINE = 1
            AC_LINE_BACKUP_POWER = 2
            AC_LINE_UNKNOWN = 255
            Unknown = 255
        End Enum
        Public Enum BatteryFlag As Byte
            BATTERY_FLAG_HIGH = 1
            BATTERY_FLAG_CRITICAL = 4
            BATTERY_FLAG_CHARGING = 8
            BATTERY_FLAG_NO_BATTERY = 128
            BATTERY_FLAG_UNKNOWN = 255
            BATTERY_FLAG_LOW = 2
        End Enum
        Public Enum BatteryChemistry As Byte
            BATTERY_CHEMISTRY_ALKALINE = 1
            BATTERY_CHEMISTRY_NICD = 2
            BATTERY_CHEMISTRY_NIMH = 3
            BATTERY_CHEMISTRY_LION = 4
            BATTERY_CHEMISTRY_LIPOLY = 5
            BATTERY_CHEMISTRY_ZINCAIR = 6
            BATTERY_CHEMISTRY_UNKNOWN = 255
        End Enum
        Public Class SYSTEM_POWER_STATUS_EX
            Public ACLineStatus As ACLineStatus
            Public BatteryFlag As BatteryFlag
            Public BatteryLifePercent As Byte
            Friend Reserved1 As Byte
            Public BatteryLifeTime As Integer
            Public BatteryFullLifeTime As Int32
            Friend Reserved2 As Byte
            Public BackupBatteryFlag As Byte
            Public BackupBatteryLifePercent As Byte
            Friend Reserved3 As Byte
            Public BackupBatteryLifeTime As Integer
            Public BackupBatteryFullLifeTime As Integer
        End Class
    
    
      <DllImport("rapi.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _
            Friend Shared Function CeGetSystemPowerStatusEx(ByVal pStatus As SYSTEM_POWER_STATUS_EX, ByVal fUpdate As Boolean) As Integer
            End Function
    
    
            Public Function GetDeviceSystemPowerStatus(Optional ForceUpdate As Boolean = False) As SYSTEM_POWER_STATUS_EX
                Try
                    Dim tmpPowerStatus As New SYSTEM_POWER_STATUS_EX
                    Dim tmp As Integer = CeGetSystemPowerStatusEx(tmpPowerStatus, ForceUpdate)
                    Return tmpPowerStatus
                Catch generatedExceptionName As Exception
                    Throw New RAPIException("Error retrieving system power status." & NewLine & generatedExceptionName.ToString)
                End Try
            End Function


    Thursday, January 16, 2014 6:10 PM

All replies

  • I found the issue with "Dim tmp As Integer = CeGetSystemPowerStatusEx(tmpPowerStatus, ForceUpdate)"
    returning 0. I forgot to add the classes open com.

    ComConnect(TIMEOUT_SECONDS)
    

    So now the function looks like this:

           Public Function GetDeviceSystemPowerStatus(Optional ForceUpdate As Boolean = False) As SYSTEM_POWER_STATUS_EX
                Try
                    ComConnect(TIMEOUT_SECONDS)
                    Dim tmpPowerStatus As New SYSTEM_POWER_STATUS_EX
                    Dim tmp As Integer = CeGetSystemPowerStatusEx(tmpPowerStatus, ForceUpdate)
                    Return tmpPowerStatus
                Catch generatedExceptionName As Exception
                    Throw New RAPIException("Error retrieving system power status." & NewLine & generatedExceptionName.ToString)
                Finally
                    ForceDisconnect()
                End Try
            End Function

    and tmp is now 1, which I expect it to be the length of the structure. The structure is not filled out either.

    One other note, there is a delete function and it works, so I believe the problem is not a connection issue any more since they call the same connect.

        Public Sub DeleteIfExists(PathName As String, Optional RecursiveDirectory As Boolean = False)
                Try
                    ComConnect(TIMEOUT_SECONDS)
                    Dim attr As UInteger = CeGetFileAttributes(PathName)
                    If attr = UInteger.MaxValue Then
                        Return
                    End If
    
                    If (attr = 16) Then
                        RemoveDeviceDirectory(PathName, RecursiveDirectory)
                        Return
                    End If
    
                    DeleteDeviceFile(PathName)
                Finally
                    ForceDisconnect()
                End Try
            End Sub

     

    Thanks again.


    • Edited by paenright Thursday, January 16, 2014 6:44 PM
    Thursday, January 16, 2014 6:41 PM