Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ EnumWindows()不';找不到顶级窗口?_C++_C_Winapi_Process - Fatal编程技术网

C++ EnumWindows()不';找不到顶级窗口?

C++ EnumWindows()不';找不到顶级窗口?,c++,c,winapi,process,C++,C,Winapi,Process,我正在尝试创建一个程序,它的功能类似于任务管理器以及许多其他功能。目前,我在使用当前枚举函数查找所有顶级窗口时遇到问题。出于某些原因,它正确地枚举并填写了某些应用程序(例如Google Chrome、命令提示符、代码::Blocks)的HWND,但不适用于某些游戏(例如Roblox(我只测试了一款))。我试着看看FindWindow()是否也会失败,但在这种情况下它工作得很好。这意味着EnumWindows()显然应该找到它,但很明显,我不是做错了什么,就是在文档中读错了什么。我真的不想使用Fi

我正在尝试创建一个程序,它的功能类似于任务管理器以及许多其他功能。目前,我在使用当前枚举函数查找所有顶级窗口时遇到问题。出于某些原因,它正确地枚举并填写了某些应用程序(例如Google Chrome、命令提示符、代码::Blocks)的HWND,但不适用于某些游戏(例如Roblox(我只测试了一款))。我试着看看FindWindow()是否也会失败,但在这种情况下它工作得很好。这意味着EnumWindows()显然应该找到它,但很明显,我不是做错了什么,就是在文档中读错了什么。我真的不想使用FindWindow(),因为我可能根本不知道大多数windows的名称

枚举函数:

BOOL CALLBACK FindWindows( HWND handle, LPARAM option )
{
    DWORD window_process_id = 0;
    GetWindowThreadProcessId( handle, &window_process_id );
    process_list * p1 = NULL;

    switch ( option )
    {
        case FIND_WINDOW_HANDLE :
            if ( IsWindowEnabled( handle ) )
                for ( p1 = head_copy; p1; p1 = p1->next )
                    if ( p1->pid == window_process_id )
                        p1->window_handle = handle;
        break;

        default :
            SetLastError( ERROR_INVALID_PARAMETER );
            return 0;
        break;
    }

    return TRUE;
}
/* Preprocessor directives */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>

#define TARGET_PROCESS "chrome.exe"

/* Structures */

typedef struct process_list
{
    char * process_name;
    DWORD pid;
    HANDLE process_handle;
    HWND window_handle;

    int process_name_sz;

    struct process_list * next;
} process_list;

typedef struct drawing_data
{
    RECT window_pos;
} drawing_data;

/* Enums ( Global integer constants ) */

enum
{
    FIND_WINDOW_HANDLE
};

enum
{
    TIMER_START,
    TIMER_STOP,
    TIMER_GETDIFF
};

typedef struct t_timer
{
    clock_t start_time;
    clock_t end_time;
} t_timer;

/* Global variables */

process_list * head_copy = NULL;

/* ***************************************************************** */
/* Time functions */

clock_t timer( int command, t_timer * timer1 )
{
    switch ( command )
    {
        case TIMER_START :
            timer1->start_time = clock( );
        break;

        case TIMER_STOP :
            timer1->end_time = clock( );
        break;

        case TIMER_GETDIFF :
            return ( ( timer1->end_time - timer1->start_time ) / ( CLOCKS_PER_SEC / 1000 ));
        break;

        default : break;
    }

    return -1;
}

/* ***************************************************************** */
/* Windows error functions */

void show_error( char * user_string, BOOL exit )
{
    char buffer[BUFSIZ] = { 0 };
    DWORD error_code = GetLastError( );

    FormatMessage
    (
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        error_code,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) buffer,
        BUFSIZ,
        NULL
    );

    printf( "%s : %s", user_string, buffer );

    if ( exit ) ExitProcess( error_code );

    return;
}

/* ***************************************************************** */

void win_error( char * message, BOOL exit )
{
    char buffer[BUFSIZ] = { 0 };
    DWORD error_code = GetLastError( );

    FormatMessage
    (
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        error_code,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) buffer,
        BUFSIZ,
        NULL
    );

    MessageBox(NULL, buffer, "Error from System :", MB_ICONWARNING | MB_OK );

    if ( exit ) ExitProcess( error_code );

    return;
}

