⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memview.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
            if( obj[i].rva > rva ) break;
        }
        i--;
        if( i == -1 ) {
            objname = "";
        } else {
            objname = obj[i].name;
        }
    }
    info->data[info->used] = createMemListItem( objname, name, mbi );
    info->used++;
    if( mnode != NULL && i < mnode->num_objects - 1 ) {
        i++;
        /*
         * if this region contains more than one object the split it
         */
        if( obj[i].rva < rva + mbi->RegionSize ) {
            new = info->data[info->used - 1];
            topass = *mbi;
            new->mbi.RegionSize = obj[i].rva - rva;
            topass.BaseAddress = (char *)new->mbi.BaseAddress
                                 + new->mbi.RegionSize;
            topass.RegionSize = mbi->RegionSize - new->mbi.RegionSize;
            addMemListItem( pnode, info, &topass );
        }
    }
}

/*
 * FreeMemListItems
 */
static void freeMemListItems( MemListData *info ) {

    DWORD       i;

    for( i=0; i < info->used; i++ ) {
        MemFree( info->data[i] );
    }
    info->used = 0;
}


void FreeMemList( MemListData *info ) {
    freeMemListItems( info );
    MemFree( info->data );
    info->allocated = 0;
    info->data = NULL;
}

void RefreshMemList( DWORD procid, HANDLE prochdl, MemListData *proclist ) {

    DWORD                       offset;
    MEMORY_BASIC_INFORMATION    mbi;
    ProcNode                    *pnode;

    offset = 0;
    pnode = FindProcess( procid );
    for( ;; ) {
        VirtualQueryEx( prochdl, (LPVOID)offset, &mbi,
                        sizeof( MEMORY_BASIC_INFORMATION ) );
        if( offset != (DWORD)mbi.BaseAddress ) break;
        addMemListItem( pnode, proclist, &mbi );
        offset = (DWORD)mbi.BaseAddress + mbi.RegionSize;
    }
    if( pnode != NULL ) {
        identifyAllStacks( pnode, proclist );
    }
}

/*
 * viewMem
 */
static void viewMem( MemWalkerInfo *info ) {

    LRESULT                     index;
    MEMORY_BASIC_INFORMATION    *mbi;
    char                        buf[100];
    BOOL                        ret;

    index = SendMessage( GetListBoxHwnd( info->lbox ), LB_GETCURSEL, 0, 0 );
    mbi = &info->listdata.data[index]->mbi;
    if( mbi->State == MEM_FREE || mbi->State == MEM_RESERVE ) {
        RCMessageBox( GetListBoxHwnd( info->lbox ), STR_MEM_NOT_COMMITTED,
                      AppName, MB_OK | MB_ICONEXCLAMATION );
    } else {
        ret = ReadProcessMemory( info->prochdl, mbi->BaseAddress,
                                 buf, 1, NULL );
        if( ret ) {
            if( info->stats != NULL ) {
                RCsprintf( buf, STR_MEM_RANGE_NAME_PROC,
                    (DWORD)mbi->BaseAddress,
                    (DWORD)mbi->BaseAddress + mbi->RegionSize - 1,
                    info->procid, info->stats->name );
            } else {
                RCsprintf( buf, STR_MEM_RANGE_UNNAMED_PROC,
                    (DWORD)mbi->BaseAddress,
                    (DWORD)mbi->BaseAddress + mbi->RegionSize - 1,
                    info->procid );
            }
            DispNTMem( GetListBoxHwnd( info->lbox ), Instance,
                info->prochdl, (DWORD)mbi->BaseAddress,
                (DWORD)mbi->BaseAddress + mbi->RegionSize, buf );
        } else {
            RCsprintf( buf, STR_CANT_READ_MEM_AT_X, (DWORD)mbi->BaseAddress );
            MessageBox( GetListBoxHwnd( info->lbox ), buf, AppName,
                        MB_OK | MB_ICONEXCLAMATION );
        }
    }
}

