0

我向客户提供了一个使用 Visual C++ 开发的应用程序,该应用程序在我的环境中运行良好。_tfopen不幸的是,我的客户在调用时收到错误 22 。

这是一个与我在此应用程序中编写的代码类似的小片段:

#include "pch.h"

#include <iostream>
#include <stdio.h>
#include <wchar.h>
#include "tchar.h"

FILE* fp;

int openFile(const TCHAR* p, const TCHAR* r)
{
    errno_t err = 0;

    fp = _tfopen(p, r);
    if (!fp)
    {
        err = errno;

        _tprintf(_T("Error %d, can't open file: %s with rights %s"), err, p, r);
    }
    else
    {
        printf("All is ok\n");
    }
    return err;

}

int main()
{
    openFile(_T("\\\\127.0.0.1\\hidden_folder$\\folder\\video_file.mxf"), _T("rb"));
    return 0;
}

我的客户得到:

Error 22, can't open file: \\127.0.0.1\hidden_folder$\folder\video_file.mxf with rights rb

错误 22 表示EINVAL参数无效。但是,就我而言,论点是正确的(根据日志)。
这种行为的原因是什么?

我尝试了很多东西来重现这个错误:删除video_file.mxf,更改文件的位置,更改文件的权限,......没关系,我从来没有得到错误22。

笔记:

  • 我知道_tfopen现在已弃用,我还创建了一个使用的版本_tfopen_s,问题仍然存在。
  • 这不是这个问题的重复,因为就我而言,我没有错误并且我想重现它(以了解如何更正它)。
4

1 回答 1

0

我遵循@Michael 的想法,将我的源代码改成了这个:

#include "pch.h"

#include <iostream>
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
#include "tchar.h"

FILE* fp;

int openFile(const TCHAR* p, const TCHAR* r)
{
    errno_t err = 0;

    fp = _tfopen(p, r);
    if (!fp)
    {
        err = errno;

        _tprintf(_T("Error %d, can't open file: %s with rights %s"), err, p, r);

        HANDLE hFile;
        hFile = ::CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

        if (hFile == INVALID_HANDLE_VALUE)
        {
            _tprintf(_T("GetLastError = %d"), GetLastError());
        }
        else
        {
            _tprintf(_T("No error with CreateFile"));
            CloseHandle(hFile);
        }
    }
    else
    {
        printf("All is ok\n");
    }
    return err;

}

int main()
{
    openFile(_T("\\\\127.0.0.1\\hidden_folder$\\folder\\video_file.mxf"), _T("rb"));
    return 0;
}

这个想法是继续用于_tfopen获取FILE指针(在我的应用程序中使用了很多时间),但现在用于CreateFile获取更精确的错误代码。它工作得很好,因为我收到了错误 1326

用户名或密码不正确。

所以,现在,我可以得出结论,_tfopen当它无法登录远程文件服务器时会抛出错误 22。此错误与错误 13(权限被拒绝)不同,因为错误 13 表示用户已正确登录。

于 2019-01-16T15:39:20.327 回答