Ich habe einen Code kopiert, den ich online here gefunden habe. Ich habe es erfolgreich ausgeführt, aber wenn ich es in einer Schleife ausführe, gibt es ein schlechtes Speicherleck. Ich programmiere hauptsächlich in C#, und dieses Beispiel geht mir über den Kopf. Könnte mir jemand in die richtige Richtung zeigen, um das Speicherleck zu beheben? Hier ist meine C++ - Konsolenanwendung in ihrer Gesamtheit. Jede Hilfe wird geschätzt. Vielen Dank.Speicherverlust in C++ - Konsolenprogramm Windows-APIs aufrufen
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "Netlistmgr.h"
bool checkForCaptivePortalMode()
{
bool fCaptivePortalDetected = false;
// Initialize COM.
if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
// Declare a pointer to INetworkListManager
INetworkListManager* pNetworkListManager;
// Create instance of the CLSID_NetworkListManger COM object
if (SUCCEEDED(CoCreateInstance(CLSID_NetworkListManager, NULL,
CLSCTX_ALL, IID_INetworkListManager,
(LPVOID*)&pNetworkListManager)))
{
// Declare pointer to an IEnumNetworkConnections
IEnumNetworks* pEnum;
// Call to GetNetworks from INetworkListManager interface
if (SUCCEEDED(pNetworkListManager->GetNetworks
(NLM_ENUM_NETWORK_CONNECTED, &pEnum)) && pEnum != NULL)
{
INetwork *pINetwork;
HRESULT hr = pEnum->Next(1, &pINetwork, nullptr);
while (hr == S_OK)
{
if (pINetwork != NULL)
{
IPropertyBag *pNetworkPropertyBag;
HRESULT hrQueryInterface = pINetwork->QueryInterface
(IID_IPropertyBag, (LPVOID*)&pNetworkPropertyBag);
if (SUCCEEDED(hrQueryInterface) && pNetworkPropertyBag != nullptr)
{
NLM_CONNECTIVITY networkConnectivity;
VARIANT variantConnectivity;
if (SUCCEEDED(pINetwork->GetConnectivity(&networkConnectivity)))
{
if ((networkConnectivity &
NLM_CONNECTIVITY_IPV4_INTERNET) == NLM_CONNECTIVITY_IPV4_INTERNET)
{
VariantInit(&variantConnectivity);
if (SUCCEEDED(pNetworkPropertyBag->Read
(NA_InternetConnectivityV4, &variantConnectivity, nullptr))
&& (V_UINT(&variantConnectivity) &
NLM_INTERNET_CONNECTIVITY_WEBHIJACK) ==
NLM_INTERNET_CONNECTIVITY_WEBHIJACK)
{
fCaptivePortalDetected = true;
}
auto t = V_UINT(&variantConnectivity);
VariantClear(&variantConnectivity);
}
if (!fCaptivePortalDetected && (networkConnectivity
& NLM_CONNECTIVITY_IPV6_INTERNET) == NLM_CONNECTIVITY_IPV6_INTERNET)
{
VariantInit(&variantConnectivity);
if (SUCCEEDED(pNetworkPropertyBag->Read(NA_InternetConnectivityV6,
&variantConnectivity, nullptr)) &&
(V_UINT(&variantConnectivity) &
NLM_INTERNET_CONNECTIVITY_WEBHIJACK) ==
NLM_INTERNET_CONNECTIVITY_WEBHIJACK)
{
fCaptivePortalDetected = true;
}
VariantClear(&variantConnectivity);
}
}
}
pINetwork->Release();
}
if (fCaptivePortalDetected)
break;
hr = hr = pEnum->Next(1, &pINetwork, nullptr);
}
}
}
}
// Uninitialize COM.
// (This should be called on application shutdown.)
CoUninitialize();
return fCaptivePortalDetected;
}
int main()
{
for (;;)
{
bool check = checkForCaptivePortalMode();
std::cout << "\n" << check;
//char c = std::getchar();
}
return 0;
}
Ich werde das untersuchen. Vielen Dank. – user3140383