108 lines
3.8 KiB
C++
108 lines
3.8 KiB
C++
|
|
|||
|
|
|||
|
|
|||
|
#include <cstdio>
|
|||
|
#include <netcon.h>
|
|||
|
#include <atlstr.h>
|
|||
|
|
|||
|
HRESULT shareNet(INetSharingManager *pNSM, const char *srcName, const char *dstName) {
|
|||
|
INetConnection *pNC = nullptr;
|
|||
|
INetSharingConfiguration *pNSC = nullptr;
|
|||
|
IEnumVARIANT *pEV = nullptr;
|
|||
|
IUnknown *pUnk = nullptr;
|
|||
|
INetSharingEveryConnectionCollection *pNSECC = nullptr;
|
|||
|
|
|||
|
HRESULT hr = pNSM->get_EnumEveryConnection(&pNSECC);
|
|||
|
VARIANT v;
|
|||
|
VariantInit(&v);
|
|||
|
|
|||
|
if (!pNSECC) {
|
|||
|
printf(TEXT("failed to get EveryConnectionCollection!\r\n"));
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
hr = pNSECC->get__NewEnum(&pUnk);
|
|||
|
if (pUnk) {
|
|||
|
hr = pUnk->QueryInterface(__uuidof(IEnumVARIANT), (void **)&pEV);
|
|||
|
pUnk->Release();
|
|||
|
}
|
|||
|
|
|||
|
printf(TEXT("----------------------------------------------------------\n"));
|
|||
|
int i = 0;
|
|||
|
while (S_OK == pEV->Next(1, &v, nullptr)) {
|
|||
|
if (V_VT(&v) == VT_UNKNOWN) {
|
|||
|
V_UNKNOWN(&v)->QueryInterface(__uuidof(INetConnection), (void **)&pNC);
|
|||
|
if (pNC) {
|
|||
|
WCHAR guid[256];
|
|||
|
NETCON_PROPERTIES *pNP = nullptr;
|
|||
|
pNC->GetProperties(&pNP);
|
|||
|
//setlocale(LC_ALL, "chs");
|
|||
|
StringFromGUID2(pNP->guidId, guid, 256);
|
|||
|
printf("+++++++++++ %d\n", i++);
|
|||
|
printf(TEXT("guid--%s\n"), static_cast<const char *>(CW2A(guid)));
|
|||
|
printf(TEXT("pszwName--%s\n"), static_cast<const char *>(CW2A(pNP->pszwName)));
|
|||
|
printf(TEXT("pszwDeviceName--%s\n"), static_cast<const char *>(CW2A(pNP->pszwDeviceName)));
|
|||
|
printf(TEXT("Status--%d\n"), pNP->Status);
|
|||
|
printf(TEXT("\n"));
|
|||
|
|
|||
|
//continue;
|
|||
|
if (pNP->Status != NCS_CONNECTED) {
|
|||
|
continue;
|
|||
|
}
|
|||
|
TCHAR* tmpName = CW2A(pNP->pszwName);
|
|||
|
// printf("###### |%s| : |%s|\r\n", tmpName, "");
|
|||
|
|
|||
|
hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC);
|
|||
|
if (!strcmp(tmpName, (char *)srcName)) {
|
|||
|
printf("**************find nic srcName : %s\r\n", (char *)srcName);
|
|||
|
//hr = pNSC->DisableSharing();
|
|||
|
Sleep(500);
|
|||
|
//getchar();
|
|||
|
hr = pNSC->EnableSharing(ICSSHARINGTYPE_PUBLIC);
|
|||
|
}
|
|||
|
pNSC->Release();
|
|||
|
#if 0
|
|||
|
hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC);
|
|||
|
if (!strcmp(tmpName.c_str(), (char *)dstName)) {
|
|||
|
printf("**************find nic dstName : %s\r\n", (char *)dstName);
|
|||
|
//hr = pNSC->DisableSharing();
|
|||
|
Sleep(500);
|
|||
|
hr = pNSC->EnableSharing(ICSSHARINGTYPE_PRIVATE);
|
|||
|
}
|
|||
|
pNSC->Release();
|
|||
|
#endif
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return hr;
|
|||
|
}
|
|||
|
|
|||
|
int NetShare() {
|
|||
|
CoInitialize(nullptr);
|
|||
|
CoInitializeSecurity(nullptr,
|
|||
|
-1,
|
|||
|
nullptr,
|
|||
|
nullptr,
|
|||
|
RPC_C_AUTHN_LEVEL_PKT,
|
|||
|
RPC_C_IMP_LEVEL_IMPERSONATE,
|
|||
|
nullptr,
|
|||
|
EOAC_NONE,
|
|||
|
nullptr);
|
|||
|
|
|||
|
INetSharingManager *pNSM = NULL;
|
|||
|
HRESULT hr = ::CoCreateInstance(__uuidof(NetSharingManager),
|
|||
|
NULL,
|
|||
|
CLSCTX_ALL,
|
|||
|
__uuidof(INetSharingManager),
|
|||
|
(void **)&pNSM);
|
|||
|
|
|||
|
if (!pNSM) {
|
|||
|
printf(TEXT("failed to create NetSharingManager object\r\n"));
|
|||
|
} else {
|
|||
|
//shareNet(pNSM, "WLAN", "мн╠Ф═Э 4");
|
|||
|
shareNet(pNSM, "", "");
|
|||
|
//StartHostednetwork();
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|