The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL Input in an existing window
Section


Joined: 05 May 2011
Posts: 3
I'd like to use SDL's input handling in an existing window. Is this possible? Using 1.3.

I have the hWnd and any other things I might need. Essentially all that's different is that I want to pass a handle or an instance rather than have SDL create me a widthXheight window.


I have tried the SDL_WINDOWID hack, but I have no success with it. Instead, it just creates a new black window of the same dimensions.
Input IS being registered as my hover buttons are responding to being hovered on, but in the wrong window :-/

Here's the code I have:

Code:


void Input::initialize() {
   HWND hWnd;
   hWnd = FindWindow(NULL, TEXT("Test Window"));

   char hack[64];
   sprintf(hack, "SDL_WINDOWID=%u", (unsigned long)hWnd);
   putenv(hack);

   RECT rw;
   GetWindowRect(hWnd, &rw);

   /* Initialize SDL */
   SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
   SDL_SetVideoMode(rw.right-rw.left, rw.bottom-rw.top, 0, 0);
   

   SDL_SysWMinfo pInfo;
   SDL_VERSION(&pInfo.version);
   SDL_GetWMInfo(&pInfo);

   SetWindowPos(hWnd, 0, rw.left, rw.top, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
   RAWINPUTDEVICE Rid;
 
   Rid.usUsagePage = 0x01;
   Rid.usUsage = 0x02;
   Rid.dwFlags = RIDEV_INPUTSINK;
   Rid.hwndTarget = hWnd;
   RegisterRawInputDevices(&Rid, 1, sizeof(Rid));

   SDL_WM_GrabInput(SDL_GRAB_ON); // SDL_GRAB_OFF to not grab input (default)

   SDL_EnableKeyRepeat(500, 30);

   absx = 0;   absy = 0;
}

Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
SDL_CreateWindowFrom IIRC
Section


Joined: 05 May 2011
Posts: 3
Sounds promising but it causes the parent application to crash.
It specifically breaks on the line: SDL_Window *w = SDL_CreateWindowFrom(hWnd);
Dump file tells me the exception:
The thread tried to read from or write to a virtual address for which it does not have appropriate access.

Code:

   HWND hWnd;
   hWnd = FindWindow(NULL, TEXT("Test Window"));
   
   SDL_Init(SDL_INIT_VIDEO);

   SDL_Window *w = SDL_CreateWindowFrom(hWnd);

   SDL_ShowWindow(w);

   SDL_EnableKeyRepeat(500, 30);
Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
Is this window in another application? If that's the case, then I am fairly confident I know the root of the issue but am unaware of a work-around that would not require modifying SDL (SDL modifies storage reserved for the window, since this is in another process it would cause an access violation); but I'm unsure as to why you'd be attempting this if that were the case.
Section


Joined: 05 May 2011
Posts: 3
Nathaniel J Fries wrote:
Is this window in another application? If that's the case, then I am fairly confident I know the root of the issue but am unaware of a work-around that would not require modifying SDL (SDL modifies storage reserved for the window, since this is in another process it would cause an access violation); but I'm unsure as to why you'd be attempting this if that were the case.



It is a seperate application of which I'm injecting into.

I'm using it to try and learn about modifying existing application functionality.
There's no specific end goal project, I'm just interested in learning it.
An example would be the various mods for roguelikes.
Such as a dwarf fortress mod that reads the base game data from the application and provides isometric or 3d graphics over the standard display.

I can get CEGUI to draw over the existing window on an external application with a DLL hook, and was hoping to use SDL's simple yet effective input management to pass the input to CEGUI from the same window.
SDL Input in an existing window
Patrick Baggett
Guest

If you are trying to hook into an existing window in a different app and have the hWnd of the window, try CreateRemoveThread() and AttachThreadInput(). You may need to hook into an existing DLL to allow for code injection. After that, you can simply send messages from that thread to your app. Not quite as straightforward, but could work.

Patrick

On Thu, Jun 2, 2011 at 3:46 AM, Section wrote:
Quote:



Nathaniel J Fries wrote:

Is this window in another application? If that's the case, then I am fairly confident I know the root of the issue but am unaware of a work-around that would not require modifying SDL (SDL modifies storage reserved for the window, since this is in another process it would cause an access violation); but I'm unsure as to why you'd be attempting this if that were the case.





It is a seperate application of which I'm injecting into.

I'm using it to try and learn about modifying existing application functionality.
There's no specific end goal project, I'm just interested in learning it.
An example would be the various mods for roguelikes.
Such as a dwarf fortress mod that reads the base game data from the application and provides isometric or 3d graphics over the standard display.

I can get CEGUI to draw over the existing window on an external application with a DLL hook, and was hoping to use SDL's simple yet effective input management to pass the input to CEGUI from the same window.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: SDL Input in an existing window
Nathaniel J Fries


Joined: 30 Mar 2010
Posts: 444
Patrick Baggett wrote:
If you are trying to hook into an existing window in a different app and have the hWnd of the window, try CreateRemoveThread() and AttachThreadInput(). You may need to hook into an existing DLL to allow for code injection. After that, you can simply send messages from that thread to your app. Not quite as straightforward, but could work.

Patrick

That would work fine if he were using windows API directly.
In the case of SDL, this will still cause problems because of SDL's use of GetWindowWord/Long/LongPtr