Hirdetés

Új hozzászólás Aktív témák

  • pmonitor
    aktív tag

    Futtattam pénteken egy tesztet: ~300K process indításból csak kb 3000 egyedi PID volt. HWND egyezést csak egyet sikerült előidéznem, azt közvetlenül a gép újraindítása után.
    C# (form) kód:
    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.IO;

    namespace pidtest {
    public partial class Form1 : Form {
    [DllImport("user32.dll", SetLastError = true)]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
    public Form1() { InitializeComponent(); }

    private void Form1_Load(object sender, EventArgs e) {
    try {
    uint pid;
    IntPtr hwnd = Handle;
    GetWindowThreadProcessId(Handle, out pid);
    using (StreamWriter w = File.AppendText(@"c:\temp\pidtest.txt")) {
    w.WriteLine("{0} {1}", pid.ToString(), hwnd.ToString());
    } }
    finally { Close(); }
    }
    }
    }

    python teszter:
    import os
    a=0
    f=open("c:/temp/pidtest.txt","r");A=f.readlines()
    while 1:
    os.startfile("c:/temp/pidtest.exe")
    A+=f.readlines()
    if len(A) != len({*A}): break
    if a%100==0:
    B,C=zip(*[a.split()for a in A])
    D,E={*B},{*C}
    print(f'Items: {len(B)} - Unique: {len(D)} PID, {len(E)} HWND', end='\r', flush=1)
    a+=1
    f.close()

    Készítettem tesztprogramot C-ben, csak nem tudom, hogy mikor tudom hosszasan futtatni. pidtesztA:
    #include <windows.h>

    HINSTANCE hInstance = NULL;
    char* AppName = "PID teszt \"A\"";
    char* WindowClassName = "pidtesztA";
    HWND AppHwnd = NULL;

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

    int main()
    {
    WNDCLASSEX wc;
    MSG msg;
    hInstance = GetModuleHandle(NULL);
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hbrBackground = COLOR_BTNFACE + 1;
    wc.hInstance = hInstance;
    wc.lpszMenuName = NULL;
    wc.lpszClassName = WindowClassName;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(0, IDC_ARROW);
    RegisterClassEx(&wc);
    AppHwnd = CreateWindowEx(0, WindowClassName, AppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1200, 900, 0, 0, hInstance, 0);
    ShowWindow(AppHwnd, SW_SHOWDEFAULT);
    UpdateWindow(AppHwnd);
    while (GetMessage(&msg, 0, 0, 0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }

    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    switch (uMsg)
    {
    case WM_CREATE:
    ShowWindow(AppHwnd, SW_NORMAL);
    break;
    case WM_COMMAND:
    break;
    case WM_SIZE:
    {
    break;
    }
    case WM_SHOWWINDOW:
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
    break;
    }

    }

    pidtesztB:
    #include <stdio.h>
    #include <stdlib.h>
    #include <Windows.h>

    STARTUPINFOA SI;
    PROCESS_INFORMATION PI;
    HWND Pids[400000000];
    int PidsP = 0;
    int waitmsec = 500;

    int appStart(char name[])
    {
    if (!CreateProcessA(NULL, name, NULL, NULL, FALSE, 0, NULL, NULL, &SI, &PI))
    {
    printf("A %s file-t nem lehet megnyitni...\n", name);
    return -1;
    }
    WaitForSingleObject(PI.hProcess, waitmsec);
    return 0;
    }



    int main(int argc, char* argv[])
    {
    DWORD procid = 0, hwndid;
    ZeroMemory(&SI, sizeof(SI));
    SI.cb = sizeof(SI);
    ZeroMemory(&PI, sizeof(PI));
    if (argc == 3) waitmsec = atoi(argv[2]);
    char appA[50] = "pidtesztA.exe";
    char appAClass[50] = "pidtesztA";
    if (!appStart(appA))
    {
    HWND hwnd = FindWindowExA(NULL, NULL, appAClass, NULL);
    while (hwnd)
    {
    int pidact = 0;
    int tid = GetWindowThreadProcessId(hwnd, &pidact);
    Pids[PidsP] = pidact;
    PidsP++;
    hwnd = FindWindowExA(NULL, hwnd, appAClass, NULL);
    }
    TerminateProcess(PI.hProcess, 0);
    }
    HWND prochwnd;
    int i, out = 0, imax = 500;
    if (argc >= 2) imax = atoi(argv[1]);
    for (i = 0; i < imax; i++)
    {
    ZeroMemory(&SI, sizeof(SI));
    SI.cb = sizeof(SI);
    ZeroMemory(&PI, sizeof(PI));
    if (!appStart(appA))
    {
    HWND hwnd = FindWindowExA(NULL, NULL, appAClass, NULL);
    while (hwnd)
    {
    int pidact = 0;
    int tid = GetWindowThreadProcessId(hwnd, &pidact);
    int j = 0;
    while (j < PidsP)
    {
    if (Pids[j] == pidact)
    {
    printf("Pid egyezik!\n");
    out = 1;
    break;
    }
    j++;
    }
    if (out) break;
    Pids[PidsP] = pidact;
    PidsP++;
    hwnd = FindWindowExA(NULL, hwnd, appAClass, NULL);
    }
    TerminateProcess(PI.hProcess, 0);
    if (out) break;
    }
    }
    printf("i: %d\n", i);
    return 0;
    }

    Mint látható az "A" egy standard window. A "B" console. Az indítási intervallum, és a waitforsingleobject() ideje is alapból 500, de parancssorból változtatható, mint az a kódból is látható. A szám adatok validitását nem ellenőriztem le, csak egyből konvertál(legalábbis megpróbál :) ).
    Még nem tudtam hosszan futtatni. Ha vki. működésbeli hibát vesz észre, Ő kérem jelezze.

Új hozzászólás Aktív témák