superflexible / TGPuttyLib

An SFTP client shared library (dll/so/dylib) with bindings and classes for C++, Delphi and Free Pascal based on PuTTY
https://www.syncovery.com/tgputtylib/
Other
101 stars 22 forks source link

Using gputtylib.pas and gputtysftp.pas in RADStudio C++ project #28

Open XenoGallois opened 2 years ago

XenoGallois commented 2 years ago

Hi, thanks for the great software. I'm trying to add gputtylib.pas and gputtysftp.pas to a C++ Builder project. I can usually mix PAS and CPP with no problem, but I am getting an unresolved external for each procedure in gputtylib.pas that declared to be in the DLL. here's an example.

procedure tggetstructsizes(const Pulongsize,Pnamesize,Pattrsize,Pnamessize:PInteger); cdecl; external tgputtydll {$ifdef HASDELAYED}delayed{$endif};

I can use the PAS files in a Delphi program with no problem. Any ideas what I can do to keep the linker in C++Builder happy.

As an aside, I have made a C++ project using the C++ specific files, but it crashes when downloading a file from an SFTP server. This is why I'd like to use the PAS files, as they work perfectly in a Delphi app.

superflexible commented 2 years ago

Hello, I think you need to add the import library TGPuttyLibCPP.lib to your project's libraries.

Please see the C++ examples.

XenoGallois commented 2 years ago

Hi Tobias, Thanks for your help. I've spent a bit of time and I've made great progress. I'll post here for other C++ Builder users to find.

I gave up trying to use the PAS files in a C++ Builder project. You can mix CPP and PAS (I do it occasionally) in a C++Builder project, but you can't use the PAS code to call a DLL. It doesn't matter if you include the LIB file and HPP. The PAS code to use functions in the DLL can't be used in a C++Builder project.

I decided to focus my time on working out why the C++ file tgputtylibcbclass.cpp was crashing with no call stack to play with. The main problem was that I didn't have SUPPORTDELPHISTREAMS defined. This is defined in tgputtycbsftpclient.h, but I wasn't including this file as I don't want to use the component. Once I defined SUPPORTDELPHISTREAMS nearly all my problems disappeared.

C++ Builder (10.3.2) doesn't seem to like the thrown exceptions, e.g. throw TTGPuttySFTPException(MakePSFTPErrorMsg("tgsftp_getfile")); I've no idea why this crashes, but it's no problem, as I can just just delete the exception and return an error code.

I never got the CPPBuilderClassDemo.cbproj project to link. There's a lot of unresolved externals related to AnsiString. I expect adding a VCL form to the project would sort it out, but I didn't try.

Now I've spent some time going through the code, I can appreciate how much work you've put into this and what an amazing job you've done. I've thoroughly enjoyed learning about SFTP, Delphi DLL etc. Sorry I didn't spot my obvious mistake of not including SUPPORTDELPHISTREAMS earlier, but I secretly blame C++ Builder as it wasn't letting me step through the code properly. It allowed me to enter the DownloadStream function even without SUPPORTDELPHISTREAMS defined. Builder is weird.

Thanks again for all your hard work and generosity. Amusingly, the project I was going to need SFTP for has now disappeared, but no regrets. I'm sure it will be useful in future.

Cheers, Richard