mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-30 09:03:20 -05:00
1 line
3.0 KiB
JavaScript
1 line
3.0 KiB
JavaScript
var WH_CALLWNDPROC=4;var WM_QUIT=18;var GM=require("_GenericMarshal");function WindowsMessagePump(d){this._ObjectID="win-message-pump";this._options=d;var b=require("events").inherits(this);b.createEvent("hwnd");b.createEvent("error");b.createEvent("message");b.createEvent("exit");this._msg=GM.CreateVariable(GM.PointerSize==4?28:48);this._kernel32=GM.CreateNativeProxy("Kernel32.dll");this._kernel32.mp=this;this._kernel32.CreateMethod("GetLastError");this._kernel32.CreateMethod("GetModuleHandleA");this._user32=GM.CreateNativeProxy("User32.dll");this._user32.mp=this;this._user32.CreateMethod("GetMessageA");this._user32.CreateMethod("CreateWindowExA");this._user32.CreateMethod("TranslateMessage");this._user32.CreateMethod("DispatchMessageA");this._user32.CreateMethod("RegisterClassExA");this._user32.CreateMethod("DefWindowProcA");this._user32.CreateMethod("PostMessageA");this.wndclass=GM.CreateVariable(GM.PointerSize==4?48:80);this.wndclass.mp=this;this.wndclass.hinstance=this._kernel32.GetModuleHandleA(0);this.wndclass.cname=GM.CreateVariable("MainWWWClass");this.wndclass.wndproc=GM.GetGenericGlobalCallback(4);this.wndclass.wndproc.mp=this;this.wndclass.toBuffer().writeUInt32LE(this.wndclass._size);this.wndclass.cname.pointerBuffer().copy(this.wndclass.Deref(GM.PointerSize==4?40:64,GM.PointerSize).toBuffer());this.wndclass.wndproc.pointerBuffer().copy(this.wndclass.Deref(8,GM.PointerSize).toBuffer());this.wndclass.hinstance.pointerBuffer().copy(this.wndclass.Deref(GM.PointerSize==4?20:24,GM.PointerSize).toBuffer());this.wndclass.wndproc.on("GlobalCallback",function c(h,i,g,f){if(this.mp._hwnd!=null&&this.mp._hwnd.Val==h.Val){this.mp.emit("message",{message:i.Val,wparam:g.Val,lparam:f.Val,lparam_hex:f.pointerBuffer().toString("hex")});return(this.mp._user32.DefWindowProcA(h,i,g,f))}else{if(this.mp._hwnd==null&&this.CallingThread()==this.mp._user32.RegisterClassExA.async.threadId()){return(this.mp._user32.DefWindowProcA(h,i,g,f))}}});this._user32.RegisterClassExA.async(this.wndclass).then(function(){this.nativeProxy.CreateWindowExA.async(this.nativeProxy.RegisterClassExA.async,136,this.nativeProxy.mp.wndclass.cname,0,8388608,0,0,100,100,0,0,0,0).then(function(f){if(f.Val==0){this.nativeProxy.mp.emit("error","Error creating hidden window")}else{this.nativeProxy.mp._hwnd=f;this.nativeProxy.mp.emit("hwnd",f);this.nativeProxy.mp._startPump()}})});this._startPump=function a(){this._user32.GetMessageA.async(this._user32.RegisterClassExA.async,this._msg,this._hwnd,0,0).then(function(f){if(f.Val>0){this.nativeProxy.TranslateMessage.async(this.nativeProxy.RegisterClassExA.async,this.nativeProxy.mp._msg).then(function(){this.nativeProxy.DispatchMessageA.async(this.nativeProxy.RegisterClassExA.async,this.nativeProxy.mp._msg).then(function(){this.nativeProxy.mp._startPump()})})}else{delete this.nativeProxy.mp._hwnd;this.nativeProxy.mp.emit("exit",0)}},function(f){this.nativeProxy.mp.stop()})};this.stop=function e(){if(this._hwnd){this._user32.PostMessageA(this._hwnd,WM_QUIT,0,0)}}}module.exports=WindowsMessagePump; |