I open a file in C # with FileStream, and I got the file descriptor number with this line:
IntPtr file_handle = fs.SafeFileHandle.DangerousGetHandle();
Now I want to pass this handle to C ++ code and use this handle value to access the file. Is it possible? How to open a file with just a file descriptor in C ++?
Thank.
Update
I am using C # for P / Invoke in a C ++ Win32 DLL (not a COM-DLL). I open the file in C # as a FileStream and pass the handle to C ++. Here are some of my code in the 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;
}
And here is my C # code:
[DllImport("...",EntryPoint = "read_file", CharSet = CharSet.Auto)]
public static extern void read_file(IntPtr file_handle_arg);
But I get this error:
Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.
Thank.
source
share