2010-07-01 94 views
0

我已經將我的DLL注入了目標應用程序,並且我也吸引了幾個WINAPI函數 。其中之一是DrawTextExW。我試圖將所有'l'字母替換爲'!'在 之前將其打印出來。我的解決方案可以正常工作幾秒鐘,但目標應用程序崩潰。我真的不明白爲什麼。注入/掛鉤目標應用程序崩潰

這裏的功能:

編輯 - 工作的解決方案:

int WINAPI DetouredDrawTextExW(__in HDC hdc, 
           __inout LPWSTR lpchText, 
           __in int cchText, 
           __inout LPRECT lprc, 
           __in UINT dwDTFormat, 
           __in LPDRAWTEXTPARAMS lpDTParams) 
{ 
    std::wstring s_wc(lpchText, cchText); 

    std::replace(s_wc.begin(), s_wc.end(), L'l', L'!'); 

    return ::DrawTextExW(hdc, const_cast<wchar_t *>(s_wc.c_str()), 
     s_wc.length(), lprc, dwDTFormat, lpDTParams); 
}

因此,有人可以指出來給我什麼,我做錯了嗎?

回答

1

我看到你忽略cchText,你是否會收到一個非NULL結尾的字符串,其值爲cchText,結果是讀取字符串末尾到無效內存?不過,這個錯誤會在構造函數s_wc中作爲Win32異常出現。

此外,您不在dwDTFormat參數中檢查DT_MODIFYSTRING。如果該標誌存在,則:: DrawTextExW()可能會覆蓋無效內存。這將在:: DrawTextExW()中顯示爲Win32異常,或者在s_wc析構函數中顯示爲C++異常。

編輯

這裏的未編譯未經測試代碼,我相信服從的

int WINAPI DetouredDrawTextExW(__in HDC hdc, 
           __inout LPWSTR lpchText, 
           __in int cchText, 
           __inout LPRECT lprc, 
           __in UINT dwDTFormat, 
           __in LPDRAWTEXTPARAMS lpDTParams) 
{ 
    std::vector<wchar_t> v_wc; 
    int strSize = cchText == -1 ? wcslen(lpchText) : cchText; 
    v_wc.resize(strSize + 4); 
    std::copy(lpchText, lpchText + strSize, &v_wc.front()); 
    std::replace(v_wc.begin(), v_wc.end() - 4, L'l', L'!'); 

    int result = ::DrawTextExW(hdc, &v_wc.front(), 
     strSize, lprc, dwDTFormat, lpDTParams); 
    if (dwDTFormat & DT_MODIFYSTRING) 
    { 
     std::copy(&v_wc.front(), &v_wc.front() + v_wc.size(), lpchText); 
    } 
} 
+0

謝謝回覆了合同,但你從一開始是大勢所趨。我忽略了cchText。根據規格,字符串不必以null結尾。因此,std :: wstring(lpchText,cchText)做了訣竅。 – nhaa123 2010-07-02 00:47:37