Homepage image
Sample of the Day RSS Feed

Sample Download: http://code.msdn.microsoft.com/CppEnableNetBiosOverTCPIP-ed32d818

Today’s code sample illustrates how to set the NetBIOS Over TCP/IP on the server. We are using here WMI’s class Win32_NetworkAdapterConfiguration under ROOT\CimV2 namespace.  This namespace is provided by Microsoft to manage most of the WMI Classes.  This application takes the advantages of WMI classes and executes the method SetTCPIPNetBIOS.

The sample was written by Microsoft Support Escalation Engineer - Shaleen Thapa.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

This sample application demonstrates how to set the NetBIOS Over TCP/IP on the server. We are using here WMI’s class Win32_NetworkAdapterConfiguration under ROOT\CimV2 namespace. 

This namespace is provided by Microsoft to manage most of the WMI Classes.  This application takes the advantages of WMI classes and executes the method SetTCPIPNetBIOS.

To know more about Net BIOS Over TCP IP, please visit http://technet.microsoft.com/en-us/library/bb727013.aspx 

 

Running the Sample

In order to execute this sample code, we need a Server where NIC is IP Enabled. You must execute this application under Administrator privileges as this requires admin privileges to update NIC configuration.

We should know which we are going to pass to the method SetTcpipNetBIOS. This can be found here, http://msdn.microsoft.com/en-us/library/windows/desktop/aa393601(v=vs.85).aspx. In our case, it should be 1 (0x1)  Enable Netbios.

 

Using the Code

Since this is a C++ Application, we need to include certain header files: comdef.h for COM & Wbemidl.h for WMI. In the code, first we need to initialize COM by calling (CoInitializeEx). Once the COM is initialized we need to set the COM general security calling (CoInitializeSecurity). Then we require to get the WMI Locator in order to connect to a WMI service by calling (CoCreateInstance) & (IWbemServices::ConnectServer). While connecting to the WMI service we need to connect to the ROOT\CimV2 namespace. This namespace gives us the facilities to work with the most of the base WMI classes. After we obtain the connection to the WMI service we would require to setting the proxy setting via CoSetProxyBlanket, make sure we use the impersonation level as RPC_C_IMP_LEVEL_IMPERSONATE.

For executing this method SetTCPIPNetBios we need to first get the Network adapter where the IP is enabled. For this we would need to fire a WQL as “Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True” Once we get this record, we need to get the instance of this record by getting the __RELPATH and then we need to execute the method “SetTCPIPNetBIOS”

This takes one parameter: uint32 TcpipNetbiosOptions, which can be:

0 (0x0)   Enable Netbios via DHCP

1 (0x1)   Enable Netbios

2 (0x2)   Disable Netbios

In our case it will be Enable NetBIOS i.e. 0x1.

We will execute the method with the help of IWbemServices::ExecMethod. This method returns the HRESULT and if successful we need to release all the pointers and must exit out gracefully.

More Information

For more information on:

·         Working on WMI via C++:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394558(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/aa389762(v=vs.85).aspx

·         Win32_NetworkConfigurationClass:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394217(v=vs.85).aspx