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

Enabling/disabling network CONNECTIONS.

177 views
Skip to first unread message

Joost van Schaik

unread,
Feb 12, 2003, 12:50:56 PM2/12/03
to
I'd like to enable/disable network connections
programmatically, using C#. Eugene pointed me to
GetIfEntry and SetIfEntry to do the trick.

Unfortunately, that does not work. SetIfEntry seems to
does something to the network INTERFACE, not the
CONNECTION on it. What I would like to see is the effect
of manually disabling the CONNECTION (like right-clicking
on e.g. "Local Area Connection" in "Network Connections")
and then selecting "Disable".

I used SetInEntry to set the dwAdminStatus to 2 for
disable, and although this disables the network
interface, Windows does not seem to notice this. That is,
the "connected" icon stays in my taks bar. Setting
dwOperStatus does not seem to have any effect at all...

Does anyone know how to pull this off?

Regards,

Joost

Eugene Gershnik

unread,
Feb 13, 2003, 1:19:14 AM2/13/03
to
Well I know how to do it in C or C++ but you will have to investigate how to
do it in C#. The reason is that the COM interfaces involved do not have a
type library. AFAIK you have to have one to use PInvoke so you will probably
need to make a typelib from NetCon.h PSDK header. Here is a very quick and
dirty code.

#include <netcon.h>

// wszName is the name of the connection as appears in Network Connections
folder
// set bEnable to true to enable and to false to disable
bool EnableConnection(LPCWSTR wszName, bool bEnable)
{
bool result = false;

typedef void (__stdcall * LPNcFreeNetconProperties) (NETCON_PROPERTIES*
pProps);

HMODULE hmod = LoadLibrary("netshell.dll");
if (!hmod)
return false;
LPNcFreeNetconProperties NcFreeNetconProperties =
(LPNcFreeNetconProperties)GetProcAddress(hmod, "NcFreeNetconProperties");
if (!NcFreeNetconProperties )
return false;

CoInitialize(0);

INetConnectionManager * pMan = 0;
HRESULT hres = CoCreateInstance(CLSID_ConnectionManager, 0,
CLSCTX_ALL, __uuidof(INetConnectionManager), (void**)&pMan);
if (SUCCEEDED(hres))
{
IEnumNetConnection * pEnum = 0;
HRESULT hres = pMan->EnumConnections(NCME_DEFAULT, &pEnum);
if (SUCCEEDED(hres))
{
INetConnection * pCon = 0;
ULONG count;
bool done = false;
while (pEnum->Next(1, &pCon, &count) == S_OK && !done)
{
NETCON_PROPERTIES * pProps = 0;
hres = pCon->GetProperties(&pProps);
if (SUCCEEDED(hres))
{
if (wcscmp(pProps->pszwName,wszName) == 0)
{
if (bEnable)
result = (pCon->Connect() == S_OK);
else
result = (pCon->Disconnect() == S_OK);
done = true;
}
NcFreeNetconProperties(pProps);
}
pCon->Release();
}
pEnum->Release();
}
pMan->Release();
}

CoUninitialize();
FreeLibrary(hmod);
return result;
}

int main()
{
EnableConnection(L"Local Area Connection", false);
}

"Joost van Schaik" <jjc.van...@12move.nl> wrote in message
news:05b801c2d2bf$4b5c93a0$3001...@phx.gbl...

joost van schaik

unread,
Feb 13, 2003, 3:36:05 AM2/13/03
to
This looks like it should work - the only problem is that
your understanding of C and the SDK obviously goes way
beyond mine :-) . I can understand only about half of what
you are doing. The first part (up to the first "if
(SUCCEEDED(hres))" is a total mistery to me - my best
guess is that you are retrieving a function pointer. So
basically, what is happening here is that a mere magician
is looking at advanced sourcery and wonders what to do
with it :-)

Uhm - you don't happen to have a may to enable/disable
connections by means of command line keyins ... :-)

CU

Joost

>-----Original Message-----
>Well I know how to do it in C or C++ but you (..) Here is

a very quick and
>dirty code.

(code snipped)

Eugene Gershnik

unread,
Feb 13, 2003, 11:03:18 AM2/13/03
to
The first part is indeed to get a function pointer to NcFreeNetconProperties
implemented in netshell.dll. This is precisely what C# is doing behind the
scenes when you use [DllImport("netshell.dll")]. This "sourcery" is needed
since MS doesn't provide an import library (netshell.lib) for this dll. The
function itself and what it does is documented in MSDN.
Regarding a command line tool you can just compile the code I've sent to a
console aplication :-) Make some small modification to main() so it will get
the arguments from command line, add some error reporting and you are done.

Eugene

"joost van schaik" <jjc.van...@12move.nl> wrote in message
news:04d701c2d33a$f2887bf0$a601...@phx.gbl...

Joost van schaik

unread,
Feb 13, 2003, 1:59:17 PM2/13/03
to
My problem is this code:

INetConnectionManager * pMan = 0;
HRESULT hres = CoCreateInstance(CLSID_ConnectionManager,
0,
CLSCTX_ALL, __uuidof(INetConnectionManager), (void**)
&pMan);
if (SUCCEEDED(hres))

What in blazes does it do? It reminds of an Activator
construction in C#, creating an instance of a class by
name rather than by type. Am I correct

Eugene Gershnik

