Discussion:
connect/disconnect ActiveSync programmatically
(too old to reply)
NoushadAli
2008-08-14 10:23:02 UTC
Permalink
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
2008-08-14 11:00:21 UTC
Permalink
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
2008-08-14 11:51:01 UTC
Permalink
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 ....
Post by Rik Attrill
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
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
Rik Attrill
2008-08-14 12:59:24 UTC
Permalink
Hi,

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

regards,
Rik
Post by Rahul P. Shukla
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 ....
Rahul P. Shukla
2008-08-14 14:14:00 UTC
Permalink
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
Post by Rik Attrill
Hi,
These are all declared in is "usbfnioctl.h" (C:\WINCE600\PUBLIC\COMMON
\OAK\INC).
regards,
Rik
Post by Rahul P. Shukla
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 ....
NoushadAli
2008-08-14 14:58:07 UTC
Permalink
Thank you Rik.
Your guidance helped us a lot.

Once again Thanks.

Regards,
Noushad
Post by Rahul P. Shukla
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
Post by Rik Attrill
Hi,
These are all declared in is "usbfnioctl.h" (C:\WINCE600\PUBLIC\COMMON
\OAK\INC).
regards,
Rik
Post by Rahul P. Shukla
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 ....
Laxmi
2012-01-13 06:34:54 UTC
Permalink
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
Post by NoushadAli
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
Post by Rahul P. Shukla
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 ....
Post by Rahul P. Shukla
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
Post by NoushadAli
Thank you Rik.
Your guidance helped us a lot.
Once again Thanks.
Regards,
Noushad
Post by r***@pen_fact.com
On Thu, 14 Aug 2008 03:23:02 -0700, NoushadAli
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
Post by Rik Attrill
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
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
Post by Rik Attrill
Hi,
These are all declared in is "usbfnioctl.h" (C:\WINCE600\PUBLIC\COMMON
\OAK\INC).
regards,
Rik
s***@gmail.com
2012-09-17 11:28:25 UTC
Permalink
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***@pen_fact.com
2012-09-17 15:01:08 UTC
Permalink
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.
Post by s***@gmail.com
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

r***@pen_fact.com
2008-08-14 19:06:03 UTC
Permalink
On Thu, 14 Aug 2008 03:23:02 -0700, NoushadAli
Post by NoushadAli
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).
Post by NoushadAli
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
Loading...