home *** CD-ROM | disk | FTP | other *** search
- /*
- * Author : Ranveer Chandra
- * Directory: VirtualWiFi_Root\install
- * File Name: addnetwork.cpp
- * Purpose : Has the code to add a VirtualWiFi network
- */
-
- #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 bool Verbose;
-
- extern VOID ReleaseRef (IN IUnknown* punk)
- {
- if ( punk ) {
- punk->Release();
- }
-
- return;
- }
-
- extern HRESULT HrGetINetCfg (IN BOOL fGetWriteLock,
- IN LPCWSTR lpszAppName,
- OUT INetCfg** ppnc,
- OUT LPWSTR *lpszLockedBy)
- {
- INetCfg *pnc = NULL;
- INetCfgLock *pncLock = NULL;
- HRESULT hr = S_OK;
-
- //
- // Initialize the output parameters.
- //
-
- *ppnc = NULL;
-
- if ( lpszLockedBy )
- {
- *lpszLockedBy = NULL;
- }
- //
- // Initialize COM
- //
- CoUninitialize( );
-
- hr = CoInitialize( NULL );
-
- if ( hr == S_OK ) {
-
- //
- // Create the object implementing INetCfg.
- //
-
- hr = CoCreateInstance( CLSID_CNetCfg,
- NULL, CLSCTX_INPROC_SERVER,
- IID_INetCfg,
- (void**)&pnc );
- if ( hr == S_OK ) {
- if(Verbose)
- printf("CoCreateInstance successful\n");
-
- if ( fGetWriteLock ) {
-
- //
- // Get the locking reference
- //
-
- hr = pnc->QueryInterface( IID_INetCfgLock,
- (LPVOID *)&pncLock );
- if ( hr == S_OK ) {
- if(Verbose)
- printf("QueryInterface successful\n");
-
- //
- // Attempt to lock the INetCfg for read/write
- //
-
- hr = pncLock->AcquireWriteLock( 5000,
- lpszAppName,
- lpszLockedBy);
- if (hr == S_FALSE ) {
- printf("Could not acquire write lock\n");
- hr = NETCFG_E_NO_WRITE_LOCK;
- }
- }
- }
-
- if ( hr == S_OK ) {
- if(Verbose)
- printf("Everything fine\n");
- //
- // Initialize the INetCfg object.
- //
-
- hr = pnc->Initialize( NULL );
-
- if ( hr == S_OK ) {
- if(Verbose)
- printf("Initialize successful\n");
- *ppnc = pnc;
- pnc->AddRef();
- }
- else {
-
- //
- // Initialize failed, if obtained lock, release it
- //
-
- if ( pncLock ) {
- pncLock->ReleaseWriteLock();
- }
- }
- }
-
- ReleaseRef( pncLock );
- ReleaseRef( pnc );
- }
- else
- printf("cocreateinstance failed\n");
-
- //
- // In case of error, uninitialize COM.
- //
-
- if ( hr != S_OK ) {
- CoUninitialize();
- }
- }
- else
- printf("CoInitialize Null failed\n");
- return hr;
- }
-
- //
- // Function: HrReleaseINetCfg
- //
- // Purpose: Get a reference to INetCfg.
- //
- // Arguments:
- // pnc [in] Reference to INetCfg to release.
- // fHasWriteLock [in] If TRUE, reference was held with write lock.
- //
- // Returns: S_OK on sucess, otherwise an error code.
- //
- // Notes:
- //
-
- extern HRESULT HrReleaseINetCfg (IN INetCfg* pnc,
- IN BOOL fHasWriteLock)
- {
- INetCfgLock *pncLock = NULL;
- HRESULT hr = S_OK;
-
- //
- // Uninitialize INetCfg
- //
-
- hr = pnc->Uninitialize();
-
- //
- // If write lock is present, unlock it
- //
-
- if ( hr == S_OK && fHasWriteLock ) {
-
- //
- // Get the locking reference
- //
-
- hr = pnc->QueryInterface( IID_INetCfgLock,
- (LPVOID *)&pncLock);
- if ( hr == S_OK ) {
- hr = pncLock->ReleaseWriteLock();
- ReleaseRef( pncLock );
- }
- }
-
- ReleaseRef( pnc );
-
- //
- // Uninitialize COM.
- //
-
- CoUninitialize();
-
- return hr;
- }
-
-
- //
- // Function: HrGetComponentEnum
- //
- // Purpose: Get network component enumerator reference.
- //
- // Arguments:
- // pnc [in] Reference to INetCfg.
- // pguidClass [in] Class GUID of the network component.
- // ppencc [out] Enumerator reference.
- //
- // Returns: S_OK on sucess, otherwise an error code.
- //
- // Notes:
- //
-
- extern HRESULT HrGetComponentEnum (INetCfg* pnc,
- IN const GUID* pguidClass,
- OUT IEnumNetCfgComponent **ppencc)
- {
- INetCfgClass *pncclass;
- HRESULT hr;
-
- *ppencc = NULL;
-
- //
- // Get the class reference.
- //
-
- hr = pnc->QueryNetCfgClass( pguidClass,
- IID_INetCfgClass,
- (PVOID *)&pncclass );
-
- if ( hr == S_OK ) {
-
- //
- // Get the enumerator reference.
- //
-
- hr = pncclass->EnumComponents( ppencc );
-
- //
- // We don't need the class reference any more.
- //
-
- ReleaseRef( pncclass );
- }
-
- return hr;
- }
-
- //
- // Function: HrGetFirstComponent
- //
- // Purpose: Enumerates the first network component.
- //
- // Arguments:
- // pencc [in] Component enumerator reference.
- // ppncc [out] Network component reference.
- //
- // Returns: S_OK on sucess, otherwise an error code.
- //
- // Notes:
- //
-
- extern HRESULT HrGetFirstComponent (IN IEnumNetCfgComponent* pencc,
- OUT INetCfgComponent **ppncc)
- {
- HRESULT hr;
- ULONG ulCount;
-
- *ppncc = NULL;
-
- pencc->Reset();
-
- hr = pencc->Next( 1,
- ppncc,
- &ulCount );
- return hr;
- }
-
- DWORD WINAPI PressEnter( LPVOID lpParam )
- {
- Sleep(500);
- // These two lines imulate enter
- keybd_event(VK_RETURN, 0, 0, 0);
- keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
-
- return 0;
- }
-
- DWORD WINAPI PressDownAndEnter( LPVOID lpParam )
- {
- Sleep(500);
- printf("Pressing down and Enter\n");
- // These two lines emulate the down key
- keybd_event(VK_DOWN, 0, 0, 0);
- keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
- // These two lines emulate enter
- keybd_event(VK_RETURN, 0, 0, 0);
- keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
-
- return 0;
- }
-
- extern
- VOID AddNetwork (HWND hwndDlg, BOOL addNetwork)
- {
- 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;
- DWORD dwThreadId, dwThrdParam=1;
- HANDLE hThread;
-
- hr = HrGetINetCfg( TRUE,
- L"NETCONN",
- &pnc,
- NULL );
-
- if ( hr == S_OK ) {
-
- //
- // Get Component Enumerator Interface.
- //
-
- //hr = HrGetComponentEnum( pnc,
- // &GUID_DEVCLASS_NET,
- // &penccAdapter );
- if ( hr == S_OK ) {
-
- //hr = HrGetFirstComponent( penccAdapter, &pnccAdapter );
-
- if ( hr == S_OK ) {
-
- //hr = pnccAdapter->GetInstanceGuid( &guidAdapter );
-
- 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"Wireless" ) != NULL ) {
- gotAdapter = TRUE;
- if(Verbose)
- printf("Found Adapter\n");
- }
- }
- }
-
- fLanConn = FALSE;
-
- while ( (hr == S_OK) && !fLanConn ) {
-
- hr = pCon->GetProperties( &pConProps );
-
- if ( hr == S_OK ) {
- fLanConn = pConProps->MediaType == NCM_LAN;
- CoTaskMemFree( pConProps );
- }
-
- if ( fLanConn ) {
- hr = pCon->GetUiObjectClassId( &ClsId );
- if ( hr == S_OK ) {
- hr = CoCreateInstance( ClsId, NULL,
- CLSCTX_INPROC_SERVER |
- CLSCTX_NO_CODE_DOWNLOAD,
- IID_INetConnectionPropertyUi,
- (LPVOID *)&pConUi );
- if ( hr == S_OK ) {
- hr = pConUi->SetConnection( pCon );
-
- if(addNetwork) {
- hThread = CreateThread(
- NULL, // no security attributes
- 0, // use default stack size
- PressEnter, // thread function
- &dwThrdParam, // argument to thread function
- 0, // use default creation flags
- &dwThreadId); // returns the thread identifier
- } else {
- hThread = CreateThread(
- NULL, // no security attributes
- 0, // use default stack size
- PressDownAndEnter, // thread function
- &dwThrdParam, // argument to thread function
- 0, // use default creation flags
- &dwThreadId); // returns the thread identifier
- }
-
- // Check the return value for success.
- hr = pnccTrans->RaisePropertyUi( hwndDlg,
- NCRP_SHOW_PROPERTY_UI,
- pConUi
- );
-
-
-
- if (hThread == NULL)
- {
- printf("CreateThread failed." );
- }
- else
- {
- Sleep(1000);
- CloseHandle( hThread );
- }
-
- if ( hr == S_OK ) {
- if(Verbose)
- printf("RaisePropertyUi successful.");
- pnc->Apply();
- }
- else
- {
- printf("RaisePropertyUi failed.");
- }
- pConUi->Release();
- }
- else
- {
- printf("CoCreateInstance on IID_INetConnectionCommonUi failed.");
- }
- }
- else
- {
- printf("GetUiObjectClassId failed.");
- }
- }
- pCon->Release() ;
- hr = pEnumCon->Next( 1, &pCon, &count );
- }
- pEnumCon->Release();
- }
- else
- {
- printf("EnumConnections failed.");
- }
- pConMan->Release();
- }
- else
- {
- printf("CoCreateInstance on IID_INetConnectionManager failed.");
- }
- pnccTrans->Release();
- }
- }
- else
- {
- printf("GetInstanceGuid failed.");
- }
- //pnccAdapter->Release();
- }
- else
- {
- printf("HrGetFirstComponent failed.");
- }
- //penccAdapter->Release();
- }
- else
- {
- printf("HrGetComponentEnum failed.");
- }
- HrReleaseINetCfg( pnc, TRUE );
- }
- else
- {
- printf("HrGetINetCfg failed.");
- }
-
-
- /*
- INPUT input[4] = {INPUT_KEYBOARD, VK_TAB, 0, 0, 0, 0,
- INPUT_KEYBOARD, VK_TAB, 0, KEYEVENTF_KEYUP, 0, 0,
- INPUT_KEYBOARD, VK_RETURN, 0, 0, 0, 0,
- INPUT_KEYBOARD, VK_RETURN, 0, KEYEVENTF_KEYUP, 0, 0};
-
-
- SendInput(4, input, sizeof input[0]);
- */
-
- return;
- }
-
-