1

I face the following problem: In Embarcadero project (either exe or dll) there is an exception when I call the libssh2_session_handshake() that establishes and ssh communication between 2 hosts. I have built an .exe in Embarcadero RAD XE5 C++ (the same occurs if I build dll). The project that creates this .exe, has linked statically the libssh2.lib library. When trying to run the exe it loads dynamically the libssh2.dll. The libssh2.lib and the libssh2.dll are built with Visual Studio. I have used various versions of them. The last one built the last code of libssh2 from git hub, with Visual Studio 2015.

From my job it is required these files to be incorporated to Embarcadero project.

In order the libssh2.lib file to be workable in Embarcadero I run the coff2omf.exe tool found in Embarcadero, that converts a .lib from Visual Studio to compatible to embarcadero.

When the code in Embarcadero project calls the libssh2_session_handshake, it constantly raises an exception: "Access violation at address 00000000. Read of address 00000000".

I follow the example https://www.libssh2.org/examples/scp.html, which shows how to send a file to an sftp server, having first created an ssh conection.

Could anyone help to fix that? Do you have any idea what may cause it.

Thank you for you time.

Below I give a part of my code.

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());
}
  • As a general rule you cannot link objects built with different compilers into the same executable and expect working results. C++ provides *no* ABI guarantees - *only* source level compatibility. So, did you build your ssh library code with the (*exact*) same compiler as the rest of the project? – Jesper Juhl May 16 '20 at 17:32

1 Answers1

0

Usually in Embarcadero c++ Builder when you have error messages like

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

you should check to see if you have instantiated correctly your classes or third-party classes. Your program searched for your pointer for example, it can not find it, and then it goes to address 0000000. in example

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

Then in your code

MyAgent *ptMyAgent = new MyAgent();

IF you had

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

It won't complain when building but will throw an error like the access violation at the top at runtime.