【问题标题】:WinInet ftp connection error 123WinInet ftp 连接错误 123
【发布时间】:2014-08-10 13:20:17
【问题描述】:

我正在尝试通过 C++/WinInet 连接到我的 ftp 服务器,但我不断收到错误 123 (ERROR_INVALID_NAME)。谷歌研究表明这个错误是由错误的服务器地址引起的,但我的地址是有效的,与我的用户名和密码相同。这是我的代码:

void FileSubmit(LPCWSTR addr, LPCWSTR login, LPCWSTR pass, LPCWSTR pcFile, LPCWSTR ftpFile)
{
    HINTERNET hInternet;
    HINTERNET hFtpSession;
    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, NULL);
    if (hInternet == NULL)
    {
        cout << "Error: " << GetLastError();
    }
    else
    {
        cout<<"Connecting to :"<<(char*)login<<" @ "<<(char*)addr<<", to upload file from PC: "<<(char*)pcFile<<" as "<<(char*)ftpFile<<endl;
        hFtpSession = InternetConnect(hInternet, addr, INTERNET_DEFAULT_FTP_PORT, 
                    login, pass, INTERNET_SERVICE_FTP, NULL, NULL);
        if (hFtpSession == NULL)
        {
            cout<<"FTPSESSION ERROR!\n";
            cout << "Error: " << GetLastError();
        }
        else
        {
            cout<<"hFTPsession successfull!\n";
            if (!FtpPutFile(hFtpSession, pcFile, ftpFile, FTP_TRANSFER_TYPE_BINARY, 0))
            {
                cout << "Error: " << GetLastError();
            }
            else
                cout<<"FILE "<<(char*)pcFile<<" HAS BEEN SUCCESSFULLY UPLOADED AS "<<(char*)ftpFile<<"!\n";
        }
    }
}

我正在使用它:

FileSubmit((LPCWSTR)"ftp.ibieda.cba.pl",(LPCWSTR)"login",
               (LPCWSTR)"pass",(LPCWSTR)"C:\\file.txt",(LPCWSTR)"\\file.txt");

谁能帮忙告诉我为什么 InternetConnect 返回 123 (ERROR_INVALID_NAME)?

【问题讨论】:

  • “ftp.server.x”对我来说听起来不是一个有效的域名。
  • @JonathanPotter 这只是一个例子。
  • 调试器说什么?可能您从 char 字符串重新键入到 LPCWSTR 是万恶之源。重构您的代码,然后重试。
  • @Xearinox 是的,使用 L"address" 而不是 (LPCWSTR)"address" 有效!谢谢!

标签: c++ winapi ftp wininet


【解决方案1】:

您从const char*const wchar* 的类型转换是错误的。传递宽字符串文字(例如,L"ftp.ibieda.cpl.pl")或使用MultiByteToWideChar() 函数。

【讨论】:

    【解决方案2】:

    我使用过去运行良好的程序在 InternetConnect 上遇到错误 123。我的问题是我创建的新项目设置为使用 UNICODE。改变了这一点,一切都恢复了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多