Click here to Skip to main content
13,250,182 members (68,628 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 26 Sep 2008


, 26 Sep 2008
Rate this:
Please Sign up or sign in to vote.
This article explains how to use the DDK interface-INetCfg in C#.


NetCfgTest uses the DDK interface INetCfg and related interfaces to fetch a network adapter list (installed on the system). This article describes:

  1. How to write interop code for pure COM interfaces.
  2. How to use INetCfg in C# to fetch installed network adapters.
  3. How to initialize a pure COM class from C#.


There was a requirement to use the interface INetCfg in C#. I struggled to get a COM interop for this interface. Finally, I decided to write one.

Using the Code

The interface INetCfg is written in the namespace NetCfg. We can use the following source code to fetch an adapters list.:

object objINetCfg = null;
int nRet = 0;
nRet = Ole32Methods.CoCreateInstance(ref INetCfg_Guid.CLSID_CNetCfg, null,
    Ole32Methods.CLSCTX_INPROC_SERVER, ref INetCfg_Guid.IID_INetCfg, out objINetCfg);
INetCfg netCfg = objINetCfg as INetCfg;
nRet = netCfg.Initialize(IntPtr.Zero);
object componet = new object();
nRet = netCfg.QueryNetCfgClass(ref INetCfg_Guid.IID_DevClassNet,
    ref INetCfg_Guid.IID_INetCfgClass, out componet);
INetCfgClass NetCfgClass = componet as INetCfgClass;
object EnumNetCfgComponentObj = new object();
nRet = NetCfgClass.EnumComponents(out EnumNetCfgComponentObj);

IEnumNetCfgComponent EnumNetCfgComponent =
    EnumNetCfgComponentObj as IEnumNetCfgComponent;
object netCfgCompObj = new object();
INetCfgComponent netcfgcomp = null;
int outCelt = 0;
nRet = EnumNetCfgComponent.Next(1, out netCfgCompObj, out outCelt);
netcfgcomp = netCfgCompObj as INetCfgComponent;
string strBind;
string strDispName = "";
    netcfgcomp.GetBindName(out strBind);
    netcfgcomp.GetDisplayName(out strDispName);
    netCfgCompObj = null;
    this.listBox1.Items.Add(strDispName+" - " + strBind);
    nRet = EnumNetCfgComponent.Next(1, out netCfgCompObj, out outCelt);
    netcfgcomp = netCfgCompObj as INetCfgComponent;
} while (nRet == 0);

EnumNetCfgComponent = null;
objINetCfg = null;

Points of Interest

I wrote interop for a pure COM interface for the first time. It was really interesting to learn the nitty-gritty of how to use a pure COM interface in C#.


  • Sep. 26, 2008: Created version 1.0.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

ram verma
Software Developer (Senior)
India India
I'm in software industry for over 10 years.

You may also be interested in...

Comments and Discussions

QuestionGetting exception HRESULT: 0x800106D9 Pin
sudhi16421-Jun-16 21:11
membersudhi16421-Jun-16 21:11 
GeneralDriver Installation Through a *.INF file [In VB.NET] [modified] Pin
emicroxxx4-Oct-10 1:41
memberemicroxxx4-Oct-10 1:41 
GeneralUsing INetCfgClassSetup Install, another null reference! Pin
Trevor Ackerman5-Apr-09 20:31
memberTrevor Ackerman5-Apr-09 20:31 
QuestionINetCfgLock AcquireWriteLock NullReference Exception - Help! Pin
pfrances31-Jan-09 12:30
memberpfrances31-Jan-09 12:30 
QuestionRe: INetCfgLock AcquireWriteLock NullReference Exception - Help! Pin
Bruins6-Jul-09 1:47
memberBruins6-Jul-09 1:47 
AnswerRe: INetCfgLock AcquireWriteLock NullReference Exception - Help! Pin
Member 28749596-Jul-09 4:41
memberMember 28749596-Jul-09 4:41 
GeneralRe: INetCfgLock AcquireWriteLock NullReference Exception - Help! Pin
matt4274816-Nov-09 11:48
membermatt4274816-Nov-09 11:48 
RantCode dump Pin
psouza4micronet26-Sep-08 8:18
memberpsouza4micronet26-Sep-08 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 26 Sep 2008
Article Copyright 2008 by ram verma
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid