Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Forcing a DebugBreak()


March 1999/Forcing a DebugBreak()/Listing 2

Listing 2: isdbgex.cpp — Implementation of IsDebuggerPresentEx()

// IsDbgEx.cpp
//    Windows platform independent implementation of
//    IsDebuggerPresent.

/* disable unnecessary level-4 warnings */
#pragma warning(disable : 4201)  //nameless struct/union
#pragma warning(disable : 4214)  //bit field types other than int
#pragma warning(disable : 4514)  //unreferenced inline function

/* include files */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "IsDbgEx.h"

/* required link libraries */
#pragma comment(lib, "kernel32")

/* private type definitions */
typedef BOOL (WINAPI * ISDEBUGGERPRESENTPROC)(VOID);

/* private function prototypes */
BOOL IsDebuggerPresent95(VOID);

//------------------------------------------------------------------
//IsDebuggerPresentEx()
//------------------------------------------------------------------
BOOL IsDebuggerPresentEx(VOID)
{
    /* CONSTANTS */
    static const ISDEBUGGERPRESENTPROC fnIsDebuggerPresent = 
        (ISDEBUGGERPRESENTPROC)GetProcAddress(
            GetModuleHandle(TEXT("KERNEL32")), 
            TEXT("IsDebuggerPresent"));

    /* TEMPORARY VARIABLES */
    BOOL fDebuggerPresent = FALSE;

    /* use documented API if available */
    if (fnIsDebuggerPresent != NULL)
    {
        fDebuggerPresent = fnIsDebuggerPresent();
    }
    /* otherwise use undocumented Win95 technique */
    else
    {
        fDebuggerPresent = IsDebuggerPresent95();
    }
    /* done */
    return fDebuggerPresent;
}

//------------------------------------------------------------------
//IsDebuggerPresent95()
//    Returns TRUE if process is being debugged, FALSE otherwise.
//    Note that this should only be used under Windows 95.  
//    Windows NT and Windows 98 should use the documented 
//    IsDebuggerPresent() API.
//    See chapter 3 of 'Windows 95 System Programming Secrets' by 
//    Matt Pietrek for details on the obfuscator and the structure 
//    of the process database.
//------------------------------------------------------------------
BOOL IsDebuggerPresent95(VOID)
{
    #ifdef _M_IX86
    /* CONSTANTS */
    static const DWORD FLAG_DEBUGGERPRESENT = 0x00000001;
    static const UINT  cbProcessDatabase = 190;
    static const UINT  dwOffsetFlags = 8;
    /* TEMPORARY VARIABLES */
    const DWORD  dwThreadId = GetCurrentThreadId();
    const DWORD  dwProcessId = GetCurrentProcessId();
    LPVOID       pProcessDatabase = NULL;
    DWORD        obfuscator = 0;
    DWORD        dwFlags = 0;
    BOOL         fIsDebuggerPresent = FALSE;

    /* retrieve the obfuscator */
    __asm
    {
        mov ax, fs
        mov es, ax
        mov eax, 18h
        mov eax, es:[eax]
        sub eax, 10h
        xor eax, [dwThreadId]
        mov [obfuscator], eax
    }
    /* locate the process database and validate */
    pProcessDatabase = (VOID *)(dwProcessId ^ obfuscator);
    if (!IsBadReadPtr(pProcessDatabase, cbProcessDatabase))
    {
        /* get process flags and determine if debugger present */
        dwFlags = ((DWORD *)pProcessDatabase)[dwOffsetFlags];
        fIsDebuggerPresent = dwFlags & FLAG_DEBUGGERPRESENT;
    }
    /* done */
    return fIsDebuggerPresent;
    #else //!_M_IX86
    SetLastError(ERROR_NOT_IMPLEMENTED);
    return FALSE;
    #endif //!_M_IX86
}

/* End of file */

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.