How programantically enable file sharing (or 'why doesn't snetcfg.exe work?')
-
Friday, January 05, 2007 7:09 PMHowdy,
I've been trying for a while now to programatically enable file sharing
on Windows XP and Windows 2000 for a local application running under a non-domain controlled network, on a computer which has never been authorized for file sharing before.It appears to me that there is no documented way to share a file or folder unless and until the user manually enables file sharing by using the file manager to either run the network setup wizard, or click the "just enable file sharing" checkbox in response to right clicking the file or folder to be shared in windows file manager and then selecting 'sharing'.
According to Google anyway, the DDK
sample program snetcfg is supposed to do what I want. I've compiled the
DDK version, and also written my own simplified version using ATL.
Neither seems to really work.
Uninstalling with snetcfg.exe works:
snetcfg.exe -v -u MS_Server
But installing starts the server service, but the user still has to use
file manager's user interface to enable file sharing, both on XP and
2000. Neither form of the snetcfg command line works for me:
snetcfg.exe -v -u MS_Server
or the long form
snetcfg.exe -v -l %system%\inf \netserv.inf -c S -i ms_server
I wrote a simplified version of snetcfg that uninstalls ms_server and
then reinstalls it, since some google hits recommend this technique.
In both the ddk sample snetcfg and my simplified version the api calls
all succeed as expected.
I'd appreciate any insights on how to programatically enable file and
printer sharing that anyone might have. An out line of my C++ attempt
follows.
Thanks
Jim Howard
grayraven42 [at] gmail [dot] com
// outline version of a c++ program to uninstall
// and then reinstall ms_server
//uninstall
// Get an INetCfg object
CComPtr<INetCfg> Cfg
HRESULT hr = Cfg.CoCreateInstance(CLSID_CNetCfg, NULL);
//...
// lock the object
hr = Cfg.QueryInterface(&lock);
if (SUCCEEDED(hr))
{ ....
hr = lock->AcquireWriteLock(
c_cmsTimeout,
c_szSampleNetcfgApp,
&szLockedBy);
...
}
// initialize the configuration object
hr = Cfg->Initialize(0);
// do the uninstall
CComPtr<INetCfgComponent> ncc;
const HRESULT HrNcc =
Cfg->FindComponent(A2W(NETCFG_SERVICE_CID_MS_SERVER), &ncc);
// get the configuration class pointer
CComPtr<INetCfgClass> ncClass;
hr = Cfg->QueryNetCfgClass(&GUID_DEVCLASS_NETSERVICE, IID_INetCfgClass,
(void**)&ncClass);
// get the setup pointer
CComPtr<INetCfgClassSetup> ncClassSetup;
ncClass->QueryInterface(IID_INetCfgClassSetup,(void**)&ncClassSetup);
// do the actual uninstall
hr = ncClassSetup->DeInstall (ncc, &m_OboToken, NULL);
// clean up
Cfg->Apply();
Cfg->Uninitialize();
lock->ReleaseWriteLock();
/////////////////////////
//Install MS_Server
//////////////////////
CComPtr<INetCfg> Cfg
CComPtr<INetCfgLock> lock;
// here we obtain and lock net configuration object Cfg exactly as shown
//above
// variables setup here
// copy netserve.inf using the setup api
BOOL bSetup = SetupCopyOEMInf(
path, // usually c:\windows\inf\netserv.inf
NULL, // other files are in the
// same dir. as primary INF
SPOST_PATH, // first param. contains path to INF
0, // default copy style
szInfNameAfterCopy, // receives the name of the INF
// after it is copied to %windir%\inf
MAX_PATH, // max buf. size for the above
NULL, // receives required size if non-null
NULL); // optionally retrieves filename
}
// do the actual install
CComPtr<INetCfgClassSetup> ncClassSetup;
CComPtr<INetCfgComponent> ncc;
HRESULT hr(E_FAIL);
hr = Cfg->QueryNetCfgClass (&GUID_DEVCLASS_NETSERVICE,
IID_INetCfgClassSetup,(void**)&ncClassSetup);
if(SUCCEEDED(hr))
{
hr = ncClassSetup->Install(
A2W(NETCFG_SERVICE_CID_MS_SERVER),
&m_OboToken,NSF_POSTSYSINSTALL,
0, // <upgrade-from-build-num>
NULL, // answerfile name
NULL, // answerfile section name
&ncc);
// clean up
Cfg->Apply();
Cfg->Uninitialize();
lock->ReleaseWriteLock();
}return SUCCEEDED(hr);
All Replies
-
Friday, January 12, 2007 12:42 AM
Change this in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\forceguest=dword:0
that will make it so the user doesn't have to go through the network setup wizard to enable file sharing
-
Friday, January 12, 2007 4:12 PMDid you try to execute a "NET SHARE MyShareName=d:\mySharePath" from your program?
-
Friday, January 12, 2007 4:28 PMNET SHARE will only work AFTER you enable file and print sharing via the registry key or via the wizard. The sharing hand will appear under the folder after a net share, but you won't be able to even see the computer from the network until file and print sharing is enabled.
-
Friday, January 12, 2007 4:51 PMOops -- I figured that out right after I posted the comment. Sorry for misunderstanding.
-
Wednesday, February 07, 2007 9:42 PMjtizzle, Thank you so much! I've been looking for this for a solid year!
-
Sunday, April 15, 2007 4:25 AMSo is there a final solution to enable File and Printer Sharing remotely?
Is there a compiled executable or source that will allow this?
Does this solution work with Windows XP Home as well?
I have a few floating users that use Windows XP Home to access network services and I wanted to be able to use SysInternals PSTools on their systems. I believe that enabling File and Print Sharing enables the default administrative shares necessary for PSTools to work. But I wanted to be able to enable File and Print Sharing remotely.
Can anyone help? -
Tuesday, May 29, 2007 7:58 PMhi ,
Could someone please tell me where i could get snetcfg.exe and some of the command line switches?
Also , can i access file sharing privileges. As in allowing netowork users to change shared files?
I'd really be greateful if anyone helps , there is'nt much about this on the web. -
Monday, January 23, 2012 2:06 PM
Hi everyone. I know it's been a very long time since this thread started, but since I was like all of you trying to find a way to remotely enable/disable "File and Printer sharing" and other binded protocols for a specific network adapter, I thought I could give you the solution I found.
There is now a tool called nvspbind that does just that.
It utilizes the INetCfg API.
It is still a little buggy, and unfortunally the guy who coded it does not seem to work on it anymore, but it is still worth to check it out.

