Convert a C# WinSCP program to C++/CLI
-
Please help! Even I am experienced in C++ and C#, I am new to C++/CLI. I have written a simple C# program using WinSCP library. I know how to do it in C# as there is already an example (http://winscp.net/eng/docs/library#csharp[^]) I try to convert but I do not get any success.
#using "WinSCPnet.dll"
using namespace System;int main(array ^args)
{
try
{
Console::WriteLine("Staring...");WinSCP::SessionOptons ^ sftpSessionOptions = gcnew WinSCP::SessionOptions::SessionOptions ( WinSCP::SessionOptions::Protocol = WinSCP::Protocol::Sftp, ... );
...
}But I get this error: Error c2061: syntax error: identifer '{ctor}' why? Please help in the translation. Yes I have made sure it compiles with '/clr' option.
-
Please help! Even I am experienced in C++ and C#, I am new to C++/CLI. I have written a simple C# program using WinSCP library. I know how to do it in C# as there is already an example (http://winscp.net/eng/docs/library#csharp[^]) I try to convert but I do not get any success.
#using "WinSCPnet.dll"
using namespace System;int main(array ^args)
{
try
{
Console::WriteLine("Staring...");WinSCP::SessionOptons ^ sftpSessionOptions = gcnew WinSCP::SessionOptions::SessionOptions ( WinSCP::SessionOptions::Protocol = WinSCP::Protocol::Sftp, ... );
...
}But I get this error: Error c2061: syntax error: identifer '{ctor}' why? Please help in the translation. Yes I have made sure it compiles with '/clr' option.
-
Please help! Even I am experienced in C++ and C#, I am new to C++/CLI. I have written a simple C# program using WinSCP library. I know how to do it in C# as there is already an example (http://winscp.net/eng/docs/library#csharp[^]) I try to convert but I do not get any success.
#using "WinSCPnet.dll"
using namespace System;int main(array ^args)
{
try
{
Console::WriteLine("Staring...");WinSCP::SessionOptons ^ sftpSessionOptions = gcnew WinSCP::SessionOptions::SessionOptions ( WinSCP::SessionOptions::Protocol = WinSCP::Protocol::Sftp, ... );
...
}But I get this error: Error c2061: syntax error: identifer '{ctor}' why? Please help in the translation. Yes I have made sure it compiles with '/clr' option.
Hello,.. Hey i dont know how u willtake my suggestion but , i have an idea for u Use C# to C++ convertor.. it may help you..
int Example::Main()
{
try
{
// Setup session options
SessionOptions ^sessionOptions = gcnew SessionOptions {Protocol = Protocol::Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"};using (Session session = new Session()) Session ^session = gcnew Session(); try { // Connect session->Open(sessionOptions); // Upload files TransferOptions ^transferOptions = gcnew TransferOptions(); transferOptions->TransferMode = TransferMode::Binary; TransferOperationResult ^transferResult; transferResult = session->PutFiles("d:\\\\toupload\\\\\*", "/home/user/", false, transferOptions); // Throw on any error transferResult->Check(); // Print results for each (TransferEventArgs ^transfer in transferResult->Transfers) { Console::WriteLine("Upload of {0} succeeded", transfer->FileName); } } finally { delete session; } return 0; } catch (Exception ^e) { Console::WriteLine("Error: {0}", e); return 1; }
This ur sample C# code to C++/CLI.
-
Hello,.. Hey i dont know how u willtake my suggestion but , i have an idea for u Use C# to C++ convertor.. it may help you..
int Example::Main()
{
try
{
// Setup session options
SessionOptions ^sessionOptions = gcnew SessionOptions {Protocol = Protocol::Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"};using (Session session = new Session()) Session ^session = gcnew Session(); try { // Connect session->Open(sessionOptions); // Upload files TransferOptions ^transferOptions = gcnew TransferOptions(); transferOptions->TransferMode = TransferMode::Binary; TransferOperationResult ^transferResult; transferResult = session->PutFiles("d:\\\\toupload\\\\\*", "/home/user/", false, transferOptions); // Throw on any error transferResult->Check(); // Print results for each (TransferEventArgs ^transfer in transferResult->Transfers) { Console::WriteLine("Upload of {0} succeeded", transfer->FileName); } } finally { delete session; } return 0; } catch (Exception ^e) { Console::WriteLine("Error: {0}", e); return 1; }
This ur sample C# code to C++/CLI.
I try that converter, but when I compile, there are problems. Thanks for the suggestion. :)
-
You are passing parameters to a parameterless constructor; see http://winscp.net/eng/docs/library_sessionoptions#constructors[^]. You need to create the object and then set the properties separately.
Richard, Thanks for your help. I get it. The thing is when I mouse over the new SessionOptions in this line in the C# program,
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Sftp,I see SessionOptions.SessionOptions(). With that in mind, when I do the C++ program, I convert that line to:
WinSCP::SessionOptions ^ sftpOptions = gcnew
WinSCP::SessionOptions::SessionOptions()
sftpOptions->Protocol = WinSCP::Protocol::Sftp;As you can see, that (WinSCP::SessionOptions::SessionOptions()) is not a valid call. I change that to gcnew WinSCP::SessionOptions() and it works! Thanks.