I am implementing a DLL using Rust. The DLL's host application passes Win32 file handles to the DLL for reading and writing.
Is there some function existing that returns a "normal" Rust reader from a Windows file handle? How would a manual implementation of the Read or Write trait look like?
On Windows, there is a platform-specific trait FromRawHandle, which is implemented for std::fs::File and std::process::Stdio.
Using this transfers ownership of the handle; when the File or Stdio is dropped, the handle will be closed. Make sure that matches with your API and use it accordingly.
There is an equivalent trait for *nix platforms: FromRawFd.
Related
How do I write a code which copies a text file using assembly and in windows?
My compiler is masm.
Not really sure whether you want to read from the file to memory and do something with that, or simply create a copy. In the first case, use CreateFile, otherwise go with CopyFile. You'll need to link with kernel32.dll to be able to use these functions.
In Windows, interacting with the OS involves calling API functions rather than making interrupt calls as in Linux.
If you just want to copy the file, call CopyFile. If you want to read the file, do some processing, and then write, you'll need CreateFile, ReadFile, and WriteFile. (You can find documentation for those functions from the CopyFile link above.)
I don't have a link to a good tutorial on calling Windows API functions from assembly language. Searching reveals some information, but nothing that I'd call a good tutorial. You'll have to look for examples and try things.
I want to create a annonymous pipe on windows using the CreatePipe of the WinAPI via the FFI. This will give me a HANDLE (type from the Win32 haskell package), but I'd like to get an ordinary haskell Handle such that I can use the standard haskell IO functions on it. So I need a function of type:
win32handleToStandardHandle :: HANDLE -> IO Handle
How can I implement this?
On linux, I can use System.Posix.IO's fdToHandle function to convert between the FD type used by the linux system calls and the standard haskell type. But there seems to be no such function for windows.
Even on Windows the standard implementation of files in System.IO uses a file descriptor, not a win32 HANDLE. These file descriptors are provided by the C runtime, not Windows. To convert a HANDLE to a file descriptor the runtime provides the _open_osfhandle function. GHC uses MinGW on Windows but MinGW doesn't provide its own C runtime, so Haskell EXEs use msvcrt.dll.
Use the FFI to import _open_osfhandle. Use this to convert your HANDLE to a file descriptor and then call fdToHandle, which seems to live in GHC.IO.Handle.FD.
I haven't actually tried this.
Is it possible to create a new, arbitrary, file namespace scheme in Windows?
As best I understand, Windows currently understands two or three file system or file-system-like namespace schemes:
The namespace scheme we all know and love, eg, C:\path\to\file.
UNC paths, eg, \\server\path\to\file
One, perhaps uncommon scheme - the Windows NT Object Manager, eg, \\.\Device\COM1 - see WinObj on SysInternals, usually accessed by programs by calling CreateFile, though this is not really a file system.
Is it possible to implement a custom namespace scheme that would be universally, automatically used by the rest of the operating system? Perhaps a filter driver or some other specialized kernel-mode driver? I'm out of my league here, but I'm genuinely curious.
I don't have anything concrete, but lets say I wanted to implement a kernel driver that, not only understands how to read and write OpenVMS file systems, but also implements some sort of filter driver so that userland programs could use standard File-11 syntax to access such a filesystem.
For example, an existing program calls OpenFile("[DIR1.DIR2.DIR3]FILE.EXT;10"); and somehow a custom handler deals with it transparently, and lo, notepad can read and write VMS files. More importantly, perhaps, some ported program that expects OpenVMS File-11 path strings just works. Simply mapping the OpenVMS file system into the regular windows file system as D:\dir1\dir2\file.ext would be insufficient.
I should clarify that my OpenVMS reference is just an example; I'd be looking for a more generic solution. This could be for OpenVMS File-11, MVS, standard unix syntax ala /path/to/thing, or something I just cooked up myself.
I'm aware of shell-based namespace extensions, and compatibility layers like cygwin, but that's not what I'm looking for.
So SO, what do you think? Is this possible? Where do you start?
I'm using a virtual file system (PhysFS) and I'd like the entire application to do file IO through this VFS (that includes third-party libraries).
How can I redirect all file IO operations (C FILE* objects and C++ streams) through this VFS in Windows?
Also, a related question. Is file IO redirection a common feature of OS APIs? Will it be easy for me to port my application?
API hooking is probably the only way to address the problem. Hooking can be done using third-party helper libraries such as Detours and some other. This method is both non-trivial and not portable. In theory you could use a filesystem filter driver, but this way is much more complicated and requires a kernel-mode driver (which is a PITA to develop).
What are the differences, and in what cases one or the other would prove superior in some way?
First of all the function fopen can be used only for simple portable operations with files.
CreateFile on the other side can be used not only for operations with files, but also with directories (with use of corresponding options), pipes and various Windows devices.
CreateFile has a lot of additional useful switches, like FILE_FLAG_NO_BUFFERING, FILE_ATTRIBUTE_TEMPORARY and FILE_FLAG_SEQUENTIAL_SCAN, which can be very useful in different scenarios.
You can use CreateFile with a filename longer that MAX_PATH characters. It can be important for some server applications or ones which must be able to open any file (a virus scanner or a backup application for example). This is enabled by using namespace semantics, though this mode has its own concerns, like ability to actually create a file named ".." or L"\xfeff\x20\xd9ab" (good luck trying to delete them later).
You can use CreateFile in different security scenarios. I mean not only usage of security attributes. If current process has SE_BACKUP_NAME or SE_RESTORE_NAME privilege (like Administrators typically have) and enable this privilege, one can use CreateFile to open any file also a file to which you have no access through security descriptor.
If you only want to read the content of a file, you can use CreateFile, CreateFileMapping and MapViewOfFile to create file mapping. Then you can work with a file as with a block of memory, which can possibly increase your application's speed.
There are also other uses of the function, which are described in detail in the corresponding MSDN article.
So I can summarize: only if you have a hard portability requirements or if you need to pass a FILE* to some external library, then you have to use fopen. In all other cases I would recommend you to use CreateFile.
For best results, I would also advise to learn Windows API specifically, as there are many features that you can find a good use for.
UPDATED: Not directly related to your question, but I also recommend you to take a glance at transactional I/O functions which are supported starting with Windows Vista. Using this feature, you can commit a bunch of operation with files, directories or registry as one transaction that cannot be interrupted. It is a very powerful and interesting tool. If you are not ready now to use the transactional I/O functions, you can start with CreateFile and port your application to transactional I/O later.
That really depends on what type of program you are writing. If it is supposed to be portable, fopen will make your life easier. fopen will call CreateFile "behind the scenes".
Some more advanced options (cache control, file access control, etc) are only available if you are using the Win32 API (they depend on the Win32 file handle, as opposed to the FILE pointer in stdio), so if you are writing a pure Win32 application, you may want to use CreateFile.
CreateFile lets you
Open file for asynchronous I/O
Pass optimization hints like FILE_FLAG_SEQUENTIAL_SCAN
Set security and inherit settings without threading issues
They don't return the same handle type, with fopen/FILE object you can call other runtime functions such as fputs (as well as converting it to a "native" file handle)
Whenever possible, prefer object oriented wrappers that support RAII, like fstream or boost file IO objects.
You should, of course, care about the share mode, so fopen() and STL are insufficient.