/* ***************************************************************** */
/* Linked list functions */

process_list * create( )
{
    process_list * temp = NULL;

    if ( !( temp = malloc( sizeof( process_list ) ) ) )
    {
        perror("Malloc");
        exit( 1 );
    }

    return temp;
}

/* ***************************************************************** */

process_list * add( process_list * head, HANDLE process_handle, PROCESSENTRY32 * process_structure )
{
    process_list * temp = NULL;

    if ( !head )
    {
        head = create( );

        head->pid = process_structure->th32ParentProcessID;
        head->process_handle = process_handle;
        head->process_name_sz = strlen( process_structure->szExeFile ) + 1;
        head->process_name = malloc( head->process_name_sz );

        if ( !head->process_name )
        {
            perror( "Malloc" );
            exit( 1 );
        }

        strcpy( head->process_name, process_structure->szExeFile );
        head->next = NULL;
    } else
    {
        temp = create( );
        temp->next = head;
        head = temp;

        head->pid = process_structure->th32ParentProcessID;
        head->process_handle = process_handle;
        head->process_name_sz = strlen( process_structure->szExeFile ) + 1;
        head->process_name = malloc( head->process_name_sz );

        if ( !head->process_name )
        {
            perror( "Malloc" );
            exit( 1 );
        }

        strcpy( head->process_name, process_structure->szExeFile );
    }

    return head;
}

/* ***************************************************************** */

void print_list( process_list * head )
{
    process_list * p1 = NULL;

    for ( p1 = head; p1; p1 = p1->next  )
    {
        printf(
               "-------------------------------------------------\n"
               "node.process_name\t=\t%s\n"
               "node.process_id\t\t=\t%d\n"
               "\nCan terminate process : %s\n\n"
               "node.window_handle\t=\t0x%p\n"
               "node.next\t\t=\t%s\n",
               p1->process_name,
               ( int )p1->pid,
               p1->process_handle == INVALID_HANDLE_VALUE ? "NO" : "YES",
               ( void * )p1->window_handle,
               p1->next ? "(node address)\n" : "NULL"
              );
    }
}

/* ***************************************************************** */

void print_node( process_list * node )
{
    printf(
            "node.process_name\t=\t%s\n"
            "node.process_id\t\t=\t%d\n"
            "\nCan terminate process : %s\n\n"
            "node.window_handle\t=\t0x%p\n"
            "node.next\t\t=\t%s\n",
            node->process_name,
            ( int )node->pid,
            node->process_handle == INVALID_HANDLE_VALUE ? "NO" : "YES",
            ( void * )node->window_handle,
            node->next ? "(node address)\n" : "NULL"
           );

    return;
}

/* ***************************************************************** */

process_list * delete( process_list * head, process_list * node )
{
    process_list * p1 = head;
    process_list * p2 = NULL;

    if ( !p1 )
        return NULL;
    else if ( p1 == node )
    {
        if ( !p1->next )
        {
            free( p1->process_name );
            if ( p1->process_handle != INVALID_HANDLE_VALUE )
                CloseHandle( p1->process_handle );
            if ( p1->window_handle )
                CloseHandle( p1->window_handle );

            free( p1 );
        }
        else
        {
            free( p1->process_name );
            if ( p1->process_handle != INVALID_HANDLE_VALUE )
                CloseHandle( p1->process_handle );
            if ( p1->window_handle )
                CloseHandle( p1->window_handle );

            p2 = head->next;
            free( p1 );

            return p2;
        }

        return NULL;
    }

    for ( ; p1 && p1 != node; p2 = p1, p1 = p1->next );

    if ( !p1 )
        return NULL;
    else
    {
        free( p1->process_name );
        if ( p1->process_handle != INVALID_HANDLE_VALUE )
            CloseHandle( p1->process_handle );
        if ( p1->window_handle )
            CloseHandle( p1->window_handle );

        p2->next = p1->next;
        free( p1 );
    }

    return head;
}

