Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当文件句柄号已知时,如何打开文件?_C#_C++_Interop - Fatal编程技术网

C# 当文件句柄号已知时,如何打开文件?

C# 当文件句柄号已知时,如何打开文件?,c#,c++,interop,C#,C++,Interop,我用FileStream在C#中打开了一个文件,我用这行代码得到了文件句柄号: IntPtr file_handle = fs.SafeFileHandle.DangerousGetHandle(); 现在我想把这个句柄传递给C++代码,并使用这个句柄值访问文件。这可能吗?如何在C++中使用文件句柄打开文件? 谢谢 更新 我使用C++来调用/调用到C++ Win32 DLL(不是COM DLL)。我将文件以C文件的形式打开,并将句柄传递给C++。下面是我在C++ DLL中的一些代码: exte

我用FileStream在C#中打开了一个文件,我用这行代码得到了文件句柄号:

IntPtr file_handle = fs.SafeFileHandle.DangerousGetHandle();

现在我想把这个句柄传递给C++代码,并使用这个句柄值访问文件。这可能吗?如何在C++中使用文件句柄打开文件? 谢谢

更新

我使用C++来调用/调用到C++ Win32 DLL(不是COM DLL)。我将文件以C文件的形式打开,并将句柄传递给C++。下面是我在C++ DLL中的一些代码:

extern "C" __declspec(dllexport)void read_file(HANDLE file_handle)
{
    char buffer[64];
    ::printf("\nfile = %d\n",file_handle);

    if(::ReadFile(file_handle,buffer,32,NULL,NULL))
    {
        for(int i=0;i<32;i++)
            cout<<buffer[i]<<endl;
    }
    else
        cout<<"error"<<endl;
}
但我得到了这个错误:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.

谢谢。

您可以使用win32调用,与filestream/文件构造函数(通过p/invoke)一样

将其在中打开,看起来它正在使用此功能:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern SafeFileHandle CreateFile(
  string lpFileName,
  int dwDesiredAccess,
  FileShare dwShareMode,
  SECURITY_ATTRIBUTES securityAttrs,
  FileMode dwCreationDisposition,
  int dwFlagsAndAttributes,
  IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern unsafe int ReadFile(
  SafeFileHandle handle,
  byte* bytes,
  int numBytesToRead,
  IntPtr numBytesRead_mustBeZero,
  NativeOverlapped* overlapped
);
以下是官方参考资料:

不过,这只是为了打开文件,正如您所说:

如何在C中仅使用文件句柄打开文件++

如果要读取已打开的文件,可能会遇到更多问题。我不确定。您可能可以使用此功能:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern SafeFileHandle CreateFile(
  string lpFileName,
  int dwDesiredAccess,
  FileShare dwShareMode,
  SECURITY_ATTRIBUTES securityAttrs,
  FileMode dwCreationDisposition,
  int dwFlagsAndAttributes,
  IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern unsafe int ReadFile(
  SafeFileHandle handle,
  byte* bytes,
  int numBytesToRead,
  IntPtr numBytesRead_mustBeZero,
  NativeOverlapped* overlapped
);

您可以使用win32调用,与filestream/文件构造函数(通过p/invoke)的使用方式相同

将其在中打开,看起来它正在使用此功能:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern SafeFileHandle CreateFile(
  string lpFileName,
  int dwDesiredAccess,
  FileShare dwShareMode,
  SECURITY_ATTRIBUTES securityAttrs,
  FileMode dwCreationDisposition,
  int dwFlagsAndAttributes,
  IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern unsafe int ReadFile(
  SafeFileHandle handle,
  byte* bytes,
  int numBytesToRead,
  IntPtr numBytesRead_mustBeZero,
  NativeOverlapped* overlapped
);
以下是官方参考资料:

不过,这只是为了打开文件,正如您所说:

如何在C中仅使用文件句柄打开文件++

如果要读取已打开的文件,可能会遇到更多问题。我不确定。您可能可以使用此功能:

[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern SafeFileHandle CreateFile(
  string lpFileName,
  int dwDesiredAccess,
  FileShare dwShareMode,
  SECURITY_ATTRIBUTES securityAttrs,
  FileMode dwCreationDisposition,
  int dwFlagsAndAttributes,
  IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError=true)]
internal static extern unsafe int ReadFile(
  SafeFileHandle handle,
  byte* bytes,
  int numBytesToRead,
  IntPtr numBytesRead_mustBeZero,
  NativeOverlapped* overlapped
);

这完全取决于情况,但您不太可能做到这一点。我假设你的C代码和C++代码在不同的进程中运行——如果你在同一个进程中,你应该能够在<代码> ItpTrt/Cube >上作为一个<代码>句柄 问题是文件句柄是特定于某个进程的--您将无法在另一个进程中使用该句柄

也就是说,你可能会更好:

  • 将文件名传递给C++代码并打开它
  • 将文件中实际包含的数据传递给C++并处理它。
这完全取决于你——但你不太可能做到这一点。我假设你的C代码和C++代码在不同的进程中运行——如果你在同一个进程中,你应该能够在<代码> ItpTrt/Cube >上作为一个<代码>句柄 问题是文件句柄是特定于某个进程的--您将无法在另一个进程中使用该句柄

也就是说,你可能会更好:

  • 将文件名传递给C++代码并打开它
  • 将文件中实际包含的数据传递给C++并处理它。

如果C++代码是C++ + CLI,那么就不用费心使用句柄。只需将<代码>文件管理器< /COD>对象直接传递给C++代码。< /P>


<如果C++是本地代码,那么可以使用文件句柄,通常在文件中使用Windows >代码>句柄< /Cord>值,如<代码> Read Fixs>代码>和<代码>写文件。您不会使用句柄打开文件,因为它已打开。如果需要句柄的另一个副本,或者希望将句柄提供给另一个进程,请使用。如果需要使用类似POSIX的函数(如
\u read
\u write
)输入值,则调用以获取文件描述符。你可以用.< /p> < p>将文件描述符包装成C <代码>文件> <代码>流。如果C++代码是C++ + CLI,那么就不用费心处理句柄了。只需将<代码>文件管理器< /COD>对象直接传递给C++代码。< /P>
<如果C++是本地代码,那么可以使用文件句柄,通常在文件中使用Windows >代码>句柄< /Cord>值,如<代码> Read Fixs>代码>和<代码>写文件。您不会使用句柄打开文件,因为它已打开。如果需要句柄的另一个副本,或者希望将句柄提供给另一个进程,请使用。如果需要使用类似POSIX的函数(如
\u read
\u write
)输入值,则调用以获取文件描述符。您可以使用将文件描述符包装成一个C
文件*
流。

结果表明,标题并不是OP真正想要的

但是如果有人真的需要这样做(比如:用不同的权限重新打开一个文件),您可能可以使用的组合来获取文件ID和


FWIW.

结果证明,这个标题并不是OP真正想要的

但是如果有人真的需要这样做(比如:用不同的权限重新打开一个文件),您可能可以使用的组合来获取文件ID和


FWIW.

你确定你真的想这么做吗?即使可以,传递文件名也可能更简单……我的第一个问题是“为什么?”。这也许是可能的,但它将是丑陋的。我宁愿退一步,分析一下需要什么样的最终结果,看看是否有一种更干净的方法可以做到这一点。你确定你真的想这样做吗?即使可以,传递文件名也可能更简单……我的第一个问题是“为什么?”。这也许是可能的,但它将是丑陋的。我宁愿退一步,分析最终结果,看看是否有更干净的方法。谢谢Merlyn,但这种方法需要一个文件名,而我只有一个文件句柄。@smwikipedia:你可能在我编辑帖子时写了你的评论。查看第二个函数ReadFileThank Merlyn,但是这个方法需要一个文件名,而我只有一个文件句柄。@smwikipedia:你可能在我编辑文章时写了你的评论。查看第二个函数ReadFileThis可能比我的帖子更准确,因为文件句柄可能是特定于进程的。你可以试试ReadFile,如果不起作用,你只需要使用C++来打开文件。