unread,
Feb 14, 2003, 3:04:35 AM2/14/03
to
Yes the .NET Activator is a stripped down version of COM object activation.
For details see Component Development/Component Object Model (General) in
newer MSDNs. Also "Essential COM" by Don Box (ISBN: 0201634465) may be
helpfull. The __uuidof construct is fully documented in MSDN. It is IMHO a
rare case of genuinely usefull C++ extension by MS. :-)

Eugene


"Joost van schaik" <jjc.van...@12move.nl> wrote in message
news:018b01c2d392$0251b3b0$a301...@phx.gbl...

D.I.

unread,
Mar 3, 2003, 5:44:43 AM3/3/03
to

> The reason is that the COM interfaces involved do not have a
> type library

Hi, i found in Platform SDK July 2002 the file netcon.idl, i modified it
and i put into it the lines:

[
uuid(5C63C1AD-3956-4FF8-8486-AAAAAAAAAAAA),
helpstring("NetConnectionManager Class")
]
coclass NetConnectionManager
{
[default] interface INetConnectionManager;
};

i put a random uid, i don't know if this is the error.

Now i've the type library but when i try to execute the lines (visual
basic):

Dim a As New NETCONLib.NetConnectionManager
Dim pp As NETCONLib.IEnumNetConnection

a.EnumConnections NCME_DEFAULT, pp

they return me the error :

Run-time error '429':
ActiveX component can't create object

but, may i use the netshell.dll in this way with Windows 2000 pro?
must i put a correct uid? if yes, where can i find it?

Thank you very much for your help.


Ivan Brugiolo [MSFT]

unread,
Mar 3, 2003, 11:32:27 AM3/3/03
to
CLSID_ConnectionManager = _GUID {ba126ad1-2166-11d1-b1d0-00805fc1270e}

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"D.I." <X@X.x> wrote in message news:b3vbmk$oma$1...@news.flashnet.it...

D.I.

unread,
Mar 4, 2003, 3:30:20 AM3/4/03
to

"Ivan Brugiolo [MSFT]" <ivan...@online.microsoft.com> ha scritto nel
messaggio news:#6lD3Ja4...@TK2MSFTNGP11.phx.gbl...
> CLSID_ConnectionManager = _GUID {ba126ad1-2166-11d1-b1d0-00805fc1270e}


Thank you very very very very very much, now i can disable and enable a lan
connection from visual basic, but i've still a little problem, i can't use
INetConnection.GetProperties, the visual basic compiler tell me that could
be a restriction to use it or could be a parameter not supported by visual
basic. In fact i had to modify the struct tagNETCON_PROPERTIES in the IDL.
These are my structs :

typedef struct tagGUID {
LONG Data1;
LONG Data2;
LONG Data3;
BYTE Data4[8];
} GUID;

typedef struct tagNETCON_PROPERTIES
{
GUID guidId;
[string] BSTR pszwName;
[string] BSTR pszwDeviceName;
NETCON_STATUS Status;
NETCON_MEDIATYPE MediaType;
LONG dwCharacter;
GUID clsidThisObject;
GUID clsidUiObject;
} NETCON_PROPERTIES;

and the method is defined in this way :

HRESULT GetProperties(
[out] NETCON_PROPERTIES** ppProps);

How can i resolve the problem?

Ivan Brugiolo [MSFT]

unread,
Mar 4, 2003, 10:39:37 AM3/4/03
to
That structure has a MIDL generate marshaler,
that is not the interpreted marshaler driven by oleaut32 and based on the
Type Librbary.
I guess that VB is not capable of marshaling correcly that param based
on the IDL you have generated (or any oleauto-compatible IDL, for what
matters)
Moreover, that structure needs to be free-ed explicitely
by a routine exported by the marshaler DLL itself,
in order to prevent memory leaks.
You have run far enough with VB.

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of any included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"D.I." <X@X.x> wrote in message news:b41o6m$e42$1...@news.flashnet.it...

Yong Qu [MS]

unread,
Mar 4, 2003, 1:54:06 PM3/4/03
to
Are you using latest versions of VB? UDT (user defined type) is supported by
VB for a while. I am not familiar with the exact usage in VB though. You
might try microsoft.public.win32.programmer.ole. Someone there might be able
to help you.

thanks.
--
Yong Qu
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.

You assume all risk for your use.

"D.I." <X@X.x> wrote in message news:b41o6m$e42$1...@news.flashnet.it...

Eugene Gershnik

unread,
Mar 4, 2003, 2:26:12 PM3/4/03
to
I think your problem is GUID which VB cannot handle (I may be wrong, haven't
touched this stuff for a long time). Write a C++ wrapper that will present a
VB firendly interface. In particualr conver GUIDs to strings.

Eugene

"D.I." <X@X.x> wrote in message news:b41o6m$e42$1...@news.flashnet.it...

D.I.

unread,
Mar 5, 2003, 2:29:19 AM3/5/03
to
Thank you very much for your answer, i saw that the problem is the double
pointer to NETCON_PROPERTIES struct, only it (if i change it in LONG** or in
NETCON_PROPERTIES* all is good, only for the vb compiler of course). I tried
to modify it and the vb compiler didn't screem, but of course the VB struct
was empty after the call to the method (all without errors), i saw that only
a parameter of it was full, but with the address of the variable.
But i can use Enable and Disable without problem (i had to change all ULONG,
DWORD and WORD in LONG), i can be happy, for me it isn't important read the
properties. Maybe that i'll do a wrapper dll only for hobby :-), or for my
personal use.
0 new messages