/* ***************************************************************** */

void free_list( process_list * head )
{
    process_list * p1 = head;
    process_list * p2 = NULL;

    while ( p1 )
    {
        free(  p1->process_name );
        if ( p1->process_handle != INVALID_HANDLE_VALUE )
            CloseHandle( p1->process_handle );
        if ( p1->window_handle )
            CloseHandle( p1->window_handle );

        p2 = p1;
        p1 = p1->next;

        free( p2 );
    }

    return;
}

/* ***************************************************************** */

process_list * find_process_and_copy_node( process_list * head, const char * process_name )
{
    BOOL is_match = FALSE;
    process_list * p1 = NULL;
    process_list * new_node = NULL;

    for ( p1 = head; p1; p1 = p1->next )
    {
        if ( !strcmp( p1->process_name, process_name ) )
        {
            is_match = TRUE;
            break;
        }
    }

    if ( is_match )
    {
        new_node = create( );
        new_node->pid = p1->pid;
        new_node->process_handle = p1->process_handle;

        if ( !( new_node->process_name = malloc( p1->process_name_sz ) ) )
        {
            perror( "Malloc" );
            free( new_node );
            free_list( head );
            exit( 1 );
        }

        new_node->process_name = strcpy( new_node->process_name, p1->process_name );
        new_node->process_name_sz = p1->process_name_sz;
        new_node->window_handle = p1->window_handle;

        new_node->next = NULL;

        return new_node;
    }
    else return NULL;

}

/* ***************************************************************** */
/* WinAPI functions */

BOOL CALLBACK FindWindows( HWND handle, LPARAM option )
{
    DWORD window_process_id = 0;
    GetWindowThreadProcessId( handle, &window_process_id );
    process_list * p1 = NULL;

    switch ( option )
    {
        case FIND_WINDOW_HANDLE :
            if ( IsWindowEnabled( handle ) )
                for ( p1 = head_copy; p1; p1 = p1->next )
                    if ( p1->pid == window_process_id )
                        p1->window_handle = handle;
        break;

        default :
            SetLastError( ERROR_INVALID_PARAMETER );
            return 0;
        break;
    }

    return TRUE;
}

/* ***************************************************************** */

process_list * get_process_list( process_list * head )
{
    HANDLE h_process_snap;
    HANDLE h_process;
    PROCESSENTRY32 process_structure;

    h_process_snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

    if( h_process_snap == INVALID_HANDLE_VALUE )
    {
        show_error( "CreateToolhelp32Snapshot", FALSE );
        return NULL;
    }

    process_structure.dwSize = sizeof( PROCESSENTRY32 );

    if( !Process32First( h_process_snap, &process_structure ) )
    {
        show_error( "Process32First", FALSE );
        CloseHandle( h_process_snap );
        return NULL;
    }

    do
    {
        h_process = OpenProcess( PROCESS_TERMINATE, FALSE, process_structure.th32ProcessID );

        if ( h_process )
            head = add( head, h_process, &process_structure );
        else
            head = add( head, INVALID_HANDLE_VALUE, &process_structure );
    } while( Process32Next( h_process_snap, &process_structure ) );

    CloseHandle( h_process_snap );
    return head;
}

/* ***************************************************************** */

process_list * find_process( const char * process_name )
{
    process_list * head = NULL;
    process_list * target_process = NULL;

    if ( !( head = get_process_list( head ) ) )
        exit( 1 );

    head_copy = head;

    if ( !EnumWindows( FindWindows, FIND_WINDOW_HANDLE ) )
        win_error( "EnumWindows", FALSE );

    target_process = find_process_and_copy_node( head, TARGET_PROCESS );
    free_list( head );

    return target_process;
}

/* ***************************************************************** */

