Windows 驅動:擷取當前進程名

來源:互聯網
上載者:User

 這是一個比較簡單的問題,在 REGON 的源碼中可以找到實現的相關代碼,我只是把它們整理封裝了一下。

//
// Process name max length: by bytes
// (This value is 16 bytes in RegMon)
//
#define MAX_PROC_NAME_LEN 256
//
// This is the offset into a KPEB of the current process name. This is determined
// dynamically by scanning the process block belonging to the GUI for its name.
//
ULONG                   ProcessNameOffset = 0;

//----------------------------------------------------------------------
//
// GetProcessNameOffset
//
// In an effort to remain version-independent, rather than using a
// hard-coded into the KPEB (Kernel Process Environment Block), we
// scan the KPEB looking for the name, which should match that
// of the GUI process
//
//----------------------------------------------------------------------
ULONG
GetProcessNameOffset(
    VOID
    )
{
    PEPROCESS       curproc;
    int             i;

    curproc = PsGetCurrentProcess();

    //
    // Scan for 12KB, hopping the KPEB never grows that big!
    //
    for( i = 0; i < 3*PAGE_SIZE; i++ ) {
    
        if( !strncmp( "System", (PCHAR) curproc + i, strlen("System") )) {

            return i;
        }
    }

    //
    // Name not found - oh, well
    //
    return 0;
}

//----------------------------------------------------------------------
//
// initialization interface
//
//----------------------------------------------------------------------
//
// initialize the ProcessNameOffset when the driver is loading.
// (Call in DriverEntry())
//
NTSTATUS
ProcessInfo_LoadInit()
{
 ProcessNameOffset = GetProcessNameOffset();
 return STATUS_SUCCESS;
}

//----------------------------------------------------------------------
//
// GetCurrentProcessName
//
// Uses undocumented data structure offsets to obtain the name of the
// currently executing process.
//
//----------------------------------------------------------------------
PCHAR
GetCurrentProcessName()
{
    PEPROCESS       curproc;
    char            *nameptr;
    ULONG           i;
 static CHAR  szName[MAX_PROC_NAME_LEN];

    //
    // We only try and get the name if we located the name offset
    //
    if( ProcessNameOffset ) {
   
        //
        // Get a pointer to the current process block
        //
        curproc = PsGetCurrentProcess();

        //
        // Dig into it to extract the name. Make sure to leave enough room
        // in the buffer for the appended process ID.
        //
        nameptr   = (PCHAR) curproc + ProcessNameOffset;
        strncpy( szName, nameptr, MAX_PROC_NAME_LEN-1 );
        szName[MAX_PROC_NAME_LEN-1] = 0;
  /* for 64 bit system
#if defined(_M_IA64)
        sprintf( szName + strlen(szName), ":%I64d", PsGetCurrentProcessId());
#else
        sprintf( szName + strlen(szName), ":%d", (ULONG) PsGetCurrentProcessId());
#endif
  //*/

    } else {
    
        strcpy( szName, "???");
    }
    return szName;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.