本文主要是介绍寻找正在连接中的网络连接,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
寻找正在连接中的网络连接,并开启网络连接的网络连接共享功能。
注意:要设置为管理员权限启动工程。否则EnableSharing会失败。
- #include <Windows.h>
- #include <NetCon.h>
- #include <locale>
- #include <stdio.h>
- #pragma comment(lib,"Iphlpapi.lib")
- #pragma comment(lib,"Rpcrt4.lib")//GUID
-
- #pragma comment(lib,"ole32.lib")
-
- int main(int argc, char* argv[])
- {
- INetConnectionManager *pManager=NULL;
- INetConnection *pConnection=NULL;
- IEnumNetConnection *pEnum=NULL;
- ULONG celtFetched;
- INetSharingManager *pNetSharingManager=NULL;
- INetConnectionProps *pProps=NULL;
- INetSharingConfiguration *pConfiguration=NULL;
- NETCON_PROPERTIES* properties=NULL;
- NETCON_MEDIATYPE mediatype;
-
- setlocale(LC_CTYPE, "");
- CoInitialize(NULL);
- CoCreateInstance(CLSID_ConnectionManager, NULL, CLSCTX_SERVER, IID_INetConnectionManager, (void**)&pManager);
- if(pManager == NULL)
- {
- printf("获取接受失败,Error:%d\n",GetLastError());
- return 0;
- }
- pManager->EnumConnections(NCME_DEFAULT, &pEnum);
-
- while( pEnum->Next(1, &pConnection, &celtFetched) == S_OK )
- {
- pConnection->GetProperties(&properties);
-
- if(properties->Status == NCS_CONNECTED)
- {
- if(properties->dwCharacter & NCCF_INCOMING_ONLY == 1)
- {
- wprintf(L"\"%S\"正处于连接状态,但是此连接无法共享!确保至少有2个网络连接!\n",properties->pszwName);
- break;
- }
- CoCreateInstance(CLSID_NetSharingManager, NULL, CLSCTX_SERVER, IID_INetSharingManager, (void**)&pNetSharingManager);
- if(pNetSharingManager == NULL)
- {
- printf("获取接受失败,Error:%d\n",GetLastError());
- break;
- }
- wprintf(L"发现\"%s\"正处于连接状态。尝试开启共享...\n",properties->pszwName);
-
- if(properties->MediaType >= NCM_DIRECT && properties->MediaType <=NCM_PPPOE)
- {
- pNetSharingManager->get_INetSharingConfigurationForINetConnection(pConnection,
- &pConfiguration);
- if(pConfiguration && SUCCEEDED(pConfiguration->EnableSharing(ICSSHARINGTYPE_PUBLIC)))
- {
- wprintf(L"成功设置\"%s\"为共享状态!\n",properties->pszwName);
- break;
- }
- }
- wprintf(L"设置\"%s\"共享状态失败!Error:%d\n",properties->pszwName,GetLastError());
- }
- }
-
-
- if(pManager) pManager->Release();
- if(pConnection) pConnection->Release();
- if(pEnum) pEnum->Release();
- if(pNetSharingManager) pNetSharingManager->Release();
- if(pConfiguration) pConfiguration->Release();
- CoUninitialize();
- return 0;
- }
这篇关于寻找正在连接中的网络连接的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!