int main( )
{
    t_timer program_run_time;
    memset( &program_run_time, 0, sizeof( t_timer ) );
    timer( TIMER_START, &program_run_time );

    process_list * target_process = NULL;

    printf("Searching for target process...\n");

    while ( !( target_process = find_process( TARGET_PROCESS ) ) )
        Sleep( 100 );

    printf("Found!\n\n");
    print_node( target_process );

    timer( TIMER_STOP, &program_run_time );
    printf( "\n\n\t--\tProgram run time : %d milliseconds\t--\t\n\n", ( int )timer( TIMER_GETDIFF, &program_run_time ) );

    free( target_process->process_name );
    free( target_process );

    return 0;
}
完整来源:

BOOL CALLBACK FindWindows( HWND handle, LPARAM option )
{
    DWORD window_process_id = 0;
    GetWindowThreadProcessId( handle, &window_process_id );
    process_list * p1 = NULL;

    switch ( option )
    {
        case FIND_WINDOW_HANDLE :
            if ( IsWindowEnabled( handle ) )
                for ( p1 = head_copy; p1; p1 = p1->next )
                    if ( p1->pid == window_process_id )
                        p1->window_handle = handle;
        break;

        default :
            SetLastError( ERROR_INVALID_PARAMETER );
            return 0;
        break;
    }

    return TRUE;
}
/* Preprocessor directives */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>

#define TARGET_PROCESS "chrome.exe"

/* Structures */

typedef struct process_list
{
    char * process_name;
    DWORD pid;
    HANDLE process_handle;
    HWND window_handle;

    int process_name_sz;

    struct process_list * next;
} process_list;

typedef struct drawing_data
{
    RECT window_pos;
} drawing_data;

/* Enums ( Global integer constants ) */

enum
{
    FIND_WINDOW_HANDLE
};

enum
{
    TIMER_START,
    TIMER_STOP,
    TIMER_GETDIFF
};

typedef struct t_timer
{
    clock_t start_time;
    clock_t end_time;
} t_timer;

/* Global variables */

process_list * head_copy = NULL;

/* ***************************************************************** */
/* Time functions */

clock_t timer( int command, t_timer * timer1 )
{
    switch ( command )
    {
        case TIMER_START :
            timer1->start_time = clock( );
        break;

        case TIMER_STOP :
            timer1->end_time = clock( );
        break;

        case TIMER_GETDIFF :
            return ( ( timer1->end_time - timer1->start_time ) / ( CLOCKS_PER_SEC / 1000 ));
        break;

        default : break;
    }

    return -1;
}

/* ***************************************************************** */
/* Windows error functions */

void show_error( char * user_string, BOOL exit )
{
    char buffer[BUFSIZ] = { 0 };
    DWORD error_code = GetLastError( );

    FormatMessage
    (
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        error_code,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) buffer,
        BUFSIZ,
        NULL
    );

    printf( "%s : %s", user_string, buffer );

    if ( exit ) ExitProcess( error_code );

    return;
}

/* ***************************************************************** */

void win_error( char * message, BOOL exit )
{
    char buffer[BUFSIZ] = { 0 };
    DWORD error_code = GetLastError( );

    FormatMessage
    (
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        error_code,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) buffer,
        BUFSIZ,
        NULL
    );

    MessageBox(NULL, buffer, "Error from System :", MB_ICONWARNING | MB_OK );

    if ( exit ) ExitProcess( error_code );

    return;
}

/* ***************************************************************** */
/* Linked list functions */

process_list * create( )
{
    process_list * temp = NULL;

    if ( !( temp = malloc( sizeof( process_list ) ) ) )
    {
        perror("Malloc");
        exit( 1 );
    }

    return temp;
}

/* ***************************************************************** */

process_list * add( process_list * head, HANDLE process_handle, PROCESSENTRY32 * process_structure )
{
    process_list * temp = NULL;

    if ( !head )
    {
        head = create( );

        head->pid = process_structure->th32ParentProcessID;
        head->process_handle = process_handle;
        head->process_name_sz = strlen( process_structure->szExeFile ) + 1;
        head->process_name = malloc( head->process_name_sz );

        if ( !head->process_name )
        {
            perror( "Malloc" );
            exit( 1 );
        }

        strcpy( head->process_name, process_structure->szExeFile );
        head->next = NULL;
    } else
    {
        temp = create( );
        temp->next = head;
        head = temp;

        head->pid = process_structure->th32ParentProcessID;
        head->process_handle = process_handle;
        head->process_name_sz = strlen( process_structure->szExeFile ) + 1;
        head->process_name = malloc( head->process_name_sz );

        if ( !head->process_name )
        {
            perror( "Malloc" );
            exit( 1 );
        }

        strcpy( head->process_name, process_structure->szExeFile );
    }

    return head;
}