LONG CALLBACK MemWalkerProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
    MemWalkerInfo       *info;
    WORD                cmd;
    SIZE                txtsize;
    HDC                 dc;
    char                buf[150];

    info = (MemWalkerInfo *)GetWindowLong( hwnd, 0 );
    switch ( msg ) {
    case WM_CREATE:
        info = (MemWalkerInfo *)( (CREATESTRUCT *)lparam )->lpCreateParams;
        SetWindowLong( hwnd, 0, (DWORD)info );
        info->listdata.data = NULL;
        info->listdata.allocated = 0;
        info->listdata.used = 0;
        info->labels = CreateWindow(
            "static",    /* Window class name */
            "",    /* Window caption */
            WS_CHILD,/* Window style */
            CW_USEDEFAULT,          /* Initial X position */
            0,                      /* Initial Y position */
            0,                      /* Initial X size */
            0,                      /* Initial Y size */
            hwnd,                   /* Parent window handle */
            NULL,                   /* Window menu handle */
            Instance,               /* Program instance handle */
            NULL );                 /* Create parameters */
        ShowWindow( info->labels, SW_SHOWNORMAL );
        SetMonoFont( info->labels );
        dc = GetDC( info->labels );
        GetTextExtentPoint( dc, MEM_WALKER_HEADER, 1, &txtsize );
        ReleaseDC( info->labels, dc );
        info->label_hite = txtsize.cy;
        SetWindowText( info->labels, MEM_WALKER_HEADER );
        info->lbox = CreateListBox( hwnd );
        RefreshMemList( info->procid, info->prochdl, &info->listdata );
        redrawMemList( info->lbox, &info->listdata );
        SendMessage( GetListBoxHwnd( info->lbox ), LB_SETCURSEL, 0, 0 );
        SetFocus( GetListBoxHwnd( info->lbox ) );
        info->stats = MemAlloc( sizeof( ProcStats ) );
        if( GetProcessInfo( info->procid, info->stats ) ) {
            RCsprintf( buf, STR_MEM_WLK_NAMED_PROC, info->procid,
                        info->stats->name );
        } else {
            RCsprintf( buf, STR_MEM_WLK_UNNAMED_PROC, info->procid,
                        info->stats->name );
            MemFree( info->stats );
            info->stats = NULL;
        }
        SetWindowText( hwnd, buf );
        break;
    case WM_SIZE:
        MoveListBox( info->lbox, 0, info->label_hite, LOWORD( lparam ),
                     HIWORD( lparam ) - info->label_hite );
        MoveWindow( info->labels, 0, 0, LOWORD( lparam ), info->label_hite,
                    TRUE );
        break;
    case WM_COMMAND:
        cmd = LOWORD( wparam );
        switch( cmd ) {
        case LISTBOX_1:
            if( HIWORD( wparam ) == LBN_DBLCLK ) {
                viewMem( info );
            }
            break;
        }
        break;
    case WM_DESTROY:
        CloseHandle( info->prochdl );
        FreeMemList( &info->listdata );
        if( info->stats != NULL ) {
            MemFree( info->stats );
        }
        MemFree( info );
        curWalkHwnd = NULL;
        PostMessage( GetWindow( hwnd, GW_OWNER ), STAT_FOREGROUND, 0, 0 );
        break;
    default:
        return( DefWindowProc( hwnd, msg, wparam, lparam ) );
    }
    return( 0L );
}

BOOL RegisterMemWalker( void ) {

    WNDCLASS            wc;

    wc.style = 0L;
    wc.lpfnWndProc = MemWalkerProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 4;
    wc.hInstance = Instance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor( NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = MEM_WALKER_CLASS;
    return( RegisterClass( &wc ) );
}

void WalkMemory( HWND parent, HANDLE hdl, DWORD procid ) {

    MemWalkerInfo       *info;
    int                 resp;
    char                *title;

    if( curWalkHwnd != NULL ) {
        resp = RCMessageBox( parent, STR_MEM_WLK_IN_PROGRESS,
                             AppName, MB_YESNO | MB_ICONQUESTION );
        if( resp == IDYES ) {
            DestroyWindow( curWalkHwnd );
        } else {
            return;
        }
    }
    info = MemAlloc( sizeof( MemWalkerInfo ) );
    info->prochdl = hdl;
    info->procid = procid;
    title = AllocRCString( STR_MEMORY_WALKER_TITLE );
    curWalkHwnd = CreateWindow(
        MEM_WALKER_CLASS,               /* Window class name */
        title,                          /* Window caption */
        WS_OVERLAPPED | WS_CAPTION
        | WS_SYSMENU | WS_THICKFRAME
        | WS_MAXIMIZEBOX,               /* Window style */
        CW_USEDEFAULT,                  /* Initial X position */
        0,                              /* Initial Y position */
        CW_USEDEFAULT,                  /* Initial X size */
        0,                              /* Initial Y size */
        parent,                         /* Parent window handle */
        NULL,                           /* Window menu handle */
        Instance,                       /* Program instance handle */
        info );                         /* Create parameters */
    FreeRCString( title );
    ShowWindow( curWalkHwnd, SW_SHOWNORMAL );
    UpdateWindow( curWalkHwnd );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -