2017-05-28 6 views
2

これはHow to install a DebugBreak handler?およびHow to get a declaration for DebugBreak without including Windows.h?に関連しています。デバッガなしでDebugBreak()を使用すると、クラッシュを回避するためにIsDebuggerPresent()を使用して評価したいと考えています。Windows.hを含まないIsDebuggerPresentの宣言を取得するには?

問題は、<windows.h>を含める必要があります。さらに、WIN32_LEAN_AND_MEANが定義されていても、多くの余計なことがあります。 minmaxのような余分な旋盤は、C++のコンパイルを壊します。実際、変更をテストすることは私たちを壊しました。

extern BOOL IsDebuggerPresent()で簡単にしようとすると、多くのプリプロセッサマジックが必要となり、関数のすべてのバージョンでシグネチャが一致しません。私たちが15年のOSとコンパイラのために「物事はちょうどうまくいく」ことを確かめたいと考えると、それは厄介な問題のようです。

<windows.h>を含まないIsDebuggerPresent()を使用することはできますか?もしそうなら、私たちはどうやってそれをするのですか?

+2

正確宣言です 'にextern "C" __declspec(dllimportの)int型__stdcall IsDebuggerPresent();'と 'と関数のすべてのバージョンのシグネチャと一致しません。 - 'IsDebuggerPresent'は1つのシグネチャしか持たず、それは不変です。 – RbMm

+0

ありがとう@RbMm、私は今それをテストしています... *" ...シグネチャは1つしかなく、 * - マイクロソフトは、プラットフォームに応じてヘッダーの違いを宣言しています。 – jww

+0

もう一度@RbMmに感謝します。この変更は、VS2012/x86およびVS2015/x86でローカルで良好にテストされました。しかし、VS2015の下に[Appveyor hang](https://ci.appveyor.com/project/noloader/cryptopp/build/1.0.49/job/5kypk5xm0gm2bqlv)があります。私はそれを調べるのにもっと時間が必要です。 – jww

答えて

0

以下のコードは、windows.h自体を含めずに最小限の機能を果たすウィンドウ用のランタイム追加機能です。私はこれが直接質問に答えることはできませんが、あなたのコードを動作させるために具体的に定義する必要があるのは、アーキテクチャ定義(プリプロセッサのもの)と<errhandlingapi.h><windef.h>です。私は以下の私の例を参照してくださいほかに何か完全にはよく分からない:

マイコード:

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_IX86) 
#define _X86_ 
#if !defined(_CHPE_X86_ARM64_) && defined(_M_HYBRID) 
#define _CHPE_X86_ARM64_ 
#endif 
#endif 

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_AMD64) 
#define _AMD64_ 
#endif 

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_ARM) 
#define _ARM_ 
#endif 

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_ARM64) 
#define _ARM64_ 
#endif 

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_M68K) 
#define _68K_ 
#endif 

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_IA64_) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_MPPC) 
#define _MPPC_ 
#endif 

#if !defined(_68K_) && !defined(_MPPC_) && !defined(_X86_) && !defined(_M_IX86) && !defined(_AMD64_) && !defined(_ARM_) && !defined(_ARM64_) && defined(_M_IA64) 
#if !defined(_IA64_) 
#define _IA64_ 
#endif /* !_IA64_ */ 
#endif 

#ifndef _MAC 
#if defined(_68K_) || defined(_MPPC_) 
#define _MAC 
#endif 
#endif 
#include <string> 
#include <windef.h> 
#include <WinUser.h> 
#include <WinNls.h> 
#include <errhandlingapi.h> 
#include <processthreadsapi.h> 
#include <WinBase.h> 

__pragma (comment(lib, "User32")); 

namespace Microsoft { 
void __stdcall rt_assert(const bool test, const wchar_t* FunctionName) 
{ 
    if (test) return; 

    // Retrieve the system error message for the last-error code 
    unsigned __int32 ERROR_ID = GetLastError(); 
    void* MsgBuffer; 
    LCID lcid; //language id 
    GetLocaleInfoEx(L"en-US", LOCALE_RETURN_NUMBER | LOCALE_ILANGUAGE, (wchar_t*)&lcid, sizeof(lcid)); 

    //get error message and attach it to Msgbuffer 
    FormatMessageW(
     FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
     NULL, ERROR_ID, lcid, (wchar_t*)&MsgBuffer, 0, NULL); 

    //concatonate string to DisplayBuffer 
    const std::wstring DisplayBuffer = (static_cast<std::wstring>(FunctionName) + L" failed with error " + std::to_wstring(ERROR_ID) + L": " + static_cast<const wchar_t*>(MsgBuffer)).c_str(); 

    // Display the error message and exit the process 
    if (IsDebuggerPresent()) 
    { 
     OutputDebugStringW(DisplayBuffer.c_str()); 
    } 
    else 
    { 
     MessageBoxExW(NULL, DisplayBuffer.c_str(), L"Error", MB_ICONERROR | MB_OK, static_cast<unsigned __int16>(lcid)); 
    } 
    ExitProcess(ERROR_ID); 
} 
} 
関連する問題