/* ***************************************************************** */

void print_list( process_list * head )
{
    process_list * p1 = NULL;

    for ( p1 = head; p1; p1 = p1->next  )
    {
        printf(
               "-------------------------------------------------\n"
               "node.process_name\t=\t%s\n"
               "node.process_id\t\t=\t%d\n"
               "\nCan terminate process : %s\n\n"
               "node.window_handle\t=\t0x%p\n"
               "node.next\t\t=\t%s\n",
               p1->process_name,
               ( int )p1->pid,
               p1->process_handle == INVALID_HANDLE_VALUE ? "NO" : "YES",
               ( void * )p1->window_handle,
               p1->next ? "(node address)\n" : "NULL"
              );
    }
}

/* ***************************************************************** */

void print_node( process_list * node )
{
    printf(
            "node.process_name\t=\t%s\n"
            "node.process_id\t\t=\t%d\n"
            "\nCan terminate process : %s\n\n"
            "node.window_handle\t=\t0x%p\n"
            "node.next\t\t=\t%s\n",
            node->process_name,
            ( int )node->pid,
            node->process_handle == INVALID_HANDLE_VALUE ? "NO" : "YES",
            ( void * )node->window_handle,
            node->next ? "(node address)\n" : "NULL"
           );

    return;
}

/* ***************************************************************** */

process_list * delete( process_list * head, process_list * node )
{
    process_list * p1 = head;
    process_list * p2 = NULL;

    if ( !p1 )
        return NULL;
    else if ( p1 == node )
    {
        if ( !p1->next )
        {
            free( p1->process_name );
            if ( p1->process_handle != INVALID_HANDLE_VALUE )
                CloseHandle( p1->process_handle );
            if ( p1->window_handle )
                CloseHandle( p1->window_handle );

            free( p1 );
        }
        else
        {
            free( p1->process_name );
            if ( p1->process_handle != INVALID_HANDLE_VALUE )
                CloseHandle( p1->process_handle );
            if ( p1->window_handle )
                CloseHandle( p1->window_handle );

            p2 = head->next;
            free( p1 );

            return p2;
        }

        return NULL;
    }

    for ( ; p1 && p1 != node; p2 = p1, p1 = p1->next );

    if ( !p1 )
        return NULL;
    else
    {
        free( p1->process_name );
        if ( p1->process_handle != INVALID_HANDLE_VALUE )
            CloseHandle( p1->process_handle );
        if ( p1->window_handle )
            CloseHandle( p1->window_handle );

        p2->next = p1->next;
        free( p1 );
    }

    return head;
}

/* ***************************************************************** */

void free_list( process_list * head )
{
    process_list * p1 = head;
    process_list * p2 = NULL;

    while ( p1 )
    {
        free(  p1->process_name );
        if ( p1->process_handle != INVALID_HANDLE_VALUE )
            CloseHandle( p1->process_handle );
        if ( p1->window_handle )
            CloseHandle( p1->window_handle );

        p2 = p1;
        p1 = p1->next;

        free( p2 );
    }

    return;
}

/* ***************************************************************** */

process_list * find_process_and_copy_node( process_list * head, const char * process_name )
{
    BOOL is_match = FALSE;
    process_list * p1 = NULL;
    process_list * new_node = NULL;

    for ( p1 = head; p1; p1 = p1->next )
    {
        if ( !strcmp( p1->process_name, process_name ) )
        {
            is_match = TRUE;
            break;
        }
    }

    if ( is_match )
    {
        new_node = create( );
        new_node->pid = p1->pid;
        new_node->process_handle = p1->process_handle;

        if ( !( new_node->process_name = malloc( p1->process_name_sz ) ) )
        {
            perror( "Malloc" );
            free( new_node );
            free_list( head );
            exit( 1 );
        }

        new_node->process_name = strcpy( new_node->process_name, p1->process_name );
        new_node->process_name_sz = p1->process_name_sz;
        new_node->window_handle = p1->window_handle;

        new_node->next = NULL;

        return new_node;
    }
    else return NULL;

}

