Share via


How to enable ICS programmatically

Question

Friday, June 4, 2010 5:14 PM

Hi guys,
hope you can help me with the following.

I am writing a little app that makes sure our device can reach internet through a host PC. Device is USB RNDIS, so you plug the cable into your host PC and you get an endpoint. Host PC has internet. Now in the UI this is trivial - for Vista/Win7: Systemsteuerung\Netzwerk und Internet\Netzwerk- und Freigabecenter (sorry, german localisation, hope you get the point :) ), select the internet endpoint - properties - 'freigabe' tab - select ICS and OK the stuff. This all works well.

Programmatically, I've come as far to have found the Win32 API, and a COM object, that both offer the same API and promise to enable ICS. See http://msdn.microsoft.com/en-us/library/aa364733%28v=VS.85%29.aspx . In code it sort-of looks like:

NetSharingManagerClass^ sharing_manager = gcnew(NetSharingManagerClass);
INetSharingEveryConnectionCollection^ connection_collection = sharing_manager->EnumEveryConnection;
e = connection_collection->GetEnumerator();
..iterate..
INetConnection ^c = safe_cast<INetConnection^>(e->Current);
INetSharingConfiguration ^sc = sharing_manager->INetSharingConfigurationForINetConnection[c];
sc->EnableSharing(tagSHARINGCONNECTIONTYPE::ICSSHARINGTYPE_PRIVATE);
sc->DisableInternetFirewall();

I tried this in Visual C++ 2008 Express, on both Vista and Win7.

now this looks to be working, since I get the 'flag' in the properties box, as if I performed the action myself by hand. However, this is not the case! I can ping and network both ways, but e.g. dhcp is not catching, and neither is NAT happening.

Comparing the manual UI with the programmatical API by looking and various system settings does not give me anything to go by.. services 'SharedAccess' and 'ALG' are active in both scenarios.

I noticed that the API scenario does not change the Internet IPv4 settings but carefully copying IP address etc. did not help.

So, my questions: (1) what is windows doing additionally, (2) can I do that in code too?

Thanks,
Niels

All replies (4)

Tuesday, June 8, 2010 5:43 AM

Hi Niels,

 

I am moving this thread from “Visual C++ General" forum to the “Windows SDK” forum, since the issue is related to ICS API. There are more experts in the “Windows SDK " forum.

 

Cheers

Yi Feng Li

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


Thursday, May 19, 2011 9:08 PM

I am experiencing the exact same thing.  the EnableSharing method checks the ICS box, but it does not actually enable ICS (very tricky and mean).  If I manually toggle the box, ICS starts working.

 

Has anyone found a way to do this on Win7/2008?


Tuesday, June 7, 2011 3:21 PM

I was able to find the solution. You have to enable the public interface on the adapter you are connecting to and enable sharing on the private interface for the adapter you want to be able to use for the network.


Friday, January 11, 2019 11:42 AM

Hi Dizzel,

I am working on this issue for several days but no luck yet. What I have done is: 

1. Enable public sharing for the Wi-Fi adapter, which connects to the internet. EnableSharing(ICSSHARINGTYPE_PUBLIC);

2. Enable private sharing for the Wi-Fi Direct, which I want to connect to the Wi-Fi above and get the internet access. EnableSharing(ICSSHARINGTYPE_PRIVATE); 

However, it does not work for me. Could you please give any further advice?

BTW, my laptop is windows 10, 64 bit. I use VS 2017 and set the platform as x86.