home *** CD-ROM | disk | FTP | other *** search
- /*
- * Author : Ranveer Chandra
- * Directory: VirtualWiFi_Root\install
- * File Name: renameconn.cpp
- * Purpose: Contains code to rename a connection
- */
-
- #include <windows.h>
- #include <stdio.h>
- #include <strsafe.h>
- #include <setupapi.h>
- #include <netcfgx.h>
- #include <devguid.h>
- #include <netcon.h>
- #include <netcfgn.h>
-
- extern HRESULT HrGetINetCfg (IN BOOL fGetWriteLock,
- IN LPCWSTR lpszAppName,
- OUT INetCfg** ppnc,
- OUT LPWSTR *lpszLockedBy);
-
- extern HRESULT HrReleaseINetCfg (IN INetCfg* pnc,
- IN BOOL fHasWriteLock);
-
-
- extern HRESULT HrGetComponentEnum (INetCfg* pnc,
- IN const GUID* pguidClass,
- OUT IEnumNetCfgComponent **ppencc);
-
- extern HRESULT HrGetFirstComponent (IN IEnumNetCfgComponent* pencc,
- OUT INetCfgComponent **ppncc);
-
- extern bool Verbose;
-
- extern
- VOID RenameConnection ( LPCWSTR ConnectionName )
- {
- INetCfg *pnc;
- IEnumNetCfgComponent *penccAdapter;
- INetCfgComponent *pnccAdapter;
- INetCfgComponent *pnccTrans;
- INetConnectionManager *pConMan;
- IEnumNetConnection *pEnumCon;
- INetConnection *pCon;
- INetConnectionPropertyUi *pConUi;
- GUID guidAdapter;
- CLSID ClsId;
- NETCON_PROPERTIES *pConProps;
- BOOL fLanConn;
- BOOL gotAdapter = FALSE;
- HRESULT hr;
- ULONG count;
-
- hr = HrGetINetCfg( TRUE,
- L"RENAMECONN1",
- &pnc,
- NULL );
-
- if ( hr == S_OK ) {
- hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
-
- if ( hr == S_OK ) {
- hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
- CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
- IID_INetConnectionManager,
- (LPVOID *)&pConMan );
-
- if ( hr == S_OK ) {
- hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
-
- if ( hr == S_OK ) {
-
- while(!gotAdapter) {
- hr = pEnumCon->Next( 1, &pCon, &count );
-
- if (hr == S_OK) {
- hr = pCon->GetProperties( &pConProps );
- if (Verbose) {
- wprintf(L"Device Name is %s\n", pConProps->pszwName);
- wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
- }
- if (( wcsstr( pConProps->pszwName, L"Local Area" ) != NULL )
- && (wcsstr( pConProps->pszwDeviceName, L"VirtualWiFi" )))
- {
- gotAdapter = TRUE;
- if (Verbose)
- printf("Found Adapter\n");
- hr = pCon->Rename(ConnectionName);
- if (hr == S_OK) {
- if (Verbose)
- printf("Rename Successful\n");
- } else {
- printf("Could not rename the connection! \n");
- }
- }
- } else
- break;
- }
- }
- else
- {
- printf("EnumConnections failed.");
- }
- pConMan->Release();
- }
- else
- {
- printf("CoCreateInstance on IID_INetConnectionManager failed.");
- }
- pnccTrans->Release();
- }
- HrReleaseINetCfg( pnc, TRUE );
- }
- else
- {
- printf("HrGetINetCfg failed with %d.", hr);
- }
-
- return;
- }
-
- extern
- BOOL DoesConnectionExist ( LPCWSTR ConnectionName )
- {
- INetCfg *pnc;
- IEnumNetCfgComponent *penccAdapter;
- INetCfgComponent *pnccAdapter;
- INetCfgComponent *pnccTrans;
- INetConnectionManager *pConMan;
- IEnumNetConnection *pEnumCon;
- INetConnection *pCon;
- INetConnectionPropertyUi *pConUi;
- GUID guidAdapter;
- CLSID ClsId;
- NETCON_PROPERTIES *pConProps;
- BOOL flag = FALSE;
- BOOL gotAdapter = FALSE;
- HRESULT hr;
- ULONG count;
- INetConnectionConnectUi* pConnectionUI;
-
-
- hr = HrGetINetCfg( TRUE,
- L"RENAMECONN1",
- &pnc,
- NULL );
-
- if ( hr == S_OK ) {
- hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
-
- if ( hr == S_OK ) {
- hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
- CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
- IID_INetConnectionManager,
- (LPVOID *)&pConMan );
-
- if ( hr == S_OK ) {
- hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
-
- if ( hr == S_OK ) {
-
- while(!gotAdapter) {
- hr = pEnumCon->Next( 1, &pCon, &count );
-
- if (hr == S_OK) {
- hr = pCon->GetProperties( &pConProps );
- if (Verbose) {
- wprintf(L"Device Name is %s\n", pConProps->pszwName);
- wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
- }
- if ( wcsstr( pConProps->pszwName, ConnectionName ) != NULL )
- {
- flag = TRUE;
- break;
- }
- } else
- break;
- }
- }
- else
- {
- printf("EnumConnections failed.");
- }
- pConMan->Release();
- }
- else
- {
- printf("CoCreateInstance on IID_INetConnectionManager failed.");
- }
- pnccTrans->Release();
- }
- HrReleaseINetCfg( pnc, TRUE );
- }
- else
- {
- printf("HrGetINetCfg failed with %d.", hr);
- }
-
- return flag;
- }
-
- extern
- BOOL DoesEnabledConnectionExist ( LPCWSTR ConnectionName )
- {
- INetCfg *pnc;
- IEnumNetCfgComponent *penccAdapter;
- INetCfgComponent *pnccAdapter;
- INetCfgComponent *pnccTrans;
- INetConnectionManager *pConMan;
- IEnumNetConnection *pEnumCon;
- INetConnection *pCon;
- INetConnectionPropertyUi *pConUi;
- GUID guidAdapter;
- CLSID ClsId;
- NETCON_PROPERTIES *pConProps;
- BOOL flag = FALSE;
- BOOL gotAdapter = FALSE;
- HRESULT hr;
- ULONG count;
- INetConnectionConnectUi* pConnectionUI;
-
-
- hr = HrGetINetCfg( TRUE,
- L"RENAMECONN1",
- &pnc,
- NULL );
-
- if ( hr == S_OK ) {
- hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
-
- if ( hr == S_OK ) {
- hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
- CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
- IID_INetConnectionManager,
- (LPVOID *)&pConMan );
-
- if ( hr == S_OK ) {
- hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
-
- if ( hr == S_OK ) {
-
- while(!gotAdapter) {
- hr = pEnumCon->Next( 1, &pCon, &count );
-
- if (hr == S_OK) {
- hr = pCon->GetProperties( &pConProps );
- if (Verbose) {
- wprintf(L"Device Name is %s\n", pConProps->pszwName);
- wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
- }
- if ( wcsstr( pConProps->pszwName, ConnectionName ) != NULL )
- {
- if ( pConProps->Status == NCS_CONNECTED || pConProps->Status == NCS_CONNECTING
- || pConProps->Status == NCS_AUTHENTICATING || pConProps->Status == NCS_AUTHENTICATION_SUCCEEDED )
- flag = TRUE;
- break;
- }
- } else
- break;
- }
- }
- else
- {
- printf("EnumConnections failed.");
- }
- pConMan->Release();
- }
- else
- {
- printf("CoCreateInstance on IID_INetConnectionManager failed.");
- }
- pnccTrans->Release();
- }
- HrReleaseINetCfg( pnc, TRUE );
- }
- else
- {
- printf("HrGetINetCfg failed with %d.", hr);
- }
-
- return flag;
- }
-
- extern
- HRESULT EnableDisableConnection ( LPCWSTR ConnectionName, BOOL enableConnection )
- {
- INetCfg *pnc;
- IEnumNetCfgComponent *penccAdapter;
- INetCfgComponent *pnccAdapter;
- INetCfgComponent *pnccTrans;
- INetConnectionManager *pConMan;
- IEnumNetConnection *pEnumCon;
- INetConnection *pCon;
- INetConnectionPropertyUi *pConUi;
- GUID guidAdapter;
- CLSID ClsId;
- NETCON_PROPERTIES *pConProps;
- BOOL flag = FALSE;
- BOOL gotAdapter = FALSE;
- HRESULT hr;
- ULONG count;
-
-
- hr = HrGetINetCfg( TRUE,
- L"RENAMECONN1",
- &pnc,
- NULL );
-
- if ( hr == S_OK ) {
- hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
-
- if ( hr == S_OK ) {
- hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
- CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
- IID_INetConnectionManager,
- (LPVOID *)&pConMan );
-
- if ( hr == S_OK ) {
- hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
-
- if ( hr == S_OK ) {
-
- while(!gotAdapter) {
- hr = pEnumCon->Next( 1, &pCon, &count );
-
- if (hr == S_OK) {
- hr = pCon->GetProperties( &pConProps );
- if (Verbose) {
- wprintf(L"Device Name is %s\n", pConProps->pszwName);
- wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
- }
- if ( wcsstr( pConProps->pszwName, ConnectionName ) != NULL )
- {
- if(!enableConnection) {
- pCon->Disconnect();
- } else {
- pCon->Connect();
- }
- break;
- }
- } else
- break;
- }
- }
- else
- {
- printf("EnumConnections failed.");
- }
- pConMan->Release();
- }
- else
- {
- printf("CoCreateInstance on IID_INetConnectionManager failed.");
- }
- pnccTrans->Release();
- }
- HrReleaseINetCfg( pnc, TRUE );
- }
- else
- {
- printf("HrGetINetCfg failed with %d.", hr);
- }
-
- return hr;
- }
-
-