How programantically enable file sharing (or 'why doesn't snetcfg.exe work?')

Howdy,

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 withsnetcfg.exe works:

snetcfg.exe -v -u MS_Server

But installing starts the server service, butthe 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);
[5277 byte] By [JimInTexas] at [2007-12-28]
# 1

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

jtizzle at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 2
Did you try to execute a "NET SHARE MyShareName=d:\mySharePath" from your program?
erikj at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 3
NET 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.
jtizzle at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 4
Oops -- I figured that out right after I posted the comment. Sorry for misunderstanding.
erikj at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 5
JimInTexas at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 6
So 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?

InfoShare at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...
# 7
hi ,

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.

giddy at 2007-9-4 > top of Msdn Tech,Architecture,Architecture General...

Architecture Hot Topic

Architecture New Topic

Architecture

Site Classified