/* ***************************************************************** */
/* WinAPI functions */

BOOL CALLBACK FindWindows( HWND handle, LPARAM option )
{
    DWORD window_process_id = 0;
    GetWindowThreadProcessId( handle, &window_process_id );
    process_list * p1 = NULL;

    switch ( option )
    {
        case FIND_WINDOW_HANDLE :
            if ( IsWindowEnabled( handle ) )
                for ( p1 = head_copy; p1; p1 = p1->next )
                    if ( p1->pid == window_process_id )
                        p1->window_handle = handle;
        break;

        default :
            SetLastError( ERROR_INVALID_PARAMETER );
            return 0;
        break;
    }

    return TRUE;
}

/* ***************************************************************** */

process_list * get_process_list( process_list * head )
{
    HANDLE h_process_snap;
    HANDLE h_process;
    PROCESSENTRY32 process_structure;

    h_process_snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

    if( h_process_snap == INVALID_HANDLE_VALUE )
    {
        show_error( "CreateToolhelp32Snapshot", FALSE );
        return NULL;
    }

    process_structure.dwSize = sizeof( PROCESSENTRY32 );

    if( !Process32First( h_process_snap, &process_structure ) )
    {
        show_error( "Process32First", FALSE );
        CloseHandle( h_process_snap );
        return NULL;
    }

    do
    {
        h_process = OpenProcess( PROCESS_TERMINATE, FALSE, process_structure.th32ProcessID );

        if ( h_process )
            head = add( head, h_process, &process_structure );
        else
            head = add( head, INVALID_HANDLE_VALUE, &process_structure );
    } while( Process32Next( h_process_snap, &process_structure ) );

    CloseHandle( h_process_snap );
    return head;
}

/* ***************************************************************** */

process_list * find_process( const char * process_name )
{
    process_list * head = NULL;
    process_list * target_process = NULL;

    if ( !( head = get_process_list( head ) ) )
        exit( 1 );

    head_copy = head;

    if ( !EnumWindows( FindWindows, FIND_WINDOW_HANDLE ) )
        win_error( "EnumWindows", FALSE );

    target_process = find_process_and_copy_node( head, TARGET_PROCESS );
    free_list( head );

    return target_process;
}

/* ***************************************************************** */

