访问 DLL 中的函数时调试断言失败错误

Debug Assertion Failed error when accessing function in DLL

我目前正在学习如何创建要在其他项目中引用的 C++ 库,我 运行 遇到了“调试断言失败”错误的问题:is_block_type_valid(header-> _block_use)。我按照此处显示的演练进行操作:Create and use your own Dynamic Link Library。奇怪的是,如果我忽略错误,我会得到预期的答案。

我的DLL目前只有一个功能:

cpp:

int calculate_crc(std::string msg)
{
    std::vector<std::string> msg_vector = [](std::string& msg1) {
        std::string next;
        std::vector<std::string> result;

        // for each char in string
        for (std::string::const_iterator it = msg1.begin(); it != msg1.end(); it++)
        {
            // if we hit a terminal char
            if (*it == ' ')
            {
                if (!next.empty())
                {
                    // add them to the result vector
                    result.push_back(next);
                    next.clear();
                }
            }
            else
            {
                next += *it;
            }
        }
        if (!next.empty())
        {
            result.push_back(next);
        }

        return result;
    } (msg);

    int crcReg = 0xFFFF;

    // iterate through each element in msgVector
    for (auto&& element : msg_vector)
    {
        // step 2: xor operation performed on byte of msg and CRC register
        crcReg ^= [](std::string hex) {
            std::map<char, int> map;

            map['0'] = 0;
            map['1'] = 1;
            map['2'] = 2;
            map['3'] = 3;
            map['4'] = 4;
            map['5'] = 5;
            map['6'] = 6;
            map['7'] = 7;
            map['8'] = 8;
            map['9'] = 9;
            map['a'] = 10;
            map['b'] = 11;
            map['c'] = 12;
            map['d'] = 13;
            map['e'] = 14;
            map['f'] = 15;

            return map[hex[1]] + (map[hex[0]] * 16);
        } (element);

        // step 3-5 are repeated until 8 bit shifts
        for (int i = 0; i < 8; i++)
        {
            int crcCopy = crcReg;
            crcReg >>= 1;

            if ((crcCopy & 1) == 0)
                continue;
            else
                crcReg ^= 0xA001;
        }
    }

    return crcReg;
}

h:

#pragma once

#ifdef OMRONLIBRARY_EXPORTS
#define OMRONLIBRARY_API __declspec(dllexport)
#else
#define OMRONLIBRARY_API __declspec(dllimport)
#endif

#include <iostream>

extern "C" OMRONLIBRARY_API int calculate_crc(const std::string msg);

std::string 不是在 DLL 函数参数中使用的安全类型。非 POD 类型应该 永远不会 通过 DLL 边界,除非它们被类型擦除(例如通过使用 void* 指针)并且只被访问 直接通过代码在边界的一侧而不是另一侧。

假设调用者甚至完全使用 C++(C 风格的 DLL 可以在 non-C/C++ 语言中使用),它可能使用不同的 std::string 实现。或者它可能使用不同的 C++ 编译器,或同一 C++ 编译器的不同版本,甚至只是对齐、优化等的不同设置。即使所有这些都与 DLL 匹配,它也可能使用不同的 [ DLL 用于其 std::string 实现的内存管理器的 =24=] 实例 。

如果要将字符串安全地传递给 DLL 函数,请改用 C 风格 char* 字符串。如果需要,可以在 DLL 中使用 std::string,例如:

int calculate_crc(const char* msg)
{
    use msg as-is ...

    or

    std::string s_msg = msg;
    use s_msg as needed ...
}
extern "C" OMRONLIBRARY_API int calculate_crc(const char* msg);