Convert HANDLE to Handle - winapi

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.

Related

How to read bytes from a Windows file handle in Rust?

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.

How to call "ioctl" in Rust? And interface the Linux "tun" driver

How can I the function "ioctl" in Rust? Should I find a wrapper for it somewhere? Is there a de-facto wrapper? Or maybe it's already included in the standard Rust library? I've not found it, though.
Or, more generally, I need an interface the Linux "tun" driver.
You can find ioctl in nix, which contains convenience wrapper of *nix functionalities.

How are Windows API calls made on Assembly Level?

I've written some high level interpreters and a simple byte code compiler and interpreter and I want to start making a powerful intermediate language for my small operating system.
It has its own API just like windows does, and the only thing which prevents me of starting this project is to know how these specific API calls (for example the win32 forms api) are being made on the assembly level.
Is there a way to see the assembly output of not optimized c code for example and look how exatly the calls are being made? Or any sources on the WWW?
Thanks in advance
Having C documentation for the API, and knowing the calling convention / ABI, should be enough to create asm that uses it. There's no "magic" needed (no inline syscall instructions or anything like that).
Much of the Win32 API is implemented in user-space DLLs, so API calls are no different from other library function calls. (i.e. an indirect CALL with a function pointer, if I recall correctly).
Often the library function implementation will involve a syscall to interact with the kernel (or for 32-bit code, maybe an int or sysenter, I'm not sure), but this interface is not documented and is not stable across different Windows versions.

Is it possible to compile and pass a subroutine as input to another subroutine at runtime?

Is it possible to compile a user-defined subroutine at run-time and pass it as input to another subroutine which has already been compiled in Fortran?
Short answer: no.
Some languages allow this using some eval() procedure. The Fortran standard doesn't have any such capability, even though one could imagine such an extension if there were some compiler which was using LLVM or similar runtime systems (or even an interpreter).
All Fortran compilers I know are traditional compilers to machine code and they don't allow anything like that. What you could do, is to save the code to a file, call the Fortran compiler in background to compile a shared library (.dll or .so) and then load the library using your operating system specific routines.

Is it possible to call the Windows API from Forth?

In C/C++, Windows executables are linked against static libraries that import DLL files containing Windows API procedures.
But how do we access those procedures from Forth code (e.g. GForth)? Is it possible at all?
I'm aware that there's Win32Forth capable of doing Win32 stuff, but I'm interested how (and if) this could be done in Forth implementations that lack this functionality from the box (yet do run on target OS and are potentially able to interact with it on a certain level).
What currently comes up to my mind is loading the DLL files in question and somehow locating the address of a procedure to execute - but then, execute how? (All I know is that Windows API uses the stdcall
convention). And how do we locate a procedure without a C header? (I'm very new to Forth and just a bit less new to C++. Please bear with me if my musings are nonsense).
In general case, to implement foreign functions interface (FFI) for dynamically loaded libraries in some Forth system as extension (i.e., without changing source code and recompilation), we need the dlopen and dlsym functions, Forth assembler, and intimate knowledge of the Forth-system organization and ABI.
Sometimes it could be done even without assembler. For example, though SP-Forth has FFI, foreign calls were also implemented in pure Forth as a result of native code generation and union of the return stack with the native hardware stack.
Regarding Gforth, it seems that in the version 0.7.9 (see releases) it doesn't have FFI for stdcall calling convention out of the box (it supports cdecl only), although it has dlopen and dlsym, and an assembler. So, it should be feasible to implement FFI for stdcall.
Yes, you could do this in Gforth according to its documentation. The biggest problem will be dealing with call backs, which the Windows API relies on rather heavily. There is an unsupported package to deal with this, see 5.25.6 Callbacks. I have not attempted this myself in Gforth, but the documentation looks adequate.
You might also want to check MPE's VFXForth. From their website:
Windows API Access
VFX Forth can access all the standard Windows API calls, as well as functions in any other DLLs. The function interface allows API calls to be defined by cut and paste from other language reference manuals, for example:
EXTERN: int PASCAL CreateDialogIndirectParam( HINSTANCE, void *,HWND, WNDPROC, LPARAM );
EXTERN: int PASCAL SetWindowText( HANDLE, LPSTR );
EXTERN: HANDLE PASCAL GetDlgItem( HANDLE, int );
This is down the page a bit at VFX Forth for Windows.
As I do my Forth on Mac and Linux, I can't work through the Windows for Gforth to provide more detail, sorry.
Gforth 0.7.9 provides Windows API calls generated by Swig from the Windows header files. The C interface uses a wrapper library, which is compiled by the C compiler, to pass parameters from the Forth stack to the system functions; as the C compiler understands stdcall, and the header files declare Windows API as stdcall, this "just works".
As all pre-generated C bindings live in the directory "unix" (for historical reasons), include unix/win32.fs gives you the win32 part of the Windows API.
Callbacks in the event loop are still a problem, as Gforth is a Cygwin program, and Cygwin has its special event loop task... but I hope that problem can be fixed.

Resources