我们的c ++ DLL崩溃代码的一部分运行在Windows 7 SP1上 [英] Section of the code of our c++ DLL crashes wen ran on a Windows 7 SP1

查看:303
本文介绍了我们的c ++ DLL崩溃代码的一部分运行在Windows 7 SP1上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用空中原生扩展(ANE)的桌面Air应用程序。 ANE的本地部分仅由部分以C编写的DLL和部分以C ++编写的DLL组成。该应用程序是用Visual Studio 2010编译的,并要求MSVCR100.DLL和MSVCP100.DLL在应用程序的exe文件所在的目录。



应用程序和DLL在许多计算机上工作很好,但在干净的Windows 7 SP1计算机上,其代码的一部分使DLL崩溃。



我将冲突代码缩小为以下内容:

  // Addresses。 
String ^ defaultGateway =未找到;
String ^ interfaceIPAddress =未找到;
String ^ interfaceMask =未找到;

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

GatewayIPAddressInformationCollection ^ gateway = properties-> GatewayAddresses;
每个(网关中的GatewayIPAddressInformation ^网关)
{
if(gateway-> Address-> AddressFamily == AddressFamily :: InterNetwork)
{
defaultGateway = gateway-> Address-> ToString();
每个(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 [])
{
//地址。
String ^ defaultGateway =未找到;
String ^ interfaceIPAddress =未找到;
String ^ interfaceMask =未找到;

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

GatewayIPAddressInformationCollection ^ gateway = properties-> GatewayAddresses;
(网关中的GatewayIPAddressInformation ^网关)
{
if(gateway-> Address-> AddressFamily == AddressFamily :: InterNetwork)
{
defaultGateway = gateway-> Address-> ToString();
每个(UnicastIPAddressInformation ^ unicastIPAddressInformation in properties-> UnicastAddresses)
{
if(unicastIPAddressInformation-> Address-> AddressFamily == AddressFamily :: InterNetwork)
{
interfaceIPAddress = unicastIPAddressInformation-> Address-> ToString();
interfaceMask = unicastIPAddressInformation-> IPv4Mask-> ToString();
}
}
}
}
}
}

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

//将响应转换为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 ++的noob,因为我只看到了一对夫妇的时间10年前,所以我不知道什么是什么使DLL崩溃。有人能告诉吗?



我来实现了相同的缩小的代码使应用程序需要MSVCR100.DLL和MSVCP100.DLL。如果我删除那部分代码,应用程序可以运行没有它们。

解决方案

实际上, up is:

  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 ^ gateway = properties-> GatewayAddresses;
(网关中的GatewayIPAddressInformation ^网关)
{
if(gateway-> Address-> AddressFamily == AddressFamily :: InterNetwork)
{
defaultGateway = gateway-> Address-> ToString();
每个(单播IP地址信息在属性 - >单播地址中)
{
if(unicastIPAddressInformation-> Address-> AddressFamily == AddressFamily :: InterNetwork)
{
interfaceIPAddress = unicastIPAddressInformation-> Address-> ToString();
interfaceMask = unicastIPAddressInformation-> IPv4Mask-> ToString();
}
}
}
}
}
}


I am developing a desktop Air application that uses an Air Native Extension (ANE). The native part of the ANE is composed only by a DLL written partially in C and partially in C++. The app was compiled with Visual Studio 2010 and requires the MSVCR100.DLL and the MSVCP100.DLL to be on the same directory as the application's exe file.

The app and DLL work great on many computers but on clean Windows 7 SP1 computers, part of its code makes the DLL crash.

I've narrowed down the conflicting code to the following:

// 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();
                    }
                }
            }
        }
    }
}

Just to give you more context, I'll copy the entire function were that code is:

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;
}

I'm a noob on C and C++ because I only saw it a couple of times 10 years ago so I have no clue about what exactly is making the DLL crash. Can someone tell? Any advice will be more than appreciated.

Editted

I came to realize that the same narrowed code makes the app require the MSVCR100.DLL and the MSVCP100.DLL. If I remove that portion of code, the app can run without them.

解决方案

Actually, the line of code that was messing everything up was:

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

If that line was deleted, the DLL would run without a problem.

The issue occurred because one of the inactive network interfaces of the system reported '0.0.0.0' on gateway->Address->ToString(). So when trying to unicastIPAddressInformation->IPv4Mask->ToString() the DLL would crash.

To solve the issue I just added an if (adapter->OperationalStatus == OperationalStatus::Up) at the beggining and everything worked great afterwards. The resulting code is as follows:

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();
                        }
                    }
                }
            }
        }
    }

这篇关于我们的c ++ DLL崩溃代码的一部分运行在Windows 7 SP1上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