建立一個main.cpp。然後把下面代碼粘貼上!// CEGUIDbg.cpp : Defines the exported functions for the DLL application.//#include "stdafx.h"#include <Windows.h>#include "tchar.h"#include <string>#include <sstream>#include <vector>#include "ceguistring.h"#define ADDIN_API __declspec(dllexport)typedef struct tagDEBUGHELPER{ DWORD dwVersion; HRESULT (WINAPI *ReadDebuggeeMemory)( struct tagDEBUGHELPER *pThis, //DEBUGHELPER pointer DWORD dwAddr,//the address of object you want to show formatted prompt information DWORD nWant, //the object size in byte. VOID* pWhere, //the dest buffer for storing the object DWORD *nGot );//number bytes are transferred. // from here only when dwVersion >= 0x20000 DWORDLONG (WINAPI *GetRealAddress)( struct tagDEBUGHELPER *pThis ); //use for 64-bit system. HRESULT (WINAPI *ReadDebuggeeMemoryEx)( struct tagDEBUGHELPER *pThis, DWORDLONG qwAddr, DWORD nWant, VOID* pWhere, DWORD *nGot ); int (WINAPI *GetProcessorType)( struct tagDEBUGHELPER *pThis );} DEBUGHELPER;// 多位元組編碼轉為UTF8編碼 bool MultiByteToUtf8( char* pszDestUtf8, int iDestUtf8Size, const char* pszMultiByte, int iMultiByteSize = -1 ) { if( NULL == pszDestUtf8 || NULL == pszMultiByte ) { return false; } // convert an MBCS string to widechar int iWideCharSize = MultiByteToWideChar( CP_ACP, 0, pszMultiByte, iMultiByteSize, NULL, 0 ); std::vector< WCHAR > vctWideChar( iWideCharSize ); int iNumWritten = MultiByteToWideChar( CP_ACP, 0, pszMultiByte, iMultiByteSize, &vctWideChar.front(), iWideCharSize ); if( iNumWritten != iWideCharSize ) { return false; } // convert an widechar string to utf8 int iUtf8Size = WideCharToMultiByte(CP_UTF8, 0, &vctWideChar.front(), -1, NULL, 0, NULL, NULL); if ( iUtf8Size <= 0) { return false; } if( iUtf8Size > iDestUtf8Size ) { iUtf8Size = iDestUtf8Size; } iNumWritten = WideCharToMultiByte( CP_UTF8, 0, &vctWideChar.front(), -1, pszDestUtf8, iUtf8Size, NULL, NULL ); if ( iNumWritten != iUtf8Size ) { return false; } return true; } ADDIN_API HRESULT WINAPI CEGUIDbg_String(DWORD dwAddress, DEBUGHELPER *pHelper, int nBase, BOOL bUniStrings, char *pResult, size_t max, DWORD reserved ){ CEGUI::String strDebug; DWORD nGot; //get CEGUI::String data member. if (pHelper->ReadDebuggeeMemory(pHelper,dwAddress,sizeof( strDebug),&strDebug,&nGot) != S_OK) { return E_FAIL; } if( nGot != sizeof( strDebug ) ) { return E_FAIL; } const CEGUI::utf32* pszUtf32 = strDebug.ptr(); int iLength = strDebug.length(); std::vector< CEGUI::utf32 > vctBuffer; //if the string data is stored in a memory allocated by new(), we have to copy the data to out memory block. if( iLength > STR_QUICKBUFF_SIZE ) { vctBuffer.resize( iLength ); if( S_OK != pHelper->ReadDebuggeeMemory( pHelper, ( DWORD )pszUtf32, iLength * sizeof( CEGUI::utf32 ), &vctBuffer.front(), &nGot ) ) { return E_FAIL; } if( nGot != vctBuffer.size() * sizeof( CEGUI::utf32 ) ) { return E_FAIL; } pszUtf32 = &vctBuffer.front(); } //get ascii character. //although the data pointer is utf32*, but the data isn't encoded by utf32 if you pass const char* to CEGUI::String constructor. In contrary, it only store each ascii character //in a utf32-type element. int iSize = iLength + 1; if( iSize > max ) { iSize = max; iLength = iSize - 1; } std::vector< char > vctAscii( iSize ); for( int i = 0; i < iLength; ++i ) { vctAscii[ i ] = ( char )( unsigned char )pszUtf32[ i ]; } vctAscii[ iLength ] = 0; //convert ascii character set to utf8 character set. //Because debugger accepts utf8 character set. //If you pass ascii string to pResult, chinese character can't be shown. if( false == MultiByteToUtf8( pResult, max, &vctAscii.front() ) ) { return E_FAIL; } //set all data to 0, then CEGUI::String::~String won't delete anything should't be deleted. memset( &strDebug, 0, sizeof( strDebug ) ); return S_OK;}可以看到我們需要包含“CEGUIString.h"這樣的標頭檔,我們的做法是直接拷貝CEGUIString.h,CEGUIString.cpp到工程來,因為我們需要CEGUI::String這個類的聲明和實現(因為我們需要對這種類型進行一些解析操作)。
但是CEGUI::String.h包含了CEGUIBase.h。所以我們需要添加CEGUI標頭檔的搜尋目錄。做法是【項目屬性】-》【C/C++】-》【General】-》【Addtional include direstories】,向其中添加CEGUI SDK中的CEGUI/Include檔案路徑。
同時為了能夠靜態編譯CEGUIString.h,CEGUIString.cpp,我們在【C/C++】-》【Preprocessor】中添加CEGUI_STATIC宏,這表明使用靜態庫形式編譯CEGUI。