【问题标题】:Issues with Statically linked libraries with CAPL Dll,使用 CAPL Dll 的静态链接库的问题,
【发布时间】:2019-10-31 08:28:25
【问题描述】:

有人可以建议使用不会阻止 CANoe 测量的回调吗?

所以,当我尝试静态链接库并制作 CAPL DLL 时,我遇到了问题。我联系了 VECTOR,但没有可用的支持。

我尝试通过以下方式包含 CAPL Dll:

  • CAPL 网络节点'#pragma library("file path")'
  • “文件>>选项>>编程>>CAPL DLL>>模拟设置/测试设置”
  • “文件>>选项>>编程>>CAPL DLL>>测量设置”
  • CAPL 测试模块使用‘#pragma library("file path")’

我已经使用 Windows 10 64 位机器构建了 32 位的 DLL,我尝试使用 VS2017 和 VS2019。

使用 CPP REST 静态库

我已正确安装 Casablanca SDK 以在此处定义的 Visual Studio 2017 中使用: https://stackoverflow.com/a/44910349/1919873

然后我创建了一个带有静态链接cpprest140_2_5.lib 的 CAPL DLL,没有编译错误,该 DLL 可以在 CAPL 中使用。现在,当我在 CAPL 内的 DLL 中使用 GET request 调用函数时,一切正常,没有任何问题。但是当我用下面提到的POST Request 调用函数时,只要我在request 中添加JSON 数据,我就会得到这个Runtime Kernel error 和错误System Distributed simulation environment: The connection is broken! 然后模拟被破坏并崩溃。我只是在请求中添加Json 时遇到问题,如果我从请求中删除JSON,那么一切正常,我从服务器得到响应400 Bad requests。

web::json::value rest_method_post(uint32 handle,http_client client, web::json::value json_v)
{
    CaplInstanceData* inst = GetCaplInstanceData(handle);
    inst->DllInfo("DLL: rest_method_post:");
    web::json::value json;
    try
    {
        client
            .request(web::http::methods::POST, U("/"), json_v) // Here adding JSON results in Runtime error
            .then([&](const web::http::http_response& response) {
            inst->DllInfo(const_cast<char*>(utility::conversions::to_utf8string(response.to_string().c_str()).c_str()));
            json = response.extract_json().get();
        }).wait();
    }
    catch (exception& e)
    {
        inst->DllInfo("DLL: post Exception occured!");
        inst->DllInfo((char*)e.what());
    }
    return json;
}

我已经创建了一个控制台应用程序,同样的功能也可以正常工作。

使用库 curl

我尝试链接到另一个库以完成工作!不幸的是,这里也存在同样的错误!

我已正确安装 lib curl 7.67.0 以在此处定义的 Visual Studio 2017 中使用: https://stackoverflow.com/a/54680718/1919873

然后我创建了一个带有静态链接 CURL 库的 CAPL DLL,没有编译错误,并且 DLL 可以在 CAPL 中使用。现在,当我在 CAPL 中调用 DLL 中的其他函数时,一切正常,我没有任何问题。但是当我调用下面提到的函数来使用 CURL 库时,一旦它到达 curl_global_init() 或 curl_easy_init(); 我得到这个 Runtime Kernel error 和错误 System Distributed simulation environment: The connection is broken! 然后模拟被破坏并崩溃。

unsigned int CAPLEXPORT far CAPLPASCAL Rest_UsersLogin_new(uint32 handle)
{
    CaplInstanceData* inst = GetCaplInstanceData(handle);
    if (inst == NULL)
    {
        return -1;
    }

    try
    {
        inst->DllInfo("Request POST"); // 

        CURL* curl;
        CURLcode res;

        struct curl_slist* slist1;
        std::string data = "{\"userName\": \"xAS344yn\",\"userPassword\" : \"TjRFWFJQKRUAxyZGQxMjMj\"}";
        curl_global_init(CURL_GLOBAL_DEFAULT); // this step results in Runtime error

        curl = curl_easy_init(); // this step results in Runtime error
        std::string s;
        if (curl)
        {
            slist1 = NULL;
            slist1 = curl_slist_append(slist1, "Content-Type: application/json");

            curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:61000/users/login");

            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https
            curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https
            curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
            curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.38.0");
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
            curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
            curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
            //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //remove this to disable verbose output


            /* Perform the request, res will get the return code */
            res = curl_easy_perform(curl);

            /* Check for errors */
            if (res != CURLE_OK)
            {
                inst->DllInfo((char*)curl_easy_strerror(res));
            }

            /* cleanup */
            curl_easy_cleanup(curl);
        }
        inst->DllInfo(const_cast<char*>(s.c_str()));
        //inst->DllInfo(const_cast<char*>(utility::conversions::to_utf8string(response.to_string().c_str()).c_str()));
    }
    catch (const std::exception & e)
    {
        inst->DllInfo("DLL: post Exception occured!");
        inst->DllInfo((char*)e.what());
    }
    return 1;
}

我已经创建了一个控制台应用程序,并且相同的功能可以正常工作

【问题讨论】:

  • 您可以尝试调试崩溃。将 Visual Studio 的调试器附加到 RuntimeKernel.exe 进程并引发 Crash。观察堆栈跟踪。也许这可以提供线索。
  • 等等,... DLL 是如何静态链接的?不应该是.lib 静态链接和.dll 字面意思是“动态链接库”?

标签: visual-c++ network-programming static-linking capl canoe


【解决方案1】:

请检查你的函数,我犯了一个错误,比如在那个函数中调用了同一个函数。

例子:

Void initialization
{
    initialization();
}

【讨论】:

  • 感谢您抽出宝贵时间尝试提供答案。但是,在这种情况下,问题似乎不是关于涉及递归的运行时问题,而是关于链接/编译行为。我想你可能回答错了问题。
猜你喜欢
  • 2011-05-21
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
相关资源
最近更新 更多