int main( )
{
    t_timer program_run_time;
    memset( &program_run_time, 0, sizeof( t_timer ) );
    timer( TIMER_START, &program_run_time );

    process_list * target_process = NULL;

    printf("Searching for target process...\n");

    while ( !( target_process = find_process( TARGET_PROCESS ) ) )
        Sleep( 100 );

    printf("Found!\n\n");
    print_node( target_process );

    timer( TIMER_STOP, &program_run_time );
    printf( "\n\n\t--\tProgram run time : %d milliseconds\t--\t\n\n", ( int )timer( TIMER_GETDIFF, &program_run_time ) );

    free( target_process->process_name );
    free( target_process );

    return 0;
}
/*预处理器指令*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义目标_进程“chrome.exe”
/*结构*/
类型定义结构进程列表
{
字符*进程名称;
德沃德pid;
处理过程\处理;
窗口把手;
int process_name_sz;
结构进程列表*下一步;
}进程列表;
typedef结构图_数据
{
矩形窗口位置;
}绘制数据;
/*枚举(全局整数常量)*/
枚举
{
查找窗口句柄
};
枚举
{
计时器启动时,
计时器(u)停止,,
计时器
};
typedef结构t_计时器
{
时钟没有开始时间;
时钟结束时间;
}定时器;
/*全局变量*/
进程列表*头副本=NULL;
/* ***************************************************************** */
/*时间函数*/
时钟定时器(int命令,时钟定时器*定时器1)
{
开关(命令)
{
案例计时器\u启动:
定时器1->开始时间=时钟();
打破
机箱计时器\u停止:
timer1->end_time=clock();
打破
案例计时器\u GETDIFF:
返回((timer1->end_time-timer1->start_time)/(时钟每秒/1000次));
打破
默认:中断;
}
返回-1;
}
/* ***************************************************************** */
/*Windows错误函数*/
无效显示错误(字符*用户字符串,布尔退出)
{
字符缓冲区[BUFSIZ]={0};
DWORD error_code=GetLastError();
格式化消息
(
格式化来自\u系统的\u消息\u,
无效的
错误代码,
MAKELANGID(LANG_中立,SUBLANG_默认),
(LPTSTR)缓冲器,
布夫西兹,
无效的
);
printf(“%s:%s”,用户字符串,缓冲区);
如果(退出)退出进程(错误代码);
回来
}
/* ***************************************************************** */
无效win_错误(字符*消息,布尔退出)
{
字符缓冲区[BUFSIZ]={0};
DWORD error_code=GetLastError();
格式化消息
(
格式化来自\u系统的\u消息\u,
无效的
错误代码,
MAKELANGID(LANG_中立,SUBLANG_默认),
(LPTSTR)缓冲器,
布夫西兹,
无效的
);
MessageBox(NULL,缓冲区,“来自系统的错误:”,MB|U ICONWARNING | MB|U OK);
如果(退出)退出进程(错误代码);
回来
}
/* ***************************************************************** */
/*链表函数*/
进程列表*创建()
{
进程列表*temp=NULL;
如果(!(temp=malloc(sizeof(进程列表)))
{
佩罗尔(“马洛克”);
出口(1);
}
返回温度;
}
/* ***************************************************************** */
进程列表*添加(进程列表*头,句柄进程句柄,PROCESSENTRY32*进程结构)
{
进程列表*temp=NULL;
如果(!头)
{
head=create();
head->pid=进程\结构->th32ParentProcessID;
head->process\u handle=process\u handle;
head->process\u name\u sz=strlen(process\u structure->szExeFile)+1;
head->process\u name=malloc(head->process\u name\u sz);
如果(!head->process\u name)
{
佩罗尔(“马洛克”);
出口(1);
}
strcpy(head->process\u name,process\u structure->szExeFile);
head->next=NULL;
}否则
{
temp=create();
温度->下一步=头部;
压头=温度;
head->pid=进程\结构->th32ParentProcessID;
head->process\u handle=process\u handle;
head->process\u name\u sz=strlen(process\u structure->szExeFile)+1;
head->process\u name=malloc(head->process\u name\u sz);
如果(!head->process\u name)
{
佩罗尔(“马洛克”);
出口(1);
}
strcpy(head->process\u name,process\u structure->szExeFile);
}
回流头;
}
/* ***************************************************************** */
作废打印列表(流程列表*头)
{
进程列表*p1=NULL;
用于(p1=头部;p1;p1=p1->next)
{
printf(
“-------------------------------------------------------------\n”
“node.process\u name\t=\t%s\n”
“node.process\u id\t\t=\t%d\n”
“\n可以终止进程:%s\n\n”
“node.window\u handle\t=\t0x%p\n”
“node.next\t\t=\t%s\n”,
p1->进程名称,
(int)p1->pid,
p1->process\u handle==无效的\u handle\u值?“否”:“是”,
(void*)p1->窗口句柄,
p1->下一步?”(节点地址)\n:“空”
);
}
}
/* ***************************************************************** */
作废打印节点(流程列表*节点)
{
printf(
“node.process\u name\t=\t%s\n”
“node.process\u id\t\t=\t%d\n”
“\n可以终止进程:%s\n\n”
“node.window\u handle\t=\t0x%p\n”
“node.next\t\t=\t%s\n”,
节点->进程名称,
(int)节点->pid,
节点->进程\u句柄==无效的\u句柄\u值?“否”:“是”,
(void*)节点->窗口句柄