运行时检查失败#3 - 正在使用变量'ProfileTypePassed'而未进行初始化

时间:2014-04-21 14:19:42

标签: c++ winapi visual-c++ com wmi

我收到错误运行时检查失败#3 - 变量' ProfileTypePassed'正在使用而未初始化。我的代码如下。它的防火墙api来自msdn。我成功地执行了它,但在这个程序中发生了某种错误。

void        Get_FirewallSettings_PerProfileType(NET_FW_PROFILE_TYPE2 ProfileTypePassed, INetFwPolicy2* pNetFwPolicy2);
HRESULT     WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2);


void Class123 :: Get_FirewallSettings_PerProfileType(NET_FW_PROFILE_TYPE2 ProfileTypePassed, INetFwPolicy2* pNetFwPolicy2)
{
    ProfileTypePassed;
    WFCOMInitialize(&pNetFwPolicy2);
    VARIANT_BOOL bIsEnabled = FALSE;
    NET_FW_ACTION action;
    HRESULT hrComInit = S_OK;
    HRESULT hr = S_OK;

    TiXmlElement* msg31 = new TiXmlElement( "Firwall" );
        TiXmlElement childText0( "Type" );
        TiXmlNode* childNode0;
        CString sTemp;

        hrComInit = CoInitializeEx(
                    0,
                    COINIT_APARTMENTTHREADED
                    );

    // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
    // initialized with a different mode. Since we don't care what the mode is,
    // we'll just use the existing mode.
    if (hrComInit != RPC_E_CHANGED_MODE)
    {
        if (FAILED(hrComInit))
        {
            printf("CoInitializeEx failed: 0x%08lx\n", hrComInit);
            goto Cleanup;
        }
    }
    WFCOMInitialize(&pNetFwPolicy2);
    if(SUCCEEDED(pNetFwPolicy2->get_FirewallEnabled(ProfileTypePassed, &bIsEnabled)))
    {

    sTemp =bIsEnabled ? "enabled" : "disabled";

        childText0.value = "Firwall Status" ;           
        childText0.LinkEndChild(new TiXmlText(GetCharArray(sTemp)));
        childNode0 = msg31->InsertEndChild( childText0 );               
        childText0.Clear();

       // printf ("Firewall is %s\n", );
    }

    if(SUCCEEDED(pNetFwPolicy2->get_BlockAllInboundTraffic(ProfileTypePassed, &bIsEnabled)))
    {sTemp =bIsEnabled ? "enabled" : "disabled";
        childText0.value = "Block all inbound traffic" ;            
        childText0.LinkEndChild(new TiXmlText(GetCharArray(sTemp)));
        childNode0 = msg31->InsertEndChild( childText0 );               
        childText0.Clear();


    }

    if(SUCCEEDED(pNetFwPolicy2->get_NotificationsDisabled(ProfileTypePassed, &bIsEnabled)))
    {
        sTemp =bIsEnabled ? "enabled" : "disabled";
        childText0.value = "Notifications ar" ;         
        childText0.LinkEndChild(new TiXmlText(GetCharArray(sTemp)));
        childNode0 = msg31->InsertEndChild( childText0 );               
        childText0.Clear();

    }

    if(SUCCEEDED(pNetFwPolicy2->get_UnicastResponsesToMulticastBroadcastDisabled(ProfileTypePassed, &bIsEnabled)))
    {
        sTemp =bIsEnabled ? "enabled" : "disabled";
        childText0.value = "UnicastResponsesToMulticastBroadcast is" ;          
        childText0.LinkEndChild(new TiXmlText(GetCharArray(sTemp)));
        childNode0 = msg31->InsertEndChild( childText0 );               
        childText0.Clear();

    }

    if(SUCCEEDED(pNetFwPolicy2->get_DefaultInboundAction(ProfileTypePassed, &action)))
    {
        sTemp =bIsEnabled ? "enabled" : "disabled";
        childText0.value = "Default inbound action is" ;            
        childText0.LinkEndChild(new TiXmlText(GetCharArray(sTemp)));
        childNode0 = msg31->InsertEndChild( childText0 );               
        childText0.Clear();

    }

    if(SUCCEEDED(pNetFwPolicy2->get_DefaultOutboundAction(ProfileTypePassed, &action)))
    {
        sTemp =bIsEnabled ? "enabled" : "disabled";
        childText0.value = "Default outbound action is" ;           
        childText0.LinkEndChild(new TiXmlText(GetCharArray(sTemp)));
        childNode0 = msg31->InsertEndChild( childText0 );               
        childText0.Clear();
    }
    net ->LinkEndChild (msg31);

 Cleanup:

    // Release INetFwPolicy2
    if (pNetFwPolicy2 != NULL)
    {
        pNetFwPolicy2->Release();
    }

    // Uninitialize COM.
    if (SUCCEEDED(hrComInit))
    {
        CoUninitialize();
    }
}


// Instantiate INetFwPolicy2
HRESULT Class123 :: WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2)
{
    HRESULT hr = S_OK;

    hr = CoCreateInstance(
        __uuidof(NetFwPolicy2), 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        __uuidof(INetFwPolicy2), 
        (void**)ppNetFwPolicy2);

    if (FAILED(hr))
    {
        printf("CoCreateInstance for INetFwPolicy2 failed: 0x%08lx\n", hr);
        goto Cleanup;        
    }

Cleanup:
    return hr;
}

void Class123::OnBnClickedBtnWritexml()
{

    Class123 obj_XML;
    obj_XML.InitilizeXMLFile();
    NET_FW_PROFILE_TYPE2 ProfileTypePassed;

INetFwPolicy2 *pNetFwPolicy2 = NULL;
obj_XML.WFCOMInitialize(&pNetFwPolicy2);

    obj_XML.Get_FirewallSettings_PerProfileType((ProfileTypePassed),(pNetFwPolicy2));
    obj_XML.SaveXMLFile();
    AfxMessageBox(_T("Success"));
} 

1 个答案:

答案 0 :(得分:0)

Class123::OnBnClickedBtnWritexml中,简而言之:

NET_FW_PROFILE_TYPE2 ProfileTypePassed;  // (1)
obj_XML.Get_FirewallSettings_PerProfileType((ProfileTypePassed),(pNetFwPolicy2));  // (2)

在(1)处,ProfileTypePassed已定义但未初始化。在(2)中,它在没有首先被初始化的情况下被使用。