How to check a PDA is connected to the PC

G

Guest

pHi
I have a program to copy files from desktop pc to PDA for that I
was used OpenNETCF RAPI . It copies the file to the device well.

It gives an exception, when I try to copy the file to device which is not
connected to the PC ( Active sync not installed )

My program hangs, when try to copy the file to device which is not
connected to the PC ( Active sync installed )

There is any way to fix that problem
 
N

Neville Lang

Prakash,

I am using ordinary RAPI and not the one from OpenNETCF though I susepct
these would be the same.

The clue is that CeRapiInit() simply waits for a connection, effectively
"hanging" the program. I have found the best choice is to use the
CeRapiInitEx(). Here is a method that might be useful in that it informs the
user if there is no connection:

public static bool IsDeviceConnected(int MillisecondsToWait)
{
RAPIINIT rapiStructure = new RAPIINIT();
rapiStructure.cbSize = 12;
uint hResult = CeRapiInitEx(ref rapiStructure);
if (hResult == E_FAIL)
{
MessageBox.Show("Unable to initialize connection to device !!");
return false;
}
uint hWait = WaitForSingleObject(rapiStructure.heRapiInit,
MillisecondsToWait);
if (hWait == WAIT_FAILED)
{
MessageBox.Show("Unable to wait for device: " +
Marshal.GetLastWin32Error().ToString());
return false;
}
if (hWait == WAIT_TIMEOUT)
{
MessageBox.Show("Timed out waiting for device !!");
return false;
}
if (rapiStructure.hrRapiInit == E_FAIL)
{
MessageBox.Show("Failed to connect to device !!");
return false;
}
return true;
}


I pass a value of 5000 to this method, allowing 5 seconds to wait if the PPC
is not in the cradle.

Regards,
Neville Lang
 
G

Guest

Thank you very much Neville

can you give the declaration of RAPIINIT,CeRapiInitEx , constant values

regards
prakash
 
N

Neville Lang

Prakash,

Sorry, I should have added those earlier.

public const uint E_FAIL = 0x80004005;
public const uint WAIT_FAILED = 0xffffffff;
public const uint WAIT_TIMEOUT = 0x00000102;

[StructLayout(LayoutKind.Sequential)]
public struct RAPIINIT
{
public uint cbSize;
public uint heRapiInit;
public uint hrRapiInit;
}

[DllImport("rapi.dll", CharSet=CharSet.Unicode)]
public static extern uint CeRapiInitEx([MarshalAs(UnmanagedType.Struct)] ref
RAPIINIT pRapiInit);

[DllImport("kernel32.dll", SetLastError=true)]
public static extern uint WaitForSingleObject(uint hHandle, int
milliseconds);


Regards,
Neville Lang
 
G

Guest

Thank you very much Neville . your codes works very well

I post the VB version of your code
------------------------------------------
Public Structure RAPIINIT
Friend cbSize As Int32
Friend heRapiInit As Int32
Friend hrRapiInit As Int32
End Structure

<System.Runtime.InteropServices.DllImport("rapi.dll",
CharSet:=Runtime.InteropServices.CharSet.Unicode)> _
Public Shared Function
CeRapiInitEx(<System.runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Struct)> ByRef pRapiInit As RAPIINIT) As Integer
End Function

<System.Runtime.InteropServices.DllImport("kernel32.dll",
SetLastError:=True)> _
Public Shared Function WaitForSingleObject(ByVal hHandle As Integer,
ByVal dwMilliseconds As Integer) As Integer
End Function

Public Const E_FAIL = &H80004005
Public Const WAIT_FAILED = &HFFFFFFFF
Public Const WAIT_TIMEOUT = &H102

Public Function IsDeviceConnected(ByVal MilliSecondsToWait As Integer)
As Boolean
Dim rapiStructure As New RAPIINIT
Dim hresult As Integer

rapiStructure.cbSize = 12
hresult = CeRapiInitEx(rapiStructure)

If hresult = E_FAIL Then
MsgBox("Unable to initialize connection to device !!")
Return False
End If

Dim hWait As Integer = WaitForSingleObject(rapiStructure.heRapiInit,
MilliSecondsToWait)

If hWait = WAIT_FAILED Then
MsgBox("Unable to wait for device: " &
System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString())
Return False
ElseIf (hWait = WAIT_TIMEOUT) Then
MessageBox.Show("Timed out waiting for device !!")
Return False
ElseIf (rapiStructure.hrRapiInit = E_FAIL) Then
MessageBox.Show("Failed to connect to device !!")
Return False
End If

Return True
End Function

Regards
prakash
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top