2008. 9. 5. 06:09

Detect an ActiveSync connection on a device

 

Detecting an ActiveSync connection - rmut

11-Mar-07 06:33:02

I have a .NET CF 2.0 application running on a Windows Mobile 2003 (Pocket PC)

device, and I need to detect an ActiveSync connection.

 

I've looked into IDccMan/IDccManSink but that only works on the desktop side

(correct me if I'm wrong).  Then I developed a service that polls the

isn't very efficient.

 

Is there a way I can have an event raised when the registry key changes?  I

think this is possible in Windows Mobile 5.0, but I don't see a similar

function in the Windows Mobile 2003 API.

 

Otherwise, feel free to suggest a better way to detect an ActiveSync

connection.  I'd like to avoid depending on third-party APIs though.

 

Thanks in advance.

 

Detecting an ActiveSync connection - Peter Foot [MVP]

11-Mar-07 07:58:38

You can use CeRunAppAtEvent with NOTIFICATION_EVENT_ON_AC_POWER or

NOTIFICATION_EVENT_RS232_DETECTED events. See the documentation for having

this trigger a named event then use WaitForSingleObject in a worker thread

in your application.

 

Peter

 

--

Peter Foot

 

Detecting an ActiveSync connection - RaduMotisa

12-Mar-07 12:40:08

rmuti,

 

on 2003 devices the correct aproach is to use RAS to check for the

ActiveSync connection.

 

There is a complete sample here:

http://teksoftco.com/forum/viewtopic.php?t=249 , but you will need to call

native functions from your C# code.

 

Regards,

Radu

 

Detecting an ActiveSync connection - rmut

12-Mar-07 02:14:18

Thanks.  This does what I need.

 

Detecting an ActiveSync connection - rmut

12-Mar-07 02:18:05

Thanks, but I not only want to check for an ActiveSync connection, I want to

be notified when a connection is established.  I want an event-based approach

versus a polling approach.  CeRunAppAtEvent seems to work well, so I think

I'll stick with that.

Determine Active Connection (name and type)

 

bool ConnectedViaActiveSync()//this will work on 2002,2003 and 2003 SE devices only
{
DWORD aConnections = 0,
pConnections = 0;
DWORD dwResult = 0;

pRasEntryName = (RASENTRYNAME*)malloc(sizeof(RASENTRYNAME));
pRasEntryName[0].dwSize = sizeof(RASENTRYNAME);

pRasConn = (RASCONN*)malloc(sizeof(RASCONN));
pRasConn[0].dwSize = sizeof(RASCONN);

cb = sizeof(RASCONN);
if ( RasEnumConnections(pRasConn, &cb, &aConnections) == ERROR_SUCCESS)
{
cb = sizeof(RASENTRYNAME);
if ( RasEnumEntries(NULL, NULL, pRasEntryName, &cb, &pConnections) ==
ERROR_BUFFER_TOO_SMALL)
{
free(pRasEntryName);
pRasEntryName = NULL;
pRasEntryName = (RASENTRYNAME*)malloc(cb*sizeof(BYTE));
pRasEntryName[0].dwSize = sizeof(RASENTRYNAME);
}
if ( RasEnumEntries(NULL, NULL,pRasEntryName, &cb, &pConnections) != ERROR_SUCCESS) {
free(pRasEntryName);
pRasEntryName = NULL;
free(pRasConn);
pRasConn = NULL;
return 0;
}
else
{
if (aConnections)
{
for(UINT possible = 0; possible <(UINT)pConnections; possible++)
for(UINT active = 0; active <(UINT)aConnections; active++)
if (_tcscmp(pRasEntryName[possible].szEntryName, pRasConn[active].szEntryName) == 0)
{
DWORD dwError = 0;
if (RasGetConnectStatus(pRasConn[active].hrasconn, &RasConnStatus) == 0)
if (RasConnStatus.rasconnstate == RASCS_Connected)
{
// Figure out if this is an existing entry or a new one
DWORD dwEntrySize = sizeof(RASENTRY);
RasEntry.dwSize = sizeof(RASENTRY);
dwResult = RasGetEntryProperties(NULL, pRasEntryName[possible].szEntryName, &RasEntry,
&dwEntrySize, pDevConfig, &cbDevConfig);

if (ERROR_CANNOT_FIND_PHONEBOOK_ENTRY == dwResult)
{
free(pRasEntryName);
pRasEntryName = NULL;
free(pRasConn);
pRasConn = NULL;
return 0;//pRasEntryName[possible].szEntryName;
}
else
{
if ((_tcsstr(_T("Serial"),RasEntry.szDeviceName)==0)&&
(_tcscmp(RasEntry.szDeviceType,_T("direct"))==0))
{
//_debugTEK(_T("[GetActiveConnectionName][ActiveSync connected]:%s"),pRasEntryName[possible].szEntryName);
free(pRasEntryName);
pRasEntryName = NULL;
free(pRasConn);
pRasConn = NULL;
return 1;
}
}
}
}
}
}

}
free(pRasEntryName);
pRasEntryName = NULL;
free(pRasConn);
pRasConn = NULL;
return 0;
}

 

 

On WM5 this works to determine if connected with ActiveSync:

Read registry key:
HKLM\System\State\Hardware
Key: Cradled (1 if connected with AS)

 

From :
1. http://www.eggheadcafe.com/software/aspnet/29494492/detecting-an-activesync-c.aspx

2. http://teksoftco.com/forum/viewtopic.php?t=249