【问题标题】:Exception running Embarcadero project with libssh2使用 libssh2 运行 Embarcadero 项目时出现异常
【发布时间】:2020-09-02 13:21:31
【问题描述】:

我面临以下问题: 在 Embarcadero 项目(exe 或 dll)中,当我调用在 2 个主机之间建立和 ssh 通信的 libssh2_session_handshake() 时出现异常。 我在 Embarcadero RAD XE5 C++ 中构建了一个 .exe(如果我构建 dll 也会发生同样的情况)。 创建此 .exe 的项目已静态链接 libssh2.lib 库。 当尝试运行 exe 时,它​​会动态加载 libssh2.dll。 libssh2.lib 和 libssh2.dll 是使用 Visual Studio 构建的。我使用了它们的各种版本。最后一个使用 Visual Studio 2015 从 git hub 构建 libssh2 的最后一个代码。

根据我的工作,需要将这些文件合并到 Embarcadero 项目中。

为了使 libssh2.lib 文件在 Embarcadero 中可用,我运行了 Embarcadero 中的 coff2omf.exe 工具,该工具将 .lib 从 Visual Studio 转换为与 embarcadero 兼容。

Embarcadero 项目中的代码在调用libssh2_session_handshake时,不断的抛出异常: “地址 00000000 的访问冲突。读取地址 00000000”。

我遵循示例https://www.libssh2.org/examples/scp.html,它显示了如何将文件发送到 sftp 服务器,首先创建了一个 ssh 连接。

谁能帮忙解决这个问题?你知道什么可能导致它。

感谢您的宝贵时间。

下面我给出我的部分代码。

rc = libssh2_init(0);
ShowMessage("rc = " + IntToStr(rc));
if(rc) {
    fprintf(stderr, "libssh2 initialization failed (%d)\n", rc);
    ShowMessage(AnsiString().sprintf("libssh2 initialization failed (%d)",rc));
    return 1;
}
sock = socket(AF_INET, SOCK_STREAM, 0);

//Connect to Remote Host
    //Prior that the ip of the remote host has been defined (sin)...
if(connect(sock, (struct sockaddr*)(&sin),
           sizeof(struct sockaddr_in)) != 0) {
    int wsaerr = WSAGetLastError();
    ShowMessage("Failed to connect " + IntToStr(wsaerr));
    //return -1;
}

try{
    rc = libssh2_session_handshake(session, sock);
    ShowMessage("libssh2_session_handshake rc: " + IntToStr(rc));
    if(rc) {
        ShowMessage("Failure establishing SSH session: " + IntToStr(rc));
        //return -1;
    }

}
catch(Exception &e){
    ShowMessage("exception raised " + e.Message());
}

【问题讨论】:

  • 作为一般规则,您不能将使用不同编译器构建的对象链接到同一个可执行文件并期望得到工作结果。 C++ 提供no ABI 保证 - only 源代码级别兼容性。那么,您是否使用 (exact) 与项目其余部分相同的编译器构建了 ssh 库代码?

标签: c++ exception libssh2


【解决方案1】:

通常在 Embarcadero c++ Builder 中出现类似的错误消息

"Access violation at address 00000000. Read of address 00000000"

您应该检查您是否正确实例化了您的类或第三方类。例如,您的程序搜索了您的指针,它找不到它,然后它转到地址 0000000。 例如

class MyAgent{ 
    public: MyAgent::MyAgent()=default; 
}

然后在你的代码中

MyAgent *ptMyAgent = new MyAgent();

如果你有

MyAgent *ptMyAgent; // accepted on build but produces errors on run time. 

构建时不会报错,但运行时会在顶部抛出访问冲突等错误。

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多