winim icon indicating copy to clipboard operation
winim copied to clipboard

Does winim only cover a part of the winapi?

Open KnorrFG opened this issue 3 years ago • 10 comments

Hey, I have to admit, I'm not super familiar with winapi programming. But I was thinking about writing a window manager for windows, and wanted to check how complicated that would be E.g. when looking into how to transfer windows between virtual desktops, I found this: https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ivirtualdesktopmanager-movewindowtodesktop. However, I cannot find those functions in winim. Did I miss them? Or are they actually missing?

KnorrFG avatar May 27 '21 14:05 KnorrFG

No header/library can cover all Windows API. I added SDK header one by one into winim if I think it useful for me or for most people.

khchen avatar May 27 '21 14:05 khchen

I see. Are there instructions somewhere on how to add them? If it's doable for me, and not requiring tons of winapi expertise, I'd try to add it myself.

KnorrFG avatar May 27 '21 14:05 KnorrFG

Adding one API is easy, adding a whole header file is hard. You can look at https://github.com/byt3bl33d3r/OffensiveNim/blob/master/src/pop_bin.nim as an example to add one API.

khchen avatar May 27 '21 14:05 khchen

I see. Thanks for the info. It seems that header isn't even part of mingw64, and its neccessary to use vcc. But since its a windows project anyway that's no problem.

KnorrFG avatar May 27 '21 14:05 KnorrFG

@KnorrFG Does that mean you will implement the things you would've needed for your project?

theAkito avatar May 27 '21 15:05 theAkito

I will try at least ^^

KnorrFG avatar May 27 '21 16:05 KnorrFG

Sorry for reopening, but to me, this seemed more appropriate than opening a new issue. I tried to write a wrapper for the API, but I've hit a wall. When I try to run the following program, it will always throw a sigsegv on the second to last line (the one that acctually calls IsWindowOnCurrentVirtualDesktop). Do you have an Idea why?

import winim

let 
    CLSID_VirtualDesktopManager* = DEFINE_GUID("aa509086-5ca9-4c25-8f95-589d3c07b48a")
    IID_VirtualDesktopManager* = DEFINE_GUID("a5cd92ff-29be-454c-8d04-d82879fb3f1b")
type 
  IVirtualDesktopManager* {.pure.} = object
    lpVtbl*: ptr IVirtualDesktopManagerVtbl
  IVirtualDesktopManagerVtbl* {.pure, inheritable.} = object of IDispatchVtbl
    MoveWindowToDesktop*: proc (self: ptr IVirtualDesktopManager, topLevelWindow: HWND, desktopId: REFGUID ): HRESULT {.stdcall.}
    IsWindowOnCurrentVirtualDesktop*: proc (self: ptr IVirtualDesktopManager, topLevelWindow: HWND , onCurrentDesktop: ptr bool): HRESULT {.stdcall.}
    GetWindowDesktopId*: proc (self: ptr IVirtualDesktopManager, topLevelWindow: HWND , desktopId: ptr GUID): HRESULT {.stdcall.}

var vdmngr: ptr IVirtualDesktopManager
CoInitialize(nil)

if CoCreateInstance(&CLSID_VirtualDesktopManager, nil, CLSCTX_INPROC_SERVER, 
                 &IID_VirtualDesktopManager, cast[ptr PVOID](&vdmngr)).FAILED: raise
#defer: vdmngr.Release()

let win = GetActiveWindow()
var onDesktop = false
if vdmngr.isNil:
    echo "ptr is nil"
if vdmngr.lpVtbl.isNil:
    echo "vtable is nil"
if onDesktop.addr.isNil:
    echo "on desktop is nil"
echo vdmngr.lpVtbl.IsWindowOnCurrentVirtualDesktop(vdmngr, win, onDesktop.addr)
echo "win on current desktop: ", onDesktop

KnorrFG avatar May 27 '21 17:05 KnorrFG

Not yet test your code, but the document says: IVirtualDesktopManager interface inherits from the IUnknown interface. IVirtualDesktopManagerVtbl* {.pure, inheritable.} = object of IUnknownVtbl

khchen avatar May 27 '21 20:05 khchen

You were right, making this change will make the api seem to work (after also changing GetActiveWindow() to GetForegroundWindow()). However, it outputs "false" in the end. And as far as I understand it, this code should always output true. Additionally, It seems that it is not possible to actually manage virtual desktops with a program. For example, you cannot move windows of another process with this api. The only thing I found were some reverse engineered functions in a russian forum, that seem to be buggy. By now, I'm extremely annoyed with the winapi, and I will probably just cut virtual desktop supports from my plans.

I'd be willing to open a pull request to contribute the API above if you want me to, but I read somewhere else that you autogenerated most of the files, so I'm not sure, how useful that is.

KnorrFG avatar May 28 '21 08:05 KnorrFG

By now, I'm extremely annoyed with the winapi

Who isn't?

There is a reason why most people dealing with Windows are either using platform-independent code or the new hipster C# crap and only a little bit of C++, if necessary.

For my project, I need to use the low level Windows API, because the new hipster crap is too high level for what I want to do. That is the reason I'm using the Windows API in the form of winim.

I would recommend any developer to only use the actual Windows API, if they really have to.

Trust me, you do not want to know how many hours of my time I already spent on fixing some Windows API implementation, just because the Documentation was unclear about a minor detail. Rarely, the documentation may even be wrong.

theAkito avatar May 28 '21 10:05 theAkito