1

我有一个奇怪的问题,在调试模式下这段代码工作正常:

char* PCInformation::GetCPUName()
{
    if (CPUName[0] == '\0')
    {
        _memset(CPUName, 0, 0x3F);
        // Get extended ids.
        int CPUInfo[4] = {-1};
        __cpuid(CPUInfo, 0x80000000);
        unsigned int nExIds = CPUInfo[0];

        printf(3, "%d\n%d\n", nExIds, 0x80000000);

        // Get the information associated with each extended ID.
        for(unsigned int i=0x80000000; i<=nExIds; ++i)
        {
            printf(3, "0x80000000 nExIds: %X, i: %X\n", nExIds, i);
            getchar();
            __cpuid(CPUInfo, i);

            // Interpret CPU brand string and cache information.
            if  (i == 0x80000002)
            {
                _memcpy( CPUName,
                CPUInfo,
                sizeof(CPUInfo));
            }
            else if( i == 0x80000003 )
            {
                _memcpy( CPUName + 16,
                CPUInfo,
                sizeof(CPUInfo));
            }
            else if( i == 0x80000004 )
            {
                _memcpy(CPUName + 32, CPUInfo, sizeof(CPUInfo));
            }
        }
    }

    return CPUName;
}

来源:http ://weseetips.com/2009/06/21/how-to-get-the-cpu-name-string/

编辑:( 这就是 _functions 的样子)

BOOL CallDynamic(const char* szModule, const char* szFunction, DWORD* dwReturn, size_t argc, ... );
#define printf(argc, ... ) (CallDynamic(MSVCRT, "printf", NULL, argc, __VA_ARGS__ )) //number arguments, arguments

如果您想知道为什么 STD-C 函数有 _,那是因为它们是动态调用的,但这不是问题。(因为以前的代码可以正常工作,但我把它放在一个类中,然后 bahm!)

无论如何,如果我选择 Release (as Build Mode) (int) 我包含一些奇怪的值或其他东西。更多声明一些图片:

发布 发布构建

调试 调试构建 在发行版中,它以无限的 for 循环结束。

编辑:我刚刚将代码更改为:

char* PCInformation::GetCPUName()
{
    if (CPUName[0] == '\0')
    {
        _memset(CPUName, 0, 0x3F);

        int CPUInfo[4];
        _memset(CPUInfo, 0, 4);

        __cpuid(CPUInfo, 0x80000000);
        _memcpy(CPUName, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000002);
        _memcpy(CPUName + 16, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000003);
        _memcpy(CPUName + 32, CPUInfo, sizeof(CPUInfo));

        __cpuid(CPUInfo, 0x80000004);
        _memcpy(CPUName + 48, CPUInfo, sizeof(CPUInfo));
    }

    return CPUName;
}

尽管如此,我仍然想知道是什么原因造成的。

问候,

4

0 回答 0