Play with Bindings

本文介绍了一段使用D语言编写的简单程序,该程序能够枚举当前系统中的所有进程,并展示了如何利用Windows API进行进程快照创建及遍历。通过绑定tlhelp32.dll中的函数,实现了进程信息的获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D语言是新兴语言。个人感觉相当友善,而且更新速度快。 下面是做一个简单的列出进程的程序是tlhelp32的binding windows/core.d绑定代码,略有冗余 module windows.core; import std.c.windows.windows; enum { DELETE = 0x00010000L, READ_CONTROL = 0x00020000L, SYNCHRONIZE = 0x00100000L, //Required to wait for the process to terminate using the wait functions. WRITE_DAC = 0x00040000L, WRITE_OWNER = 0x00080000L, } enum { PROCESS_ALL_ACCESS = 0x1F0FFF, // All possible access rights for a process object. PROCESS_CREATE_PROCESS = 0x0080, // Required to create a process. PROCESS_CREATE_THREAD = 0x0002, // Required to create a thread. PROCESS_DUP_HANDLE = 0x0040, // Required to duplicate a handle using DuplicateHandle. PROCESS_QUERY_INFORMATION = 0x0400, // Required to retrieve certain information about a process, such as its token, exit code, and priority class (see OpenProcessToken, GetExitCodeProcess, GetPriorityClass, and IsProcessInJob). PROCESS_QUERY_LIMITED_INFORMATION = 0x1000,// Required to retrieve certain information about a process (see QueryFullProcessImageName). PROCESS_SET_QUOTA = 0x0100, // Required to set memory limits using SetProcessWorkingSetSize. PROCESS_SET_INFORMATION = 0x0200, // Required to set certain information about a process, such as its priority class (see SetPriorityClass). PROCESS_SUSPEND_RESUME = 0x0800, // Required to suspend or resume a process. PROCESS_TERMINATE = 0x0001, // Required to terminate a process using TerminateProcess. PROCESS_VM_OPERATION = 0x0008, // Required to perform an operation on the address space of a process (see VirtualProtectEx and WriteProcessMemory). PROCESS_VM_READ = 0x0010, // Required to read memory in a process using ReadProcessMemory. PROCESS_VM_WRITE = 0x0020, // Required to write to memory in a process using WriteProcessMemory. } enum { TH32CS_SNAPHEAPLIST = 0x00000001, TH32CS_SNAPPROCESS = 0x00000002, TH32CS_SNAPTHREAD = 0x00000004, TH32CS_SNAPMODULE = 0x00000008, TH32CS_SNAPMODULE32 = 0x00000010, TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST|TH32CS_SNAPPROCESS|TH32CS_SNAPTHREAD|TH32CS_SNAPMODULE), TH32CS_INHERIT = 0x80000000, } typedef char TCHAR; struct PROCESSENTRY32 { DWORD dwSize; DWORD cntUsage; DWORD th32ProcessID; DWORD th32DefaultHeapID; DWORD th32ModuleID; DWORD cntThreads; DWORD th32ParentProcessID; LONG pcPriClassBase; DWORD dwFlags; TCHAR szExeFile[MAX_PATH]; DWORD th32MemoryBase; DWORD th32AccessKey; }; typedef PROCESSENTRY32* PPROCESSENTRY32; typedef PROCESSENTRY32* LPPROCESSENTRY32; export extern (Windows){ DWORD GetWindowThreadProcessId( // The return value is the identifier of the thread that created the window. HWND hWnd, // Handle to the window. LPDWORD lpdwProcessId // Pointer to a variable that receives the process identifier. ); HWND FindWindowEx( HWND hwndParent, //Handle to the parent window whose child windows are to be searched. HWND hwndChildAfter, //Handle to a child window. LPCTSTR lpszClass, //Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. LPCTSTR lpszWindow //Pointer to a null-terminated string that specifies the window name (the window's title). ); HWND FindWindow( LPCTSTR lpClassName, //Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. LPCTSTR lpWindowName //Pointer to a null-terminated string that specifies the window name (the window's title). ); BOOL Process32First( HANDLE hSnapshot, //Handle to the snapshot returned from a previous call to the CreateToolhelp32Snapshot function. LPPROCESSENTRY32 lppe //Pointer to a PROCESSENTRY32 structure. ); BOOL Process32Next( HANDLE hSnapshot, // Handle to the snapshot returned from a previous call to the CreateToolhelp32Snapshot function. LPPROCESSENTRY32 lppe // Pointer to a PROCESSENTRY32 structure. ); HANDLE CreateToolhelp32Snapshot( DWORD dwFlags, //Portions of the system to include in the snapshot. DWORD th32ProcessID //Process identifier of the process to be included in the snapshot. ); HANDLE OpenProcess( DWORD dwDesiredAccess, //The access to the process object. BOOL bInheritHandle, //If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle. DWORD dwProcessId //The identifier of the local process to be opened. ); } 列出我们的进程哦~ import std.c.windows.windows; import std.stdio; import windows.core; void main() { HWND a; PROCESSENTRY32 lppe; a=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); writefln(a); lppe.dwSize=PROCESSENTRY32.sizeof; if (Process32First(a, cast(LPPROCESSENTRY32)&lppe) ) writefln ("hello"); while( Process32Next(a,cast(LPPROCESSENTRY32)&lppe) ) { printf("%s/n",&lppe.szExeFile); } }
2025-07-15 14:28:55.708 1573-1586 memtrack system_server E Couldn't load memtrack module 2025-07-15 14:28:56.419 1352-1352 SELinux servicemanager E avc: denied { find } for service=tab pid=2761 uid=10051 scontext=u:r:untrusted_app:s0:c51,c256,c512,c768 tcontext=u:object_r:default_android_service:s0 tclass=service_manager permissive=1 2025-07-15 14:28:57.008 2761-2790 eglCodecCommon org.cocos2d.demo E glUtilsParamSize: unknow param 0x000082da 2025-07-15 14:28:57.008 2761-2790 eglCodecCommon org.cocos2d.demo E glUtilsParamSize: unknow param 0x000082e5 2025-07-15 14:28:57.013 2761-2790 eglCodecCommon org.cocos2d.demo E glUtilsParamSize: unknow param 0x00008c29 2025-07-15 14:28:57.014 2761-2790 eglCodecCommon org.cocos2d.demo E glUtilsParamSize: unknow param 0x000087fe 2025-07-15 14:28:57.037 2761-2790 EGL_emulation org.cocos2d.demo E tid 2790: eglSurfaceAttrib(1493): error 0x3009 (EGL_BAD_MATCH) 2025-07-15 14:28:58.749 2761-2788 jswrapper org.cocos2d.demo E ScriptEngine::onGetStringFromFile stream not found, possible missing file. 2025-07-15 14:28:58.749 2761-2788 jswrapper org.cocos2d.demo E ScriptEngine::runScript script stream, buffer is empty! 2025-07-15 14:28:58.749 2761-2788 jswrapper org.cocos2d.demo E [ERROR] Failed to invoke require, location: C:/ProgramData/cocos/editors/Creator/2.4.13/resources/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:299 2025-07-15 14:28:58.785 2761-2788 jswrapper org.cocos2d.demo E ScriptEngine::evalString catch exception: 2025-07-15 14:28:58.805 2761-2788 jswrapper org.cocos2d.demo E ERROR: Uncaught ReferenceError: self is not defined, location: src/assets/_plugs/lib/gravityengine.mg.cocoscreator.min.dbb97.js:0:0 STACK: [0]anonymous@src/assets/_plugs/lib/gravityengine.mg.cocoscreator.min.dbb97.js:2 [1]anonymous@src/assets/_plugs/lib/gravityengine.mg.cocoscreator.min.dbb97.js:3 [2]anonymous@jsb-adapter/jsb-engine.js:2975 [3]download@jsb-adapter/jsb-engine.js:2984 [4]downloadScript@jsb-adapter/jsb-engine.js:2971 [5]a@src/cocos2d-jsb.28d62.js:16668 [6]anonymous@src/cocos2d-jsb.28d62.js:16678 [7]retry@src/cocos2d-jsb.28d62.js:18111 [8]download@src/cocos2d-jsb.28d62.js:16663 [9]load@src/cocos2d-jsb.28d62.js:17318 [10]94.e.exports@src/cocos2d-jsb.28d62.js:17134 [11]_flow@src/cocos2d-jsb.28d62.js:17579 [12]async@src/cocos2d-jsb.28d62.js:17574 [13]anonymous@src/cocos2d-jsb.28d62.js:17261 [14]forEach@src/cocos2d-jsb.28d62.js:18189 [15]94.e.exports@src/cocos2d-jsb.28d62.js:17244 [16]_flow@src/cocos2d-jsb.28d62.js:17579 [17]anonymous@src/cocos2d-jsb.28d62.js:17586 [18]98.e.exports@src/cocos2d-jsb.2 2025-07-15 14:28:58.807 2761-2788 jswrapper org.cocos2d.demo E ScriptEngine::evalString script src/assets/_plugs/lib/gravityengine.mg.cocoscreator.min.dbb97.js, failed! 2025-07-15 14:28:58.807 2761-2788 jswrapper org.cocos2d.demo E [ERROR] Failed to invoke require, location: C:/ProgramData/cocos/editors/Creator/2.4.13/resources/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:299 2025-07-15 14:29:01.667 1450-1471 storaged storaged E getDiskStats failed with result NOT_SUPPORTED and size 0 2025-07-15 14:29:11.348 1715-2033 bt_btif com.android.bluetooth E register_notification_rsp: Avrcp device is not connected, handle: 0x0 2025-07-15 14:29:11.348 1715-2033 bt_btif com.android.bluetooth E register_notification_rsp: Avrcp device is not connected, handle: 0x0 2025-07-15 14:29:11.476 1452-1452 OMXNodeInstance media.codec E setConfig(0xf5210060:google.mp3.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001) 2025-07-15 14:29:11.476 1452-1452 OMXNodeInstance media.codec E getConfig(0xf5210060:google.mp3.decoder, ConfigAndroidVendorExtension(0x6f100004)) ERROR: Undefined(0x80001001) 解决问题
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值