Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

connect/disconnect ActiveSync programmatically

584 views
Skip to first unread message

NoushadAli

unread,
Aug 14, 2008, 6:23:02 AM8/14/08
to
Hi,

My Device (WinCE/Pocket PC/Windows Mobile) placed in the dock and connected
to XP desktop through USB. ie, Activesync connection established.
When my application starts in the device, I want to disconnect ActiveSync
from my app (not manual removal of cable). When my app ends ActiveSync
connection should reestablished.
How I can connect/disconnect ActiveSync programmatically.

Thanks for your help in advance.

Regards,
NoushadAli

Rik Attrill

unread,
Aug 14, 2008, 7:00:21 AM8/14/08
to
Hi,

You can certainly use the IOCTL_UFN_CHANGE_CURRENT_CLIENT to
dynamically swap between a mass-storage profile and a serial
(Activesync) one, so I think therefore it should also be possible to
do the same to effectively enable/disable Activesync (by picking a
different client):

http://msdn.microsoft.com/en-us/library/ms895475.aspx

Something like this should work...

HANDLE hUSBFn;
UFN_CLIENT_INFO info;
DWORD dwBytes;

// Open the USB function driver
hUSBFn = CreateFile(USB_FUN_DEV, DEVACCESS_BUSNAMESPACE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if(enableActiveSync)
{
// enable activesync
swprintf(info.szName, _T("Serial_Class"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL))
}
else
{
// enable mass-storage, disable actviesync
swprintf(info.szName, _T("Mass_Storage_Class"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL);
}

best regards,
Rik Attrill

Rahul P. Shukla

unread,
Aug 14, 2008, 7:51:01 AM8/14/08
to
Hey Rik,
I could not found any file containing def of UFN_CLIENT_INFO & USB_FUN_DEV,
DEVACCESS_BUSNAMESPACE and IOCTLs
May I know what file I have to include to get def of all these. I searched
on MSDN and googled also .... but ....

Rik Attrill

unread,
Aug 14, 2008, 8:59:24 AM8/14/08
to
Hi,

These are all declared in is "usbfnioctl.h" (C:\WINCE600\PUBLIC\COMMON
\OAK\INC).

regards,
Rik

Rahul P. Shukla

unread,
Aug 14, 2008, 10:14:00 AM8/14/08
to
Thanks Rik,

Below is the working code: (I have tested it on WM 5.0 and 6.1 device)

#include "winioctl.h"
#include "usbfnioctl.h"
#include "devload.h"

#define USB_FUN_DEV L"UFN1:"

void TogleActiveSync(bool bStatus)
{


HANDLE hUSBFn;
UFN_CLIENT_INFO info;
DWORD dwBytes;

// Open the USB function driver
hUSBFn = CreateFile(USB_FUN_DEV, DEVACCESS_BUSNAMESPACE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if(Status)
{

// enable activesync
swprintf(info.szName, _T("Serial_Class"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT, info.szName,

sizeof(info.szName), NULL, 0, &dwBytes, NULL);

}
else
{
// enable mass-storage, disable actviesync
swprintf(info.szName, _T("Mass_Storage_Class"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT, info.szName,
sizeof(info.szName), NULL, 0, &dwBytes, NULL);
}
}

Regards,
Rahul

NoushadAli

unread,
Aug 14, 2008, 10:58:07 AM8/14/08
to
Thank you Rik.
Your guidance helped us a lot.

Once again Thanks.

Regards,
Noushad

r_z_...@pen_fact.com

unread,
Aug 14, 2008, 3:06:03 PM8/14/08
to
On Thu, 14 Aug 2008 03:23:02 -0700, NoushadAli
<Noush...@discussions.microsoft.com> wrote:

>Hi,
>
>My Device (WinCE/Pocket PC/Windows Mobile) placed in the dock and connected
>to XP desktop through USB. ie, Activesync connection established.
>When my application starts in the device, I want to disconnect ActiveSync
>from my app (not manual removal of cable). When my app ends ActiveSync
>connection should reestablished.
>How I can connect/disconnect ActiveSync programmatically.

I admit I haven't checked in a while, but I'm quite sure repllog.exe
is "ActiveSync" on the device side. I _think_ you can stop it by
"running" it (use CreateProcess function), but I seem to stop it by
finding the window (use FindWindow with windowclassname ActiveSync and
then sending a WM_CLOSE message. I'm quite sure you can restart it by
running it without arguments. My uncertainty is because all this is
used in code I haven't touched in years, and I'm not in touch with all
the users (I'm pretty sure some still use the related feature, and
know I haven't received complaints about it).

>
>Thanks for your help in advance.
>
>Regards,
>NoushadAli

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 478
Boston, MA 02116
www.penfact.com

Laxmi

unread,
Jan 13, 2012, 1:34:54 AM1/13/12
to
Hi ,

Can i disable the USB MAss storage by enabling Srial Class/RNDIS?

If yes , here is code which i have used to disable MASS Storage(USB)
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hUSBFn;

UFN_CLIENT_INFO info;
DWORD dwBytes;
hUSBFn = CreateFile(L"UFN1", DEVACCESS_BUSNAMESPACE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hUSBFn!=NULL)
{
RETAILMSG(TRUE, (TEXT("hUSBFn success\r\n")));
}
else
{
RETAILMSG(TRUE, (TEXT("hUSBFn failed\r\n")));
}

RETAILMSG(TRUE, (TEXT("Enabling Active sync\r\n")));
swprintf(info.szName, _T("RNDIS"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL);

return 0;
}

Have used above code But i am not able to disable USB. What can be changed in above to get successful.

Thanks for your help.

Laxmi



> On Thursday, August 14, 2008 6:23 AM NoushadAl wrote:

> Hi,
>
> My Device (WinCE/Pocket PC/Windows Mobile) placed in the dock and connected
> to XP desktop through USB. ie, Activesync connection established.
> When my application starts in the device, I want to disconnect ActiveSync
> from my app (not manual removal of cable). When my app ends ActiveSync
> connection should reestablished.
> How I can connect/disconnect ActiveSync programmatically.
>
> Thanks for your help in advance.
>
> Regards,
> NoushadAli


>> On Thursday, August 14, 2008 7:51 AM RahulPShukl wrote:

>> Hey Rik,
>> I could not found any file containing def of UFN_CLIENT_INFO & USB_FUN_DEV,
>> DEVACCESS_BUSNAMESPACE and IOCTLs
>> May I know what file I have to include to get def of all these. I searched
>> on MSDN and googled also .... but ....
>>
>>
>> "Rik Attrill" wrote:


>>> On Thursday, August 14, 2008 10:14 AM RahulPShukl wrote:

>>> Thanks Rik,
>>>
>>> Below is the working code: (I have tested it on WM 5.0 and 6.1 device)
>>>
>>>
>>>
>>> void TogleActiveSync(bool bStatus)
>>> {
>>> HANDLE hUSBFn;
>>> UFN_CLIENT_INFO info;
>>> DWORD dwBytes;
>>>
>>> // Open the USB function driver
>>> hUSBFn = CreateFile(USB_FUN_DEV, DEVACCESS_BUSNAMESPACE, 0, NULL,
>>> OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
>>>
>>> if(Status)
>>> {
>>> // enable activesync
>>> swprintf(info.szName, _T("Serial_Class"));
>>> DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT, info.szName,
>>> sizeof(info.szName), NULL, 0, &dwBytes, NULL);
>>> }
>>> else
>>> {
>>> // enable mass-storage, disable actviesync
>>> swprintf(info.szName, _T("Mass_Storage_Class"));
>>> DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT, info.szName,
>>> sizeof(info.szName), NULL, 0, &dwBytes, NULL);
>>> }
>>> }
>>>
>>> Regards,
>>> Rahul
>>>
>>>
>>> "Rik Attrill" wrote:


>>>> On Thursday, August 14, 2008 10:58 AM NoushadAl wrote:

>>>> Thank you Rik.
>>>> Your guidance helped us a lot.
>>>>
>>>> Once again Thanks.
>>>>
>>>> Regards,
>>>> Noushad
>>>>
>>>> "Rahul P. Shukla" wrote:


>>>>> On Thursday, August 14, 2008 3:06 PM r_z_are wrote:

>>>>> On Thu, 14 Aug 2008 03:23:02 -0700, NoushadAli
>>>>> <Noush...@discussions.microsoft.com> wrote:
>>>>>
>>>>>
>>>>> I admit I haven't checked in a while, but I'm quite sure repllog.exe
>>>>> is "ActiveSync" on the device side. I _think_ you can stop it by
>>>>> "running" it (use CreateProcess function), but I seem to stop it by
>>>>> finding the window (use FindWindow with windowclassname ActiveSync and
>>>>> then sending a WM_CLOSE message. I'm quite sure you can restart it by
>>>>> running it without arguments. My uncertainty is because all this is
>>>>> used in code I haven't touched in years, and I'm not in touch with all
>>>>> the users (I'm pretty sure some still use the related feature, and
>>>>> know I haven't received complaints about it).
>>>>>
>>>>>
>>>>> -----------------------------------------
>>>>> To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).
>>>>>
>>>>> Robert E. Zaret, eMVP
>>>>> PenFact, Inc.
>>>>> 20 Park Plaza, Suite 478
>>>>> Boston, MA 02116
>>>>> www.penfact.com


>>>>>> On Saturday, August 16, 2008 9:01 PM Rik Attrill wrote:

>>>>>> Hi,
>>>>>>
>>>>>> You can certainly use the IOCTL_UFN_CHANGE_CURRENT_CLIENT to
>>>>>> dynamically swap between a mass-storage profile and a serial
>>>>>> (Activesync) one, so I think therefore it should also be possible to
>>>>>> do the same to effectively enable/disable Activesync (by picking a
>>>>>> different client):
>>>>>>
>>>>>> http://msdn.microsoft.com/en-us/library/ms895475.aspx
>>>>>>
>>>>>> Something like this should work...
>>>>>>
>>>>>> HANDLE hUSBFn;
>>>>>> UFN_CLIENT_INFO info;
>>>>>> DWORD dwBytes;
>>>>>>
>>>>>> // Open the USB function driver
>>>>>> hUSBFn = CreateFile(USB_FUN_DEV, DEVACCESS_BUSNAMESPACE, 0, NULL,
>>>>>> OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
>>>>>>
>>>>>> if(enableActiveSync)
>>>>>> {
>>>>>> // enable activesync
>>>>>> swprintf(info.szName, _T("Serial_Class"));
>>>>>> DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
>>>>>> info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL))
>>>>>> }
>>>>>> else
>>>>>> {
>>>>>> // enable mass-storage, disable actviesync
>>>>>> swprintf(info.szName, _T("Mass_Storage_Class"));
>>>>>> DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
>>>>>> info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL);
>>>>>> }
>>>>>>
>>>>>> best regards,
>>>>>> Rik Attrill

srival...@gmail.com

unread,
Sep 17, 2012, 7:28:25 AM9/17/12
to Srivalli Vadavalli
The above code surely works to toggle to USB/Serial device. But I have to physically reconnect the cable for the active sync to recongnize my device again.
Can some help if u hv a solution on this. I need to disconnect the USB and recoonect after certain time in the application. And active sync to pick up the connection automatically.

r_z_...@pen_fact.com

unread,
Sep 17, 2012, 11:01:08 AM9/17/12
to
I'm replying just so you know I tried. But, unfortunately, I have
nothing real to add to my earlier post. I will say that ActiveSync can
be challenging, but you probably know that already:-(

Good luck.

On Mon, 17 Sep 2012 04:28:25 -0700 (PDT), srival...@gmail.com
wrote:

>The above code surely works to toggle to USB/Serial device. But I have to physically reconnect the cable for the active sync to recongnize my device again.
>Can some help if u hv a solution on this. I need to disconnect the USB and recoonect after certain time in the application. And active sync to pick up the connection automatically.

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
0 new messages