簡體   English   中英

在Windows 7 SP1上,我們的C ++ DLL的代碼部分崩潰了

[英]Section of the code of our c++ DLL crashes wen ran on a Windows 7 SP1

我正在開發使用Air Native Extension(ANE)的桌面Air應用程序。 ANE的本機部分僅由部分用C和部分用C ++編寫的DLL組成。 該應用程序是使用Visual Studio 2010編譯的,並且要求MSVCR100.DLL和MSVCP100.DLL與應用程序的exe文件位於同一目錄中。

該應用程序和DLL在許多計算機上均可正常運行,但在干凈的Windows 7 SP1計算機上,其部分代碼會使DLL崩潰。

我將沖突代碼縮小為以下內容:

// Addresses.
String^ defaultGateway = "Not Found";
String^ interfaceIPAddress = "Not Found";
String^ interfaceMask = "Not Found";

array<NetworkInterface^>^nics = NetworkInterface::GetAllNetworkInterfaces();
if (nics != nullptr || nics->Length > 0)
{
    System::Collections::IEnumerator^ myEnum4 = nics->GetEnumerator();
    while (myEnum4->MoveNext())
    {
        NetworkInterface^ adapter = safe_cast<NetworkInterface ^>(myEnum4->Current);
        IPInterfaceProperties^ properties = adapter->GetIPProperties();

        GatewayIPAddressInformationCollection^ gateways = properties->GatewayAddresses;
        for each (GatewayIPAddressInformation^ gateway in gateways)
        {
            if (gateway->Address->AddressFamily == AddressFamily::InterNetwork)
            {
                defaultGateway = gateway->Address->ToString();
                for each (UnicastIPAddressInformation^ unicastIPAddressInformation in properties->UnicastAddresses)
                {
                    if (unicastIPAddressInformation->Address->AddressFamily == AddressFamily::InterNetwork)
                    {
                        interfaceIPAddress = unicastIPAddressInformation->Address->ToString();
                        interfaceMask = unicastIPAddressInformation->IPv4Mask->ToString();
                    }
                }
            }
        }
    }
}

為了給您更多的上下文,我將復制整個函數,其中代碼是:

FREObject MainInterfaceInfo(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
{
    // Addresses.
    String^ defaultGateway = "Not Found";
    String^ interfaceIPAddress = "Not Found";
    String^ interfaceMask = "Not Found";

    array<NetworkInterface^>^nics = NetworkInterface::GetAllNetworkInterfaces();
    if (nics != nullptr || nics->Length > 0)
    {
        System::Collections::IEnumerator^ myEnum4 = nics->GetEnumerator();
        while (myEnum4->MoveNext())
        {
            NetworkInterface^ adapter = safe_cast<NetworkInterface ^>(myEnum4->Current);
            IPInterfaceProperties^ properties = adapter->GetIPProperties();

            GatewayIPAddressInformationCollection^ gateways = properties->GatewayAddresses;
            for each (GatewayIPAddressInformation^ gateway in gateways)
            {
                if (gateway->Address->AddressFamily == AddressFamily::InterNetwork)
                {
                    defaultGateway = gateway->Address->ToString();
                    for each (UnicastIPAddressInformation^ unicastIPAddressInformation in properties->UnicastAddresses)
                    {
                        if (unicastIPAddressInformation->Address->AddressFamily == AddressFamily::InterNetwork)
                        {
                            interfaceIPAddress = unicastIPAddressInformation->Address->ToString();
                            interfaceMask = unicastIPAddressInformation->IPv4Mask->ToString();
                        }
                    }
                }
            }
        }
    }

    String^ result = interfaceIPAddress + ";" + interfaceMask + ";" + defaultGateway;

    // Converting the response to const uint8_t *
    msclr::interop::marshal_context oMarshalContext;
    const char* charResponse = oMarshalContext.marshal_as<const char*>(result);
    const uint8_t * resultNativeCharArray = (const uint8_t *)charResponse;
    uint32_t resultNativeCharArrayLength = 0;

    while (true) {
        if (NULL == resultNativeCharArray[resultNativeCharArrayLength]) {
            break;
        }
        else {
            resultNativeCharArrayLength++;
        }
    }

    FREObject functionResult;
    FRENewObjectFromUTF8(resultNativeCharArrayLength, resultNativeCharArray, &functionResult);

    return functionResult;
}

我是C和C ++的新手,因為10年前我才看過幾次,所以我不知道DLL崩潰的確切原因。 有人可以告訴嗎? 任何建議將不勝感激。

已編輯

我意識到,相同的縮小代碼使該應用程序需要MSVCR100.DLL和MSVCP100.DLL。 如果我刪除了那部分代碼,該應用程序可以在沒有它們的情況下運行。

MSVCP100.DLL包含標准的C ++庫; MSVCR100.DLL是C運行時。 可能像safe_cast調用那樣引入了依賴性。 有關運行時庫的更多詳細信息,請參見此處

我建議使用官方的可再發行組件包來部署Visual C ++運行時DLL,而不要自己將其部署到應用程序的目錄中。 有關詳細信息,請參見重新分發Visual C ++文件 。其中的演練鏈接將對您有所幫助。 這是確保目標系統具有您需要的所有依賴關系的肯定方法。 您的較舊(不干凈)的Win7系統可能安裝了一些其他可重新分發的應用程序或Windows Update,這就是為什么您的代碼在此處運行的原因。

實際上,使一切混亂的代碼行是:

interfaceMask = unicastIPAddressInformation->IPv4Mask->ToString();

如果該行被刪除,則DLL可以正常運行。

發生此問題是因為系統的非活動網絡接口之一在gateway->Address->ToString()上報告了“ 0.0.0.0”。 因此,當嘗試單unicastIPAddressInformation->IPv4Mask->ToString() ,DLL將崩潰。

為了解決這個問題,我剛開始時添加了if (adapter->OperationalStatus == OperationalStatus::Up) ,之后一切正常。 結果代碼如下:

while (myEnum4->MoveNext())
    {
        NetworkInterface^ adapter = safe_cast<NetworkInterface ^>(myEnum4->Current);
        IPInterfaceProperties^ properties = adapter->GetIPProperties();

        if (adapter->OperationalStatus == OperationalStatus::Up)
        {
            GatewayIPAddressInformationCollection^ gateways = properties->GatewayAddresses;
            for each (GatewayIPAddressInformation^ gateway in gateways)
            {
                if (gateway->Address->AddressFamily == AddressFamily::InterNetwork)
                {
                    defaultGateway = gateway->Address->ToString();
                    for each (UnicastIPAddressInformation^ unicastIPAddressInformation in properties->UnicastAddresses)
                    {
                        if (unicastIPAddressInformation->Address->AddressFamily == AddressFamily::InterNetwork)
                        {
                            interfaceIPAddress = unicastIPAddressInformation->Address->ToString();
                            interfaceMask = unicastIPAddressInformation->IPv4Mask->ToString();
                        }
                    }
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM