├── .eslintrc ├── .github └── workflows │ ├── publish.yml │ └── test.yml ├── .gitignore ├── .gitlab-ci.yml ├── .npmignore ├── LICENSE ├── README.md ├── binding.gyp ├── index.js ├── make ├── make.bat ├── package.json ├── src ├── cs │ ├── .gitignore │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── User32.cs │ ├── WinAPI.csproj │ ├── WinAPI.sln │ ├── WinAPI.suo │ └── app.config └── main.cc ├── test.js └── test ├── index.js └── test_nw ├── .gitignore ├── index.html ├── index.js └── package.json /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "extends": [ 4 | "plugin:ivs/node" 5 | ] 6 | } -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: publish 2 | on: 3 | create: 4 | tags: 5 | - 'v*' 6 | jobs: 7 | publish: 8 | runs-on: windows-2019 9 | defaults: 10 | run: 11 | shell: msys2 {0} 12 | steps: 13 | - uses: msys2/setup-msys2@v2 14 | with: 15 | path-type: inherit 16 | install: rsync 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: '16.x' 21 | registry-url: 'https://registry.npmjs.org' 22 | - run: npm install 23 | - run: npm install -g nw-gyp prebuildify node-gyp 24 | - run: powershell Install-WindowsFeature Net-Framework-Core 25 | - run: npm run test 26 | - run: ./make --build --build-cs --sign 27 | env: 28 | MSYS2_ARG_CONV_EXCL: "*" 29 | SIGNING_SERVER: ${{ secrets.SIGNING_SERVER }} 30 | - run: npm publish 31 | shell: cmd 32 | env: 33 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 34 | - name: Upload binaries to release 35 | uses: svenstaro/upload-release-action@v2 36 | with: 37 | repo_token: ${{ secrets.GITHUB_TOKEN }} 38 | file: ./prebuilds/**/* 39 | file_glob: true 40 | tag: ${{ github.ref }} 41 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | on: [push] 3 | jobs: 4 | tests: 5 | runs-on: windows-2019 6 | defaults: 7 | run: 8 | shell: msys2 {0} 9 | steps: 10 | - uses: msys2/setup-msys2@v2 11 | with: 12 | path-type: inherit 13 | install: rsync 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v2 16 | with: 17 | node-version: '16.x' 18 | registry-url: 'https://registry.npmjs.org' 19 | - run: npm install 20 | - run: npm ls 21 | - run: npm install -g nw-gyp prebuildify node-gyp 22 | - run: powershell Install-WindowsFeature Net-Framework-Core 23 | - run: ./make --build --build-cs 24 | env: 25 | MSYS2_ARG_CONV_EXCL: "*" 26 | - run: npm run test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /build 3 | /npm-debug.log 4 | /build_log.txt 5 | /prebuilds 6 | .env 7 | WinAPI.exe -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | image: stefanscherer/node-windows:12.18-build-tools 2 | 3 | test: 4 | tags: 5 | - docker-windows 6 | stage: test 7 | script: 8 | - npm install 9 | - npm test 10 | 11 | deploy: 12 | tags: 13 | - docker-windows 14 | stage: deploy 15 | only: 16 | - tags 17 | script: 18 | - npm install 19 | - npm test -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /test 3 | /build 4 | /src/cs 5 | .env 6 | .git* 7 | make* 8 | .eslint* 9 | *.gyp 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Francois Leurent 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # winapi 3 | Misc natives MS Windows API wrappers for nodejs & nwjs 4 | 5 | [![Version](https://img.shields.io/npm/v/winapi.svg)](https://www.npmjs.com/package/winapi) 6 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT) 7 | [![Available platform](https://img.shields.io/badge/platform-nwjs-blue.svg)](https://www.npmjs.com/package/winapi) [![Available platform](https://img.shields.io/badge/platform-win32-blue.svg)](https://www.npmjs.com/package/winapi) 8 | [![Code style](https://img.shields.io/badge/code%2fstyle-ivs-green.svg)](https://www.npmjs.com/package/eslint-plugin-ivs) 9 | 10 | 11 | 12 | # API 13 | 14 | ## require('winapi').getIdleTime() 15 | Return the time the system has been idle (since last user interaction - e.g. mouse, keyboard & stuffs, see GetLastInputInfo) 16 | This is really usefull if you want to create a screensaver/like in nodejs / node-webkit. 17 | 18 | ``` 19 | var winapi = require('winapi'); 20 | 21 | console.log("System is idle since %s", winapi.getIdleTime() ); 22 | 23 | ``` 24 | 25 | ## require('winapi').CreateJobGroup() 26 | Create a [job group](https://docs.microsoft.com/fr-fr/windows/desktop/ProcThread/job-objects) with current process and all future child_process. Use this to kill zombies. Like Rick. 27 | 28 | ``` 29 | var winapi = require('winapi'); 30 | 31 | winapi.CreateJobGroup(); 32 | 33 | //you can now spawn subprocess and they'll be killed once you died, windows will clean everything up 34 | 35 | ``` 36 | * (checkout my [dispatcher](https://github.com/131/dispatcher) project for inspiration) 37 | * Available for node 8, nw 26 and nw 14. (i did not re-compile other platform binaries, do it by yourself...) 38 | 39 | 40 | 41 | 42 | ## require('winapi').GetLastInputInfo() 43 | Use this native binding to get system last input time ([see MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646302%28v=vs.85%29.aspx) ) 44 | 45 | ``` 46 | var winapi = require('winapi'); 47 | 48 | console.log("Last input time is %s", winapi.GetLastInputInfo() ); 49 | 50 | setTimeout(function(){ 51 | //do not move, it wont change ! 52 | console.log("Last input time is %s", winapi.GetLastInputInfo() ); 53 | }, 1000); 54 | 55 | ``` 56 | 57 | 58 | ## require('winapi').GetChildrenProcess([parentProcessId]) 59 | List all children process (of specified parent PID, default to current process) 60 | 61 | 62 | ## require('winapi').GetParentProcess([childProcessId]) 63 | Get a process parent PID (of specified process PID, default to current process) 64 | 65 | 66 | 67 | ## require('winapi').GetTickCount() 68 | Retrieves the number of milliseconds that have elapsed since the system was started (uptime). ([see MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724408%28v=vs.85%29.aspx) ) 69 | 70 | ### require('winapi').GetDisplaysList(console.log) 71 | List all connected screens 72 | 73 | # Credits 74 | * [131](mailto:131.js@cloudyks.org) 75 | * Code signing, courtesy of IVS Group. 76 | 77 | 78 | # Keywords / shout box 79 | screensaver, windows api, winuser.h, GetLastInputInfo, activity monitor, inactivity trigger, idle timer, system uptime, sytem bootime 80 | 81 | 82 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | {"targets": [ 2 | 3 | { 4 | "target_name": "winapi", 5 | "sources": [ "src/main.cc" ], 6 | "include_dirs": [ 7 | " %temp%\MSBUILD_PATH && set /P MSBUILD_PATH=<%temp%\MSBUILD_PATH 3 | for %%F in ("%MSBUILD_PATH%") do set MSBUILD_DIR=%%~dpF 4 | set path=%path%;%MSBUILD_DIR% 5 | 6 | where msbuild.exe 7 | 8 | REM in vs2017 cmd line 9 | call prebuildify --target node-webkit@0.14.6 --strip --arch=ia32 --node-gyp=nw-gyp.cmd 10 | call prebuildify --target node-webkit@0.26.0 --strip --arch=ia32 --node-gyp=nw-gyp.cmd 11 | call prebuildify --target node-webkit@0.26.6 --strip --arch=ia32 --node-gyp=nw-gyp.cmd 12 | call prebuildify --target node@10.24.0 --strip --arch=ia32 13 | 14 | 15 | REM in vs2019 cmd line 16 | call prebuildify --target node@8.17.0 --strip --arch=x64 17 | call prebuildify --target node@16.20.2 --strip --arch=x64 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "winapi", 3 | "version": "2.10.2", 4 | "description": "Misc windows API wrappers", 5 | "main": "index.js", 6 | "dependencies": { 7 | "nan": "^2.18.0", 8 | "node-gyp-build": "^4.4.0" 9 | }, 10 | "scripts": { 11 | "eslint": "eslint index.js", 12 | "test": "node node_modules/mocha/bin/mocha test", 13 | "install": "node-gyp-build" 14 | }, 15 | "os": [ 16 | "win32" 17 | ], 18 | "gypfile": true, 19 | "devDependencies": { 20 | "eslint-plugin-ivs": "^1.0.7", 21 | "expect.js": "^0.3.1", 22 | "mocha": "^2.4.5" 23 | }, 24 | "directories": { 25 | "test": "test" 26 | }, 27 | "repository": { 28 | "type": "git", 29 | "url": "git+ssh://git@github.com/131/node-winapi.git" 30 | }, 31 | "keywords": [ 32 | "screensaver", 33 | "GetLastInputInfo", 34 | "winapi", 35 | "windows.h", 36 | "activity", 37 | "idletimer" 38 | ], 39 | "author": { 40 | "name": "Francois Leurent", 41 | "email": "131.js@cloudyks.org" 42 | }, 43 | "license": "ISC", 44 | "bugs": { 45 | "url": "https://github.com/131/node-winapi/issues" 46 | }, 47 | "homepage": "https://github.com/131/node-winapi#readme", 48 | "readme": "# winapi\r\nMisc natives wrappers for nodejs\r\n\r\n## require('winapi').getIdleTime()\r\nReturn the time the system has been idle (since last user interaction - e.g. mouse, keyboard & stuffs, see GetLastInputInfo)\r\nThis is really usefull if you want to create a screensaver/like in nodejs / node-webkit.\r\n\r\n```\r\nvar winapi = require('winapi');\r\n\r\nconsole.log(\"System is idle since %s\", winapi.getIdleTime() );\r\n\r\n```\r\n\r\n\r\n\r\n## require('winapi').GetLastInputInfo()\r\nUse this native binding to get system last input time ([see MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646302%28v=vs.85%29.aspx) )\r\n\r\n```\r\nvar winapi = require('winapi');\r\n\r\nconsole.log(\"Last input time is %s\", winapi.GetLastInputInfo() );\r\n\r\nsetTimeout(function(){\r\n //do not move, it wont change !\r\n console.log(\"Last input time is %s\", winapi.GetLastInputInfo() );\r\n}, 1000);\r\n\r\n```\r\n\r\n\r\n## require('winapi').GetTickCount()\r\nRetrieves the number of milliseconds that have elapsed since the system was started (uptime). ([see MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724408%28v=vs.85%29.aspx) )\r\n\r\n\r\n# Credits\r\n* [131](mailto:131.js@cloudyks.org)\r\n\r\n\r\n# Keywords / shout box\r\nscreensaver, windows api, winuser.h, GetLastInputInfo, activity monitor, inactivity trigger, idle timer, system uptime, sytem bootime\r\n\r\n\r\n", 49 | "readmeFilename": "README.md", 50 | "_id": "winapi@1.1.0", 51 | "_from": "winapi@^1.1.0" 52 | } -------------------------------------------------------------------------------- /src/cs/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | /obj 3 | *.suo -------------------------------------------------------------------------------- /src/cs/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | using System.Web.Script.Serialization; 7 | using System.Windows.Forms; 8 | 9 | namespace WinAPI 10 | { 11 | 12 | 13 | class Program 14 | { 15 | 16 | const string CMD_ReOrientDisplay = "ReOrientDisplay"; 17 | const string CMD_GetDisplaySettings = "GetDisplaySettings"; 18 | const string CMD_GetDisplayList = "GetDisplaysList"; 19 | const string CMD_MaximizeWindow = "MaximizeWindow"; 20 | const string CMD_MinimizeWindow = "MinimizeWindow"; 21 | 22 | const string CMD_HideWindow = "HideWindow"; 23 | const string CMD_ShowWindow = "ShowWindow"; 24 | 25 | 26 | static void Main(string[] args) 27 | { 28 | var cmd = args.Length>0 ? args[0] : null; 29 | if (cmd == CMD_GetDisplaySettings) 30 | { 31 | User32.DisplaySettings ta = User32.GetDisplaySettings(); 32 | Console.WriteLine(new JavaScriptSerializer().Serialize(ta)); 33 | } 34 | 35 | if (cmd == CMD_GetDisplayList) 36 | { 37 | 38 | Console.WriteLine(new JavaScriptSerializer().Serialize(Screen.AllScreens)); 39 | 40 | } 41 | 42 | if (cmd == CMD_ReOrientDisplay) 43 | { 44 | User32.Orientation orientation = (User32.Orientation)Enum.Parse(typeof(User32.Orientation), args.Length > 1 ? args[1] : null); 45 | Console.WriteLine(orientation); 46 | User32.ReOrientDisplay(orientation); 47 | } 48 | 49 | if (cmd == CMD_MinimizeWindow) 50 | { 51 | string title = args[1]; 52 | Console.WriteLine(title); 53 | User32.MinimizeWindow(title); 54 | } 55 | 56 | if (cmd == CMD_MaximizeWindow) 57 | { 58 | string title = args[1]; 59 | Console.WriteLine(title); 60 | User32.MaximizeWindow(title); 61 | } 62 | 63 | if (cmd == CMD_HideWindow) 64 | { 65 | uint pid = Convert.ToUInt32(args[1]); 66 | Console.WriteLine(pid); 67 | User32.HideWindow(pid); 68 | } 69 | 70 | if (cmd == CMD_ShowWindow) 71 | { 72 | uint pid = Convert.ToUInt32(args[1]); 73 | Console.WriteLine(pid); 74 | User32.ShowWindow(pid); 75 | } 76 | 77 | } 78 | 79 | 80 | 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/cs/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("WinAPI")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("IVS")] 12 | [assembly: AssemblyProduct("WinAPI")] 13 | [assembly: AssemblyCopyright("Copyright © IVS 2017")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d4840230-7ae8-44c6-9f62-649be5d15ec5")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/cs/User32.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Diagnostics; 4 | using System.Runtime.InteropServices; 5 | using System.IO; 6 | using System.Collections.Generic; 7 | using System.Text; 8 | using System.Drawing; 9 | using System.Windows.Forms; 10 | 11 | namespace WinAPI 12 | { 13 | 14 | 15 | [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 16 | internal struct DEVMODE 17 | { 18 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 19 | public string dmDeviceName; 20 | 21 | public short dmSpecVersion; 22 | public short dmDriverVersion; 23 | public short dmSize; 24 | public short dmDriverExtra; 25 | public int dmFields; 26 | public int dmPositionX; 27 | public int dmPositionY; 28 | public int dmDisplayOrientation; 29 | public int dmDisplayFixedOutput; 30 | public short dmColor; 31 | public short dmDuplex; 32 | public short dmYResolution; 33 | public short dmTTOption; 34 | public short dmCollate; 35 | 36 | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] 37 | public string dmFormName; 38 | 39 | public short dmLogPixels; 40 | public short dmBitsPerPel; 41 | public int dmPelsWidth; 42 | public int dmPelsHeight; 43 | public int dmDisplayFlags; 44 | public int dmDisplayFrequency; 45 | public int dmICMMethod; 46 | public int dmICMIntent; 47 | public int dmMediaType; 48 | public int dmDitherType; 49 | public int dmReserved1; 50 | public int dmReserved2; 51 | public int dmPanningWidth; 52 | public int dmPanningHeight; 53 | }; 54 | 55 | [StructLayout(LayoutKind.Sequential)] 56 | internal struct TBBUTTON 57 | { 58 | public Int32 iBitmap; 59 | public Int32 idCommand; 60 | public byte fsState; 61 | public byte fsStyle; 62 | // [ MarshalAs( UnmanagedType.ByValArray, SizeConst=2 ) ] 63 | // public byte[] bReserved; 64 | public byte bReserved1; 65 | public byte bReserved2; 66 | public UInt32 dwData; 67 | public IntPtr iString; 68 | }; 69 | 70 | 71 | 72 | 73 | 74 | public class User32 75 | { 76 | 77 | #region consts 78 | 79 | // constants 80 | public const int ENUM_CURRENT_SETTINGS = -1; 81 | public const int DISP_CHANGE_SUCCESSFUL = 0; 82 | public const int DISP_CHANGE_BADDUALVIEW = -6; 83 | public const int DISP_CHANGE_BADFLAGS = -4; 84 | public const int DISP_CHANGE_BADMODE = -2; 85 | public const int DISP_CHANGE_BADPARAM = -5; 86 | public const int DISP_CHANGE_FAILED = -1; 87 | public const int DISP_CHANGE_NOTUPDATED = -3; 88 | public const int DISP_CHANGE_RESTART = 1; 89 | 90 | public enum Orientation 91 | { 92 | DMDO_DEFAULT = 0, 93 | DMDO_90 = 1, 94 | DMDO_180 = 2, 95 | DMDO_270 = 3 96 | }; 97 | 98 | 99 | internal static Dictionary FlipOrientation = new Dictionary 100 | { 101 | {0, Orientation.DMDO_DEFAULT}, 102 | {1, Orientation.DMDO_90}, 103 | {2, Orientation.DMDO_180}, 104 | {3, Orientation.DMDO_270}, 105 | }; 106 | 107 | 108 | 109 | public const uint MOUSEEVENTF_LEFTDOWN = 0x0002; 110 | public const uint MOUSEEVENTF_LEFTUP = 0x0004; 111 | public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008; 112 | public const uint MOUSEEVENTF_RIGHTUP = 0x0010; 113 | public const uint MOUSEEVENTF_ABSOLUTE = 0x8000; 114 | public const uint MOUSEEVENTF_MOVE = 0x0001; 115 | 116 | 117 | public const uint VM_CLOSE = 0x0010; 118 | public const uint VM_GETICON = 0x007F; 119 | public const uint VM_KEYDOWN = 0x0100; 120 | public const uint VM_COMMAND = 0x0111; 121 | public const uint VM_USER = 0x0400; // 0x0400 - 0x7FFF 122 | public const uint VM_APP = 0x8000; // 0x8000 - 0xBFFF 123 | 124 | public const uint TB_GETBUTTON = 0x0400 + 23; 125 | public const uint TB_BUTTONCOUNT = 0x0400 + 24; 126 | public const uint TB_CUSTOMIZE = 0x0400 + 27; 127 | public const uint TB_GETBUTTONTEXTA = 0x0400 + 45; 128 | public const uint TB_GETBUTTONTEXTW = 0x0400 + 75; 129 | 130 | 131 | public const int SW_HIDE = 0; 132 | public const int SW_SHOWNORMAL = 1; 133 | public const int SW_SHOWMINIMIZED = 2; 134 | public const int SW_SHOWMAXIMIZED = 3; 135 | public const int SW_SHOWNOACTIVATE = 4; 136 | public const int SW_SHOW = 5; 137 | 138 | public const int SW_RESTORE = 9; 139 | public const int SW_SHOWDEFAULT = 10; 140 | 141 | public const uint WS_CHILD = 0x40000000; 142 | public const uint WS_POPUP = 0x80000000; 143 | public const int WM_UPDATEUISTATE = 0x0127; 144 | public const int UIS_INITIALIZE = 3; 145 | 146 | 147 | public const uint WS_BORDER = 0x00800000; 148 | public const uint WS_THICKFRAME = 0x00040000; 149 | public const uint WS_EX_WINDOWEDGE = 0x00000100; 150 | public const uint WS_EX_CLIENTEDGE = 0x00000200; 151 | public const uint WS_EX_STATICEDGE = 0x00020000; 152 | public const uint WS_EX_TOOLWINDOW = 0x00000080; 153 | public const uint WS_EX_TRANSPARENT = 0x00000020; 154 | public const uint WS_VISIBLE = 0x10000000; 155 | public const uint WS_CLIPCHILDREN = 0x02000000; 156 | public const uint WS_MAXIMIZE = 0x01000000; 157 | public const uint WS_EX_APPWINDOW = 0x00040000; 158 | public const uint WS_EX_TOPMOST = 0x00000008; 159 | public const int GWL_EXSTYLE = -20; 160 | public const int GWL_STYLE = -16; 161 | public const long GWL_HWNDPARENT = -8; 162 | 163 | //GetWindow().. 164 | public const uint GW_HWNDFIRST = 0; 165 | public const uint GW_HWNDLAST = 1; 166 | public const uint GW_HWNDNEXT = 2; 167 | public const uint GW_HWNDPREV = 3; 168 | public const uint GW_OWNER = 4; 169 | public const uint GW_CHILD = 5; 170 | public const uint GW_MAX = 5; 171 | 172 | public const long GCL_MENUNAME = -8; 173 | public const long GCL_HBRBACKGROUND = -10; 174 | public const long GCL_HCURSOR = -12; 175 | public const long GCL_HICON = -14; 176 | public const long GCL_HMODULE = -16; 177 | public const long GCL_CBWNDEXTRA = -18; 178 | public const long GCL_CBCLSEXTRA = -20; 179 | public const long GCL_WNDPROC = -24; 180 | public const long GCL_STYLE = -26; 181 | public const long GCW_ATOM = -32; 182 | 183 | 184 | public const uint SPIF_UPDATEINIFILE = 0x01; 185 | public const uint SPIF_SENDCHANGE = 0x02; 186 | public const uint SPIF_SENDWININICHANGE = 0x02; 187 | 188 | public const uint SPI_GETBEEP = 0x0001; 189 | public const uint SPI_SETBEEP = 0x0002; 190 | public const uint SPI_GETMOUSE = 0x0003; 191 | public const uint SPI_SETMOUSE = 0x0004; 192 | public const uint SPI_GETBORDER = 0x0005; 193 | public const uint SPI_SETBORDER = 0x0006; 194 | public const uint SPI_GETKEYBOARDSPEED = 0x000A; 195 | public const uint SPI_SETKEYBOARDSPEED = 0x000B; 196 | public const uint SPI_LANGDRIVER = 0x000C; 197 | public const uint SPI_ICONHORIZONTALSPACING = 0x000D; 198 | public const uint SPI_GETSCREENSAVETIMEOUT = 0x000E; 199 | public const uint SPI_SETSCREENSAVETIMEOUT = 0x000F; 200 | public const uint SPI_GETSCREENSAVEACTIVE = 0x0010; 201 | public const uint SPI_SETSCREENSAVEACTIVE = 0x0011; 202 | public const uint SPI_GETGRIDGRANULARITY = 0x0012; 203 | public const uint SPI_SETGRIDGRANULARITY = 0x0013; 204 | public const uint SPI_SETDESKWALLPAPER = 0x0014; 205 | public const uint SPI_SETDESKPATTERN = 0x0015; 206 | public const uint SPI_GETKEYBOARDDELAY = 0x0016; 207 | public const uint SPI_SETKEYBOARDDELAY = 0x0017; 208 | public const uint SPI_ICONVERTICALSPACING = 0x0018; 209 | public const uint SPI_GETICONTITLEWRAP = 0x0019; 210 | public const uint SPI_SETICONTITLEWRAP = 0x001A; 211 | public const uint SPI_GETMENUDROPALIGNMENT = 0x001B; 212 | public const uint SPI_SETMENUDROPALIGNMENT = 0x001C; 213 | public const uint SPI_SETDOUBLECLKWIDTH = 0x001D; 214 | public const uint SPI_SETDOUBLECLKHEIGHT = 0x001E; 215 | public const uint SPI_GETICONTITLELOGFONT = 0x001F; 216 | public const uint SPI_SETDOUBLECLICKTIME = 0x0020; 217 | public const uint SPI_SETMOUSEBUTTONSWAP = 0x0021; 218 | public const uint SPI_SETICONTITLELOGFONT = 0x0022; 219 | public const uint SPI_GETFASTTASKSWITCH = 0x0023; 220 | public const uint SPI_SETFASTTASKSWITCH = 0x0024; 221 | public const uint SPI_SETDRAGFULLWINDOWS = 0x0025; 222 | public const uint SPI_GETDRAGFULLWINDOWS = 0x0026; 223 | public const uint SPI_GETNONCLIENTMETRICS = 0x0029; 224 | public const uint SPI_SETNONCLIENTMETRICS = 0x002A; 225 | public const uint SPI_GETMINIMIZEDMETRICS = 0x002B; 226 | public const uint SPI_SETMINIMIZEDMETRICS = 0x002C; 227 | public const uint SPI_GETICONMETRICS = 0x002D; 228 | public const uint SPI_SETICONMETRICS = 0x002E; 229 | public const uint SPI_SETWORKAREA = 0x002F; 230 | public const uint SPI_GETWORKAREA = 0x0030; 231 | public const uint SPI_SETPENWINDOWS = 0x0031; 232 | public const uint SPI_GETHIGHCONTRAST = 0x0042; 233 | public const uint SPI_SETHIGHCONTRAST = 0x0043; 234 | public const uint SPI_GETKEYBOARDPREF = 0x0044; 235 | public const uint SPI_SETKEYBOARDPREF = 0x0045; 236 | public const uint SPI_GETSCREENREADER = 0x0046; 237 | public const uint SPI_SETSCREENREADER = 0x0047; 238 | public const uint SPI_GETANIMATION = 0x0048; 239 | public const uint SPI_SETANIMATION = 0x0049; 240 | public const uint SPI_GETFONTSMOOTHING = 0x004A; 241 | public const uint SPI_SETFONTSMOOTHING = 0x004B; 242 | public const uint SPI_SETDRAGWIDTH = 0x004C; 243 | public const uint SPI_SETDRAGHEIGHT = 0x004D; 244 | public const uint SPI_SETHANDHELD = 0x004E; 245 | public const uint SPI_GETLOWPOWERTIMEOUT = 0x004F; 246 | public const uint SPI_GETPOWEROFFTIMEOUT = 0x0050; 247 | public const uint SPI_SETLOWPOWERTIMEOUT = 0x0051; 248 | public const uint SPI_SETPOWEROFFTIMEOUT = 0x0052; 249 | public const uint SPI_GETLOWPOWERACTIVE = 0x0053; 250 | public const uint SPI_GETPOWEROFFACTIVE = 0x0054; 251 | public const uint SPI_SETLOWPOWERACTIVE = 0x0055; 252 | public const uint SPI_SETPOWEROFFACTIVE = 0x0056; 253 | public const uint SPI_SETICONS = 0x0058; 254 | public const uint SPI_GETDEFAULTINPUTLANG = 0x0059; 255 | public const uint SPI_SETDEFAULTINPUTLANG = 0x005A; 256 | public const uint SPI_SETLANGTOGGLE = 0x005B; 257 | public const uint SPI_GETWINDOWSEXTENSION = 0x005C; 258 | public const uint SPI_SETMOUSETRAILS = 0x005D; 259 | public const uint SPI_GETMOUSETRAILS = 0x005E; 260 | public const uint SPI_SCREENSAVERRUNNING = 0x0061; 261 | public const uint SPI_GETFILTERKEYS = 0x0032; 262 | public const uint SPI_SETFILTERKEYS = 0x0033; 263 | public const uint SPI_GETTOGGLEKEYS = 0x0034; 264 | public const uint SPI_SETTOGGLEKEYS = 0x0035; 265 | public const uint SPI_GETMOUSEKEYS = 0x0036; 266 | public const uint SPI_SETMOUSEKEYS = 0x0037; 267 | public const uint SPI_GETSHOWSOUNDS = 0x0038; 268 | public const uint SPI_SETSHOWSOUNDS = 0x0039; 269 | public const uint SPI_GETSTICKYKEYS = 0x003A; 270 | public const uint SPI_SETSTICKYKEYS = 0x003B; 271 | public const uint SPI_GETACCESSTIMEOUT = 0x003C; 272 | public const uint SPI_SETACCESSTIMEOUT = 0x003D; 273 | public const uint SPI_GETSERIALKEYS = 0x003E; 274 | public const uint SPI_SETSERIALKEYS = 0x003F; 275 | public const uint SPI_GETSOUNDSENTRY = 0x0040; 276 | public const uint SPI_SETSOUNDSENTRY = 0x0041; 277 | public const uint SPI_GETSNAPTODEFBUTTON = 0x005F; 278 | public const uint SPI_SETSNAPTODEFBUTTON = 0x0060; 279 | public const uint SPI_GETMOUSEHOVERWIDTH = 0x0062; 280 | public const uint SPI_SETMOUSEHOVERWIDTH = 0x0063; 281 | public const uint SPI_GETMOUSEHOVERHEIGHT = 0x0064; 282 | public const uint SPI_SETMOUSEHOVERHEIGHT = 0x0065; 283 | public const uint SPI_GETMOUSEHOVERTIME = 0x0066; 284 | public const uint SPI_SETMOUSEHOVERTIME = 0x0067; 285 | public const uint SPI_GETWHEELSCROLLLINES = 0x0068; 286 | public const uint SPI_SETWHEELSCROLLLINES = 0x0069; 287 | public const uint SPI_GETMENUSHOWDELAY = 0x006A; 288 | public const uint SPI_SETMENUSHOWDELAY = 0x006B; 289 | public const uint SPI_GETSHOWIMEUI = 0x006E; 290 | public const uint SPI_SETSHOWIMEUI = 0x006F; 291 | public const uint SPI_GETMOUSESPEED = 0x0070; 292 | public const uint SPI_SETMOUSESPEED = 0x0071; 293 | public const uint SPI_GETSCREENSAVERRUNNING = 0x0072; 294 | public const uint SPI_GETDESKWALLPAPER = 0x0073; 295 | public const uint SPI_GETACTIVEWINDOWTRACKING = 0x1000; 296 | public const uint SPI_SETACTIVEWINDOWTRACKING = 0x1001; 297 | public const uint SPI_GETMENUANIMATION = 0x1002; 298 | public const uint SPI_SETMENUANIMATION = 0x1003; 299 | public const uint SPI_GETCOMBOBOXANIMATION = 0x1004; 300 | public const uint SPI_SETCOMBOBOXANIMATION = 0x1005; 301 | public const uint SPI_GETLISTBOXSMOOTHSCROLLING = 0x1006; 302 | public const uint SPI_SETLISTBOXSMOOTHSCROLLING = 0x1007; 303 | public const uint SPI_GETGRADIENTCAPTIONS = 0x1008; 304 | public const uint SPI_SETGRADIENTCAPTIONS = 0x1009; 305 | public const uint SPI_GETKEYBOARDCUES = 0x100A; 306 | public const uint SPI_SETKEYBOARDCUES = 0x100B; 307 | public const uint SPI_GETMENUUNDERLINES = SPI_GETKEYBOARDCUES; 308 | public const uint SPI_SETMENUUNDERLINES = SPI_SETKEYBOARDCUES; 309 | public const uint SPI_GETACTIVEWNDTRKZORDER = 0x100C; 310 | public const uint SPI_SETACTIVEWNDTRKZORDER = 0x100D; 311 | public const uint SPI_GETHOTTRACKING = 0x100E; 312 | public const uint SPI_SETHOTTRACKING = 0x100F; 313 | public const uint SPI_GETMENUFADE = 0x1012; 314 | public const uint SPI_SETMENUFADE = 0x1013; 315 | public const uint SPI_GETSELECTIONFADE = 0x1014; 316 | public const uint SPI_SETSELECTIONFADE = 0x1015; 317 | public const uint SPI_GETTOOLTIPANIMATION = 0x1016; 318 | public const uint SPI_SETTOOLTIPANIMATION = 0x1017; 319 | public const uint SPI_GETTOOLTIPFADE = 0x1018; 320 | public const uint SPI_SETTOOLTIPFADE = 0x1019; 321 | public const uint SPI_GETCURSORSHADOW = 0x101A; 322 | public const uint SPI_SETCURSORSHADOW = 0x101B; 323 | public const uint SPI_GETMOUSESONAR = 0x101C; 324 | public const uint SPI_SETMOUSESONAR = 0x101D; 325 | public const uint SPI_GETMOUSECLICKLOCK = 0x101E; 326 | public const uint SPI_SETMOUSECLICKLOCK = 0x101F; 327 | public const uint SPI_GETMOUSEVANISH = 0x1020; 328 | public const uint SPI_SETMOUSEVANISH = 0x1021; 329 | public const uint SPI_GETFLATMENU = 0x1022; 330 | public const uint SPI_SETFLATMENU = 0x1023; 331 | public const uint SPI_GETDROPSHADOW = 0x1024; 332 | public const uint SPI_SETDROPSHADOW = 0x1025; 333 | public const uint SPI_GETBLOCKSENDINPUTRESETS = 0x1026; 334 | public const uint SPI_SETBLOCKSENDINPUTRESETS = 0x1027; 335 | public const uint SPI_GETUIEFFECTS = 0x103E; 336 | public const uint SPI_SETUIEFFECTS = 0x103F; 337 | public const uint SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000; 338 | public const uint SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001; 339 | public const uint SPI_GETACTIVEWNDTRKTIMEOUT = 0x2002; 340 | public const uint SPI_SETACTIVEWNDTRKTIMEOUT = 0x2003; 341 | public const uint SPI_GETFOREGROUNDFLASHCOUNT = 0x2004; 342 | public const uint SPI_SETFOREGROUNDFLASHCOUNT = 0x2005; 343 | public const uint SPI_GETCARETWIDTH = 0x2006; 344 | public const uint SPI_SETCARETWIDTH = 0x2007; 345 | public const uint SPI_GETMOUSECLICKLOCKTIME = 0x2008; 346 | public const uint SPI_SETMOUSECLICKLOCKTIME = 0x2009; 347 | public const uint SPI_GETFONTSMOOTHINGTYPE = 0x200A; 348 | public const uint SPI_SETFONTSMOOTHINGTYPE = 0x200B; 349 | public const uint SPI_GETFONTSMOOTHINGCONTRAST = 0x200C; 350 | public const uint SPI_SETFONTSMOOTHINGCONTRAST = 0x200D; 351 | public const uint SPI_GETFOCUSBORDERWIDTH = 0x200E; 352 | public const uint SPI_SETFOCUSBORDERWIDTH = 0x200F; 353 | public const uint SPI_GETFOCUSBORDERHEIGHT = 0x2010; 354 | public const uint SPI_SETFOCUSBORDERHEIGHT = 0x2011; 355 | public const uint SPI_GETFONTSMOOTHINGORIENTATION = 0x2012; 356 | public const uint SPI_SETFONTSMOOTHINGORIENTATION = 0x2013; 357 | 358 | public const uint SC_SCREENSAVE = 0xF140; 359 | public const uint WM_SYSCOMMAND = 0x0112; 360 | 361 | #endregion 362 | 363 | [DllImport("user32.dll")] 364 | public static extern int GetClassLong(IntPtr hWnd, long nIndex); 365 | 366 | 367 | 368 | private const int sICONDIR = 6; // sizeof(ICONDIR) 369 | private const int sICONDIRENTRY = 16; // sizeof(ICONDIRENTRY) 370 | private const int sGRPICONDIRENTRY = 14; // sizeof(GRPICONDIRENTRY) 371 | 372 | /// 373 | /// Split an Icon consists of multiple icons into an array of Icon each consist of single icons. 374 | /// 375 | /// The System.Drawing.Icon to be split. 376 | /// An array of System.Drawing.Icon each consist of single icons. 377 | public static Icon[] SplitIcon(Icon icon) 378 | { 379 | if (icon == null) 380 | { 381 | throw new ArgumentNullException("icon"); 382 | } 383 | 384 | // Get multiple .ico file image. 385 | byte[] srcBuf = null; 386 | using (MemoryStream stream = new MemoryStream()) 387 | { 388 | icon.Save(stream); 389 | srcBuf = stream.ToArray(); 390 | } 391 | 392 | List splitIcons = new List(); 393 | { 394 | int count = BitConverter.ToInt16(srcBuf, 4); // ICONDIR.idCount 395 | 396 | for (int i = 0; i < count; i++) 397 | { 398 | using (MemoryStream destStream = new MemoryStream()) 399 | using (BinaryWriter writer = new BinaryWriter(destStream)) 400 | { 401 | // Copy ICONDIR and ICONDIRENTRY. 402 | writer.Write(srcBuf, 0, sICONDIR - 2); 403 | writer.Write((short)1); // ICONDIR.idCount == 1; 404 | 405 | writer.Write(srcBuf, sICONDIR + sICONDIRENTRY * i, sICONDIRENTRY - 4); 406 | writer.Write(sICONDIR + sICONDIRENTRY); // ICONDIRENTRY.dwImageOffset = sizeof(ICONDIR) + sizeof(ICONDIRENTRY) 407 | 408 | // Copy picture and mask data. 409 | int imgSize = BitConverter.ToInt32(srcBuf, sICONDIR + sICONDIRENTRY * i + 8); // ICONDIRENTRY.dwBytesInRes 410 | int imgOffset = BitConverter.ToInt32(srcBuf, sICONDIR + sICONDIRENTRY * i + 12); // ICONDIRENTRY.dwImageOffset 411 | writer.Write(srcBuf, imgOffset, imgSize); 412 | 413 | // Create new icon. 414 | destStream.Seek(0, SeekOrigin.Begin); 415 | Icon tmp = new Icon(destStream); 416 | splitIcons.Add(tmp); 417 | } 418 | } 419 | } 420 | 421 | return splitIcons.ToArray(); 422 | } 423 | 424 | [DllImport("user32.dll")] 425 | public static extern IntPtr GetForegroundWindow(); 426 | [DllImport("user32.dll", SetLastError = true)] 427 | public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 428 | 429 | [DllImport("user32.dll")] 430 | public static extern bool SetForegroundWindow(IntPtr hWnd); 431 | [DllImport("user32.dll")] 432 | public static extern bool BringWindowToTop(IntPtr hWnd); 433 | 434 | 435 | [DllImport("user32.dll", CharSet = CharSet.Auto)] 436 | public static extern int DrawIconEx( 437 | IntPtr hdc, // handle to device context 438 | int xLeft, // x-coord of upper left corner 439 | int yTop, // y-coord of upper left corner 440 | IntPtr hIcon, // handle to icon 441 | int cxWidth, // icon width 442 | int cyWidth, // icon height 443 | uint istepIfAniCur, // frame index, animated cursor 444 | IntPtr dbrFlickerFreeDraw, // handle to background brush 445 | uint diFlags); // icon-drawing flags 446 | 447 | 448 | [DllImport("user32.dll")] 449 | public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 450 | 451 | 452 | [DllImport("user32.dll")] 453 | public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 454 | 455 | [DllImport("user32.dll")] 456 | public static extern bool SetWindowText(IntPtr hWnd, string str); 457 | 458 | 459 | public static void SetOwner(IntPtr hWnd, IntPtr ownerWnd) 460 | { 461 | SetWindowLong(hWnd, User32.GWL_HWNDPARENT, (uint)ownerWnd); 462 | } 463 | [DllImport("user32.dll")] 464 | public static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent); 465 | 466 | 467 | [DllImport("user32.dll")] 468 | public static extern IntPtr SetFocus(IntPtr hWnd); 469 | 470 | [DllImport("user32.dll")] 471 | public static extern bool CloseWindow(IntPtr hWnd); 472 | 473 | 474 | [DllImport("user32.dll")] 475 | public static extern bool IsIconic(IntPtr hWnd); 476 | [DllImport("user32.dll")] 477 | public static extern uint GetWindowLong(IntPtr hWnd, int nIndex); 478 | [DllImport("user32.dll")] 479 | public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); 480 | 481 | 482 | [DllImport("user32.dll")] 483 | public static extern uint SetWindowLong(IntPtr hWnd, long nIndex, uint dwNewLong); 484 | 485 | // The SendMessage function sends the specified message to a 486 | // window or windows. It calls the window procedure for the specified 487 | // window and does not return until the window procedure 488 | // has processed the message. 489 | [DllImport("User32.dll")] 490 | public static extern Int32 SendMessage( 491 | IntPtr hWnd, // handle to destination window 492 | int Msg, // message 493 | int wParam, // first message parameter 494 | [MarshalAs(UnmanagedType.LPStr)] string lParam); 495 | // second message parameter 496 | 497 | [DllImport("User32.dll")] 498 | public static extern Int32 SendMessage( 499 | IntPtr hWnd, // handle to destination window 500 | int Msg, // message 501 | int wParam, // first message parameter 502 | int lParam); // second message parameter 503 | 504 | 505 | [DllImport("user32.dll")] 506 | public static extern IntPtr SendMessage( 507 | IntPtr hWnd, 508 | UInt32 msg, 509 | IntPtr wParam, 510 | IntPtr lParam); 511 | 512 | [DllImport("user32.dll")] 513 | public static extern UInt32 SendMessage( 514 | IntPtr hWnd, 515 | UInt32 msg, 516 | UInt32 wParam, 517 | UInt32 lParam); 518 | 519 | 520 | public static void Click(int x, int y) 521 | { 522 | Cursor.Position = new Point(x, y); 523 | //User32.mouse_event( User32.MOUSEEVENTF_ABSOLUTE | User32.MOUSEEVENTF_MOVE , x, y, 0, 0); 524 | User32.mouse_event(User32.MOUSEEVENTF_ABSOLUTE | User32.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0); 525 | System.Threading.Thread.Sleep(20); 526 | User32.mouse_event(User32.MOUSEEVENTF_ABSOLUTE | User32.MOUSEEVENTF_LEFTUP, x, y, 0, 0); 527 | System.Threading.Thread.Sleep(100); 528 | } 529 | 530 | 531 | [DllImport("user32.dll")] 532 | public static extern 533 | IntPtr FindWindowEx(IntPtr hWnd, IntPtr child, string classe, string window); 534 | 535 | [DllImport("user32.dll")] 536 | public static extern 537 | IntPtr FindWindowEx(IntPtr hWnd, int child, string classe, string window); 538 | 539 | [DllImport("user32.dll")] 540 | public static extern 541 | IntPtr FindWindow(string classe, string window); 542 | 543 | [DllImport("user32.dll")] 544 | public static extern 545 | IntPtr GetDesktopWindow(); 546 | 547 | [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 548 | public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); 549 | 550 | 551 | [DllImport("user32.dll")] 552 | public static extern 553 | IntPtr GetWindow(IntPtr hWnd, uint uCmd); 554 | 555 | [DllImport("user32.dll")] 556 | public static extern IntPtr GetParent(IntPtr hWnd); 557 | 558 | [DllImport("user32.dll", CharSet = CharSet.Auto)] 559 | public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, 560 | int nMaxCount); 561 | 562 | [DllImport("user32.dll")] 563 | public static extern int GetWindowRect(IntPtr hwnd, ref Rectangle rectangle); 564 | 565 | [DllImport("user32.dll")] 566 | public static extern bool IsWindowVisible(IntPtr hwnd); 567 | 568 | 569 | public static Rectangle GetWindowRect(IntPtr hwnd) 570 | { 571 | Rectangle tmp = new Rectangle(); 572 | GetWindowRect(hwnd, ref tmp); 573 | return new Rectangle(tmp.X, tmp.Y, tmp.Width - tmp.X, tmp.Height - tmp.Y); 574 | } 575 | 576 | [DllImport("user32.dll", CharSet = CharSet.Auto)] 577 | private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); 578 | 579 | //easy Wrapper pour GetClassName (StringBuilder...) 580 | public static string GetWindowText(IntPtr hWnd) 581 | { 582 | try 583 | { 584 | System.Text.StringBuilder lpClassName = new System.Text.StringBuilder(); 585 | GetWindowText(hWnd, lpClassName, 65536); 586 | string className = lpClassName.ToString(); 587 | return className; 588 | } 589 | catch 590 | { 591 | return ""; 592 | } 593 | 594 | } 595 | 596 | //easy Wrapper pour GetClassName (StringBuilder...) 597 | public static string GetClassName(IntPtr hWnd) 598 | { 599 | try 600 | { 601 | System.Text.StringBuilder lpClassName = new System.Text.StringBuilder(); 602 | GetClassName(hWnd, lpClassName, 65536); 603 | string className = lpClassName.ToString(); 604 | return className; 605 | } 606 | catch 607 | { 608 | return ""; 609 | } 610 | 611 | } 612 | 613 | //retourne la fenetre firstChild 614 | public static IntPtr FirstChildWnd(IntPtr root) 615 | { 616 | return FindWindowEx(root, new IntPtr(0), null, null); 617 | } 618 | //easy wrapper for GetWindowThreadProcessId (out...) 619 | public static uint GetProcessFromWnd(IntPtr hWnd) 620 | { 621 | uint testId; 622 | GetWindowThreadProcessId(hWnd, out testId); 623 | return testId; 624 | } 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | private static IntPtr FindWindowRecurse(IntPtr root, string className) 634 | { 635 | 636 | if (root == IntPtr.Zero 637 | || GetClassName(root) == className) 638 | return root; //sortie 639 | 640 | IntPtr next = IntPtr.Zero, 641 | testWnd = GetWindow(root, GW_CHILD); 642 | 643 | while (testWnd != IntPtr.Zero) 644 | { 645 | next = FindWindowRecurse(testWnd, className); 646 | if (next != IntPtr.Zero) 647 | return next; 648 | testWnd = GetWindow(testWnd, GW_HWNDNEXT); 649 | } 650 | return IntPtr.Zero; 651 | } 652 | 653 | public static IntPtr GetParentWnd_Class(IntPtr root, string className) 654 | { 655 | if (root == IntPtr.Zero 656 | || GetClassName(root) == className) 657 | return root; //sortie 658 | return GetParentWnd_Class(GetParent(root), className); 659 | } 660 | 661 | 662 | public static List GetWndsFromProcess(uint processId) 663 | { 664 | List result = new List(); 665 | 666 | IntPtr testWnd = FindWindow(null, null), test = IntPtr.Zero; 667 | do 668 | { 669 | 670 | if (processId != GetProcessFromWnd(testWnd)) 671 | continue; 672 | 673 | result.Add(testWnd); 674 | result.AddRange(GetChildrenWnd(testWnd)); 675 | 676 | } while ((testWnd = GetWindow(testWnd, GW_HWNDNEXT)) != IntPtr.Zero); 677 | return result; 678 | } 679 | 680 | 681 | 682 | public static IntPtr FindWindowByPid(uint processId) 683 | { 684 | 685 | IntPtr testWnd = FindWindow(null, null); 686 | bool visible; 687 | IntPtr parent = IntPtr.Zero; 688 | 689 | do 690 | { 691 | visible = IsWindowVisible(testWnd); 692 | parent = GetParent(testWnd); 693 | if (processId == GetProcessFromWnd(testWnd) && visible && parent == IntPtr.Zero) 694 | return testWnd; 695 | 696 | } while ((testWnd = GetWindow(testWnd, GW_HWNDNEXT)) != IntPtr.Zero); 697 | 698 | return IntPtr.Zero; 699 | } 700 | 701 | 702 | public static List GetChildrenWnd(IntPtr root) 703 | { 704 | List result = new List(); 705 | IntPtr testWnd = GetWindow(root, GW_CHILD); 706 | while (testWnd != IntPtr.Zero) 707 | { 708 | result.Add(testWnd); 709 | result.AddRange(GetChildrenWnd(testWnd)); 710 | testWnd = GetWindow(testWnd, GW_HWNDNEXT); 711 | } 712 | return result; 713 | } 714 | 715 | public static Process GetProcessFromWindow(string className) 716 | { 717 | foreach (Process p in Process.GetProcesses()) 718 | { 719 | if (GetClassName(p.MainWindowHandle) == className) 720 | return p; 721 | } 722 | return null; 723 | } 724 | 725 | public static IntPtr GetWndFromProcess(uint processId, List root, string className) 726 | { 727 | IntPtr test = IntPtr.Zero, testWnd; 728 | 729 | foreach (IntPtr tmp in root) 730 | { 731 | testWnd = tmp; 732 | do 733 | { 734 | //on peut etre parmis les fils de root MAIS avoir un parent... 735 | //if (GetParent(testWnd) != IntPtr.Zero) 736 | // continue; 737 | 738 | if (processId != GetProcessFromWnd(testWnd)) 739 | continue; 740 | 741 | 742 | test = FindWindowRecurse(testWnd, className); 743 | if (test != IntPtr.Zero) 744 | return test; 745 | 746 | } while ((testWnd = GetWindow(testWnd, GW_HWNDNEXT)) != IntPtr.Zero); 747 | } 748 | return IntPtr.Zero; 749 | } 750 | 751 | 752 | public static IntPtr GetWndFromProcess(uint processId, string className) 753 | { 754 | List roots = new List(); 755 | roots.Add(FindWindow(null, null)); 756 | 757 | return GetWndFromProcess(processId, roots, className); 758 | } 759 | 760 | public static Rectangle GetWindowsSize(IntPtr hwnd) 761 | { 762 | Rectangle tmp = new Rectangle(); 763 | GetWindowRect(hwnd, ref tmp); 764 | return tmp; 765 | } 766 | 767 | public static void StartScreenSaver() 768 | { 769 | SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0); 770 | } 771 | 772 | [DllImport("user32.dll", SetLastError = true)] 773 | [return: MarshalAs(UnmanagedType.Bool)] 774 | public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref uint pvParam, uint fWinIni); 775 | 776 | 777 | public static bool SystemParametersInfo(uint uiAction, bool uiParam, int pvParam, uint fWinIni) 778 | { 779 | uint result = 0; 780 | return SystemParametersInfo(uiAction, (uint)(uiParam ? 1 : 0), ref result, fWinIni); 781 | } 782 | 783 | [DllImport("user32.dll", EntryPoint = "FindWindow")] 784 | public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 785 | 786 | 787 | [DllImport("user32.dll")] 788 | public static extern bool MoveWindow(IntPtr hWnd, 789 | int X, 790 | int Y, 791 | int nWidth, 792 | int nHeight, 793 | bool bRepaint 794 | ); 795 | 796 | 797 | [DllImport("user32.dll")] 798 | public static extern bool DestroyWindow(IntPtr videoWnd); 799 | 800 | 801 | 802 | [DllImport("user32.dll")] 803 | public static extern bool AllowSetForegroundWindow(int dwProcessId); 804 | 805 | 806 | 807 | // PInvoke declaration for EnumDisplaySettings Win32 API 808 | [DllImport("user32.dll", CharSet = CharSet.Ansi)] 809 | internal static extern int EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode); 810 | 811 | // PInvoke declaration for ChangeDisplaySettings Win32 API 812 | [DllImport("user32.dll", CharSet = CharSet.Ansi)] 813 | internal static extern int ChangeDisplaySettings(ref DEVMODE lpDevMode, int dwFlags); 814 | 815 | 816 | public static void ReOrientDisplay(Orientation newOrient) 817 | { 818 | DisplaySettings CurrentDisplay = GetDisplaySettings(); 819 | if (newOrient == CurrentDisplay.Orientation) 820 | return; 821 | 822 | bool newYAxis = newOrient == Orientation.DMDO_DEFAULT || newOrient == Orientation.DMDO_180; 823 | bool currentYAxis = CurrentDisplay.Orientation == Orientation.DMDO_DEFAULT || CurrentDisplay.Orientation == Orientation.DMDO_180; 824 | 825 | if (newYAxis ^ currentYAxis) 826 | CurrentDisplay.Size = new Size(CurrentDisplay.Size.Height, CurrentDisplay.Size.Width); 827 | 828 | CurrentDisplay.Orientation = newOrient; 829 | 830 | // switch to new settings 831 | ChangeDisplaySettings(CurrentDisplay.DEVMODE); 832 | } 833 | 834 | private static void ChangeDisplaySettings(DEVMODE dm) 835 | { 836 | 837 | int iRet = ChangeDisplaySettings(ref dm, 0); 838 | switch (iRet) 839 | { 840 | case DISP_CHANGE_SUCCESSFUL: 841 | break; 842 | /* 843 | case DISP_CHANGE_RESTART: 844 | MessageBox.Show("Please restart your system"); 845 | break; 846 | case NativeMethods.DISP_CHANGE_FAILED: 847 | MessageBox.Show("ChangeDisplaySettigns API failed"); 848 | break; 849 | case NativeMethods.DISP_CHANGE_BADDUALVIEW: 850 | MessageBox.Show("The settings change was unsuccessful because system is DualView capable."); 851 | break; 852 | case NativeMethods.DISP_CHANGE_BADFLAGS: 853 | MessageBox.Show("An invalid set of flags was passed in."); 854 | break; 855 | case NativeMethods.DISP_CHANGE_BADPARAM: 856 | MessageBox.Show("An invalid parameter was passed in. This can include an invalid flag or combination of flags."); 857 | break; 858 | case NativeMethods.DISP_CHANGE_NOTUPDATED: 859 | MessageBox.Show("Unable to write settings to the registry."); 860 | break; 861 | * */ 862 | default: 863 | throw new Exception("Could save screen informations"); 864 | } 865 | } 866 | 867 | 868 | 869 | public struct DisplaySettings 870 | { 871 | public Size Size 872 | { 873 | get { return new Size(DEVMODE.dmPelsWidth, DEVMODE.dmPelsHeight); } 874 | set { DEVMODE.dmPelsHeight = value.Height; DEVMODE.dmPelsWidth = value.Width; } 875 | } 876 | public int Bits; 877 | public double Frequency; 878 | public int Id; 879 | 880 | public Point Position 881 | { 882 | get { return new Point(DEVMODE.dmPositionX, DEVMODE.dmPositionY); } 883 | set { DEVMODE.dmPelsHeight = value.X; DEVMODE.dmPelsWidth = value.Y; } 884 | } 885 | 886 | 887 | public Orientation Orientation 888 | { 889 | get { return FlipOrientation[DEVMODE.dmDisplayOrientation]; } 890 | set { DEVMODE.dmDisplayOrientation = (int)value; } 891 | } 892 | 893 | internal DEVMODE DEVMODE; 894 | } 895 | 896 | 897 | 898 | public static DisplaySettings GetDisplaySettings() 899 | { 900 | DEVMODE dm = CreateDevmode(); 901 | GetDisplaySettings(ref dm); 902 | return new DisplaySettings 903 | { 904 | Bits = dm.dmBitsPerPel, 905 | Frequency = dm.dmDisplayFrequency, 906 | DEVMODE = dm, 907 | }; 908 | 909 | } 910 | 911 | public static bool MaximizeWindow(string title) 912 | { 913 | int processId; 914 | IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, title); 915 | 916 | if(int.TryParse(title, out processId)) 917 | hwnd = FindWindowByPid((uint) processId); 918 | 919 | return ShowWindow(hwnd, SW_SHOWMAXIMIZED); 920 | } 921 | 922 | public static bool MinimizeWindow(string title) 923 | { 924 | IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, title); 925 | return ShowWindow(hwnd, SW_MINIMIZE); 926 | } 927 | 928 | private const int SW_MINIMIZE = 6; 929 | 930 | 931 | public static bool HideWindow(uint processId) 932 | { 933 | IntPtr hwnd = FindWindowByPid(processId); 934 | return ShowWindow(hwnd, SW_MINIMIZE); 935 | } 936 | 937 | 938 | public static bool ShowWindow(uint processId) 939 | { 940 | IntPtr hwnd = FindWindowByPid(processId); 941 | return ShowWindow(hwnd, SW_SHOWNORMAL); 942 | } 943 | 944 | 945 | private static int GetDisplaySettings(ref DEVMODE dm) 946 | { 947 | // helper to obtain current settings 948 | return GetDisplaySettings(ref dm, ENUM_CURRENT_SETTINGS); 949 | } 950 | 951 | private static int GetDisplaySettings(ref DEVMODE dm, int iModeNum) 952 | { 953 | // helper to wrap EnumDisplaySettings Win32 API 954 | return EnumDisplaySettings(null, iModeNum, ref dm); 955 | } 956 | 957 | 958 | // helper for creating an initialized DEVMODE structure 959 | internal static DEVMODE CreateDevmode() 960 | { 961 | DEVMODE dm = new DEVMODE(); 962 | dm.dmDeviceName = new String(new char[32]); 963 | dm.dmFormName = new String(new char[32]); 964 | dm.dmSize = (short)Marshal.SizeOf(dm); 965 | return dm; 966 | } 967 | } 968 | } 969 | -------------------------------------------------------------------------------- /src/cs/WinAPI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {A749D755-AF84-43FC-B8C5-41FAA9DE0054} 9 | Exe 10 | Properties 11 | WinAPI 12 | WinAPI 13 | v4.0 14 | 15 | 16 | 512 17 | 18 | 19 | x86 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | 28 | 29 | x86 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE 34 | prompt 35 | 4 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 65 | -------------------------------------------------------------------------------- /src/cs/WinAPI.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinAPI", "WinAPI.csproj", "{A749D755-AF84-43FC-B8C5-41FAA9DE0054}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|x86 = Debug|x86 9 | Release|x86 = Release|x86 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {A749D755-AF84-43FC-B8C5-41FAA9DE0054}.Debug|x86.ActiveCfg = Debug|x86 13 | {A749D755-AF84-43FC-B8C5-41FAA9DE0054}.Debug|x86.Build.0 = Debug|x86 14 | {A749D755-AF84-43FC-B8C5-41FAA9DE0054}.Release|x86.ActiveCfg = Release|x86 15 | {A749D755-AF84-43FC-B8C5-41FAA9DE0054}.Release|x86.Build.0 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /src/cs/WinAPI.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/131/node-winapi/100b95bdd935a1a7dda87c0302c17cf79674d6ed/src/cs/WinAPI.suo -------------------------------------------------------------------------------- /src/cs/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /src/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | // The width of the virtual screen, in pixels. 10 | static int vscreenWidth = -1; // not initialized 11 | 12 | // The height of the virtual screen, in pixels. 13 | static int vscreenHeight = -1; // not initialized 14 | 15 | // The coordinates for the left side of the virtual screen. 16 | static int vscreenMinX = 0; 17 | 18 | // The coordinates for the top of the virtual screen. 19 | static int vscreenMinY = 0; 20 | 21 | 22 | 23 | void GetLastInputInfo(const Nan::FunctionCallbackInfo& info) { 24 | 25 | LASTINPUTINFO li; 26 | li.cbSize = sizeof(LASTINPUTINFO); 27 | ::GetLastInputInfo(&li); 28 | 29 | int elapsed = li.dwTime; 30 | 31 | info.GetReturnValue().Set(Nan::New(elapsed)); 32 | } 33 | 34 | void GetTickCount(const Nan::FunctionCallbackInfo& info) { 35 | int elapsed = ::GetTickCount(); 36 | 37 | info.GetReturnValue().Set(Nan::New(elapsed)); 38 | } 39 | 40 | 41 | void CreateJobGroup(const Nan::FunctionCallbackInfo& info) { 42 | 43 | HANDLE hJob; 44 | HANDLE currentProcess; 45 | JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 }; 46 | PROCESS_INFORMATION pi = { 0 }; 47 | STARTUPINFO si = { 0 }; 48 | 49 | //Create a job object. 50 | hJob = CreateJobObject(NULL, NULL); 51 | 52 | //kill all processes associated with the job to terminate when the last handle to the job is closed. 53 | 54 | jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; 55 | SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)); 56 | currentProcess = GetCurrentProcess(); 57 | 58 | AssignProcessToJobObject(hJob, currentProcess); 59 | 60 | info.GetReturnValue().Set(Nan::Undefined()); 61 | } 62 | 63 | 64 | 65 | 66 | void ListProcessPID(const Nan::FunctionCallbackInfo& info) { 67 | int pid = -1; 68 | 69 | if (info.Length() < 1) 70 | pid = GetCurrentProcessId(); 71 | else { 72 | if (!info[0]->IsNumber() ) { 73 | Nan::ThrowTypeError("Wrong arguments"); 74 | return; 75 | } 76 | pid = Nan::To(info[0]).ToChecked(); 77 | } 78 | 79 | 80 | HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 81 | PROCESSENTRY32 pe = { 0 }; 82 | pe.dwSize = sizeof(PROCESSENTRY32); 83 | 84 | 85 | v8::Local children = Nan::New(); 86 | 87 | int i = 0; 88 | 89 | 90 | if( Process32First(h, &pe)) { 91 | do { 92 | if (pe.th32ParentProcessID == pid) 93 | Nan::Set(children, i++, Nan::New( (int) pe.th32ProcessID) ); 94 | } while( Process32Next(h, &pe)); 95 | } 96 | 97 | CloseHandle(h); 98 | 99 | info.GetReturnValue().Set(children); 100 | } 101 | 102 | 103 | void SetCursorPos(const Nan::FunctionCallbackInfo& info) { 104 | int x = Nan::To(info[0]).ToChecked();; 105 | int y = Nan::To(info[1]).ToChecked();; 106 | 107 | SetCursorPos(x, y); 108 | } 109 | 110 | 111 | void updateScreenMetrics() 112 | { 113 | vscreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN); 114 | vscreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN); 115 | vscreenMinX = GetSystemMetrics(SM_XVIRTUALSCREEN); 116 | vscreenMinY = GetSystemMetrics(SM_YVIRTUALSCREEN); 117 | } 118 | 119 | 120 | void moveMouse(const Nan::FunctionCallbackInfo& info) { 121 | 122 | 123 | if(vscreenWidth<0 || vscreenHeight<0) 124 | updateScreenMetrics(); 125 | 126 | int x = Nan::To(info[0]).ToChecked(); 127 | int y = Nan::To(info[1]).ToChecked(); 128 | 129 | INPUT mouseInput = {0}; 130 | mouseInput.type = INPUT_MOUSE; 131 | mouseInput.mi.dx = x; 132 | mouseInput.mi.dy = y; 133 | mouseInput.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK; 134 | mouseInput.mi.time = 0; //System will provide the timestamp 135 | 136 | SendInput(1, &mouseInput, sizeof(mouseInput)); 137 | } 138 | 139 | 140 | 141 | void getParentPid(const Nan::FunctionCallbackInfo& info) { 142 | 143 | int pid = -1; 144 | 145 | if (info.Length() < 1) 146 | pid = GetCurrentProcessId(); 147 | else { 148 | if (!info[0]->IsNumber() ) { 149 | Nan::ThrowTypeError("Wrong arguments"); 150 | return; 151 | } 152 | pid = Nan::To(info[0]).ToChecked(); 153 | } 154 | 155 | int parentPid = -1; 156 | 157 | HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 158 | PROCESSENTRY32 pe = { 0 }; 159 | pe.dwSize = sizeof(PROCESSENTRY32); 160 | 161 | if( Process32First(h, &pe)) { 162 | do { 163 | if (pe.th32ProcessID == pid) 164 | parentPid = pe.th32ParentProcessID; 165 | } while( Process32Next(h, &pe)); 166 | } 167 | 168 | CloseHandle(h); 169 | 170 | info.GetReturnValue().Set(parentPid); 171 | } 172 | 173 | void GetUTCTime(const Nan::FunctionCallbackInfo& info) 174 | { 175 | SYSTEMTIME system_time = {}; 176 | v8::Local ret_time = Nan::New(); 177 | 178 | GetSystemTime(&system_time); 179 | Nan::Set(ret_time, 0, Nan::New((unsigned int) system_time.wYear)); 180 | Nan::Set(ret_time, 1, Nan::New((unsigned int) system_time.wMonth - 1)); 181 | Nan::Set(ret_time, 2, Nan::New((unsigned int) system_time.wDay)); 182 | Nan::Set(ret_time, 3, Nan::New((unsigned int) system_time.wHour)); 183 | Nan::Set(ret_time, 4, Nan::New((unsigned int) system_time.wMinute)); 184 | Nan::Set(ret_time, 5, Nan::New((unsigned int) system_time.wSecond)); 185 | Nan::Set(ret_time, 6, Nan::New((unsigned int) system_time.wMilliseconds)); 186 | info.GetReturnValue().Set(ret_time); 187 | } 188 | 189 | void print_last_error(const char *err_str) 190 | { 191 | DWORD errorMessageID = GetLastError(); 192 | 193 | CHAR *messageBuffer = nullptr; 194 | size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 195 | FORMAT_MESSAGE_FROM_SYSTEM | 196 | FORMAT_MESSAGE_IGNORE_INSERTS, 197 | NULL, 198 | errorMessageID, 199 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 200 | (LPSTR) &messageBuffer, 201 | 0, 202 | NULL); 203 | 204 | printf("ERROR: %s\n %s", 205 | err_str, 206 | messageBuffer); 207 | LocalFree(messageBuffer); 208 | } 209 | 210 | void SetUTCTime(const Nan::FunctionCallbackInfo& info) 211 | { 212 | SYSTEMTIME system_time = {}; 213 | v8::Local ret_bool; 214 | 215 | system_time.wYear = (WORD) Nan::To(info[0]).ToChecked(); 216 | system_time.wMonth = (WORD) Nan::To(info[1]).ToChecked() + 1; 217 | system_time.wDayOfWeek = 0; 218 | system_time.wDay = (WORD) Nan::To(info[2]).ToChecked(); 219 | system_time.wHour = (WORD) Nan::To(info[3]).ToChecked(); 220 | system_time.wMinute = (WORD) Nan::To(info[4]).ToChecked(); 221 | system_time.wSecond = (WORD) Nan::To(info[5]).ToChecked(); 222 | system_time.wMilliseconds = (WORD) Nan::To(info[6]).ToChecked(); 223 | if (SetSystemTime(&system_time) == TRUE) 224 | ret_bool = Nan::New(true); 225 | else 226 | { 227 | print_last_error("SetSystemTime:"); 228 | ret_bool = Nan::New(false); 229 | } 230 | info.GetReturnValue().Set(ret_bool); 231 | } 232 | 233 | void Init(v8::Local exports) { 234 | v8::Local context = exports->CreationContext(); 235 | 236 | exports->Set(context, Nan::New("CreateJobGroup").ToLocalChecked(), Nan::New(CreateJobGroup)->GetFunction(context).ToLocalChecked()); 237 | exports->Set(context, Nan::New("GetLastInputInfo").ToLocalChecked(), Nan::New(GetLastInputInfo)->GetFunction(context).ToLocalChecked()); 238 | exports->Set(context, Nan::New("GetTickCount").ToLocalChecked(), Nan::New(GetTickCount)->GetFunction(context).ToLocalChecked()); 239 | exports->Set(context, Nan::New("GetChildrenProcess").ToLocalChecked(), Nan::New(ListProcessPID)->GetFunction(context).ToLocalChecked()); 240 | exports->Set(context, Nan::New("GetParentProcess").ToLocalChecked(), Nan::New(getParentPid)->GetFunction(context).ToLocalChecked()); 241 | 242 | 243 | exports->Set(context, Nan::New("SetCursorPos").ToLocalChecked(), Nan::New(SetCursorPos)->GetFunction(context).ToLocalChecked()); 244 | exports->Set(context, Nan::New("moveMouse").ToLocalChecked(), Nan::New(moveMouse)->GetFunction(context).ToLocalChecked()); 245 | 246 | exports->Set(context, Nan::New("GetUTCTime").ToLocalChecked(), Nan::New(GetUTCTime)->GetFunction(context).ToLocalChecked()); 247 | exports->Set(context, Nan::New("SetUTCTime").ToLocalChecked(), Nan::New(SetUTCTime)->GetFunction(context).ToLocalChecked()); 248 | } 249 | 250 | 251 | 252 | 253 | 254 | 255 | NODE_MODULE(winapi, Init) 256 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const winapi = require('./'); 4 | 5 | 6 | var start = winapi.GetLastInputInfo(); 7 | 8 | 9 | console.log(start ); 10 | 11 | 12 | winapi.CreateJobGroup(); 13 | 14 | var cp = require('child_process'); 15 | 16 | var child = cp.spawn("notepad.exe", { 17 | detached: true, 18 | stdio: 'ignore' 19 | }); 20 | 21 | child.unref(); 22 | 23 | 24 | setInterval(Function.prototype, 1000); -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const os = require('os'); 4 | 5 | const expect = require("expect.js"); 6 | 7 | const winapi = require('../'); 8 | 9 | 10 | const isContainer = (os.userInfo().username == 'ContainerAdministrator'); 11 | 12 | describe("Initial test suite", function(){ 13 | 14 | it("should list pids", function(){ 15 | 16 | var foo = winapi.GetChildrenProcess( winapi.GetParentProcess() ); 17 | 18 | expect(foo.indexOf(process.pid)).not.to.eql(-1); 19 | }); 20 | 21 | 22 | it("Should measure 1s of inactity", function(done){ 23 | 24 | var start = winapi.GetLastInputInfo(); 25 | console.log("DONT MOVE"); 26 | 27 | setTimeout(function(){ 28 | var now = winapi.GetLastInputInfo(); 29 | expect(now).to.equal(start); 30 | done(); 31 | }, 1000); 32 | 33 | }); 34 | 35 | 36 | if(!isContainer) it("Should measure actity", function(done){ 37 | this.timeout(5000); 38 | 39 | var start = winapi.GetLastInputInfo(); 40 | 41 | console.log("Press any key, you have 2s to comply"); 42 | 43 | setTimeout(function(){ 44 | winapi.moveMouse(0,0); 45 | }, 1000); 46 | 47 | setTimeout(function(){ 48 | winapi.SetCursorPos(0,0); 49 | var now = winapi.GetLastInputInfo(); 50 | expect(now).not.to.equal(start); 51 | 52 | console.log("You took %s to do stuff", (now - start) / 1000) 53 | done(); 54 | }, 2000); 55 | 56 | }); 57 | 58 | 59 | if(!isContainer) it("Last idle Time should be recent", function(){ 60 | var since = winapi.getIdleTime(); 61 | console.log("System idle since ", since/1000); 62 | expect(since < 2000).to.be.ok(); 63 | 64 | }); 65 | 66 | 67 | it("Get system time", function() { 68 | var value = winapi.GetUTCTime(); 69 | var now = new Date(); 70 | 71 | console.log("System time is ", value); 72 | 73 | expect(value).to.be.ok(); 74 | expect(value[0]).to.eql(now.getUTCFullYear()); 75 | expect(value[1]).to.eql(now.getUTCMonth()); 76 | expect(value[2]).to.eql(now.getUTCDate()); 77 | expect(value[3]).to.eql(now.getUTCHours()); 78 | }); 79 | 80 | /*if(!isContainer) it("Set system time", function() { 81 | var now = new Date(); 82 | var new_date = [ 83 | now.getUTCFullYear() + 1, 84 | now.getUTCMonth(), 85 | now.getUTCDate(), 86 | now.getUTCHours(), 87 | now.getUTCMinutes(), 88 | now.getUTCSeconds(), 89 | now.getUTCMilliseconds() 90 | ]; 91 | 92 | var after = new Date(); 93 | 94 | console.log('sending', new_date, 'to set date'); 95 | 96 | var result = winapi.SetUTCTime(new_date); 97 | 98 | console.log("compare dates :", now, after); 99 | 100 | expect(result).to.be.ok(); 101 | expect(now.getUTCFullYear()).to.eql(after.getUTCFullYear() + 1); 102 | });*/ 103 | 104 | this.timeout(20 * 1000); 105 | 106 | if(false) it("should check screen orientation", function(done){ 107 | var initial; 108 | 109 | winapi.GetDisplaySettings(function(err, value){ 110 | initial = value.Orientation; 111 | var newval = (initial+1) %4; 112 | console.log({initial, newval}); 113 | winapi.ReOrientDisplay(newval, function(err, value){ 114 | 115 | winapi.GetDisplaySettings(function(err, value){ 116 | expect(value.Orientation).to.eql(newval); 117 | winapi.ReOrientDisplay(initial, done); 118 | }); 119 | }); 120 | }); 121 | }); 122 | 123 | 124 | 125 | 126 | }); -------------------------------------------------------------------------------- /test/test_nw/.gitignore: -------------------------------------------------------------------------------- 1 | .nwjscache 2 | -------------------------------------------------------------------------------- /test/test_nw/index.html: -------------------------------------------------------------------------------- 1 | Bo 2 | -------------------------------------------------------------------------------- /test/test_nw/index.js: -------------------------------------------------------------------------------- 1 | var cp = require('child_process'); 2 | 3 | const gui = window.require('nw.gui'); 4 | 5 | var winapi = require('../../'); 6 | 7 | 8 | winapi.CreateJobGroup(); 9 | 10 | 11 | setInterval(function() { 12 | gui.Window.get().window.console.log("loo", winapi.GetLastInputInfo()) 13 | //console.log("Last input is " + winapi.GetLastInputInfo()); 14 | }, 1000); 15 | 16 | gui.Window.get().showDevTools(); 17 | 18 | 19 | var child = cp.spawn("notepad.exe", { 20 | detached: true, 21 | stdio: 'ignore' 22 | }); 23 | 24 | child.unref(); 25 | 26 | -------------------------------------------------------------------------------- /test/test_nw/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "winapitester", 3 | "main" : "index.html", 4 | 5 | "window" : { 6 | "show" : true 7 | 8 | }, 9 | 10 | "chromium-args": "--mixed-context --remote-debugging-port=0 --user-data-dir=./.nwjscache" 11 | 12 | 13 | 14 | } --------------------------------------------------------------------------------