如何解决错误:007A8530的堆块在007A860A修改过去要求的d2大小? [英] How to solve error: Heap block at 007A8530 modified at 007A860A past requested size of d2?

查看:221
本文介绍了如何解决错误:007A8530的堆块在007A860A修改过去要求的d2大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手。我试图将函数这里转换为将html unicode文本设置为剪贴板。这就是我到目前为止所做的:



I am a newbie to C++. I was trying to convert the function here to set html unicode text to clipboard. This is what I have done so far:

bool CopyHTML2(WCHAR *html ){
    wchar_t *buf = new wchar_t [400 + wcslen(html)];
    if(!buf) return false;

	static int cfid = 0;
    if(!cfid) cfid = RegisterClipboardFormat("HTML Format");

	    // Create a template string for the HTML header...
    wcscpy(buf,
        L"Version:0.9\r\n"
        L"StartHTML:00000000\r\n"
        L"EndHTML:00000000\r\n"
        L"StartFragment:00000000\r\n"
        L"EndFragment:00000000\r\n"
        L"<html><body>\r\n"
        L"<!--StartFragment -->\r\n");

    // Append the HTML...
    wcscat (buf, html);
    wcscat (buf, L"\r\n");
    // Finish up the HTML format...
    wcscat (buf,
        L"<!--EndFragment-->\r\n"
        L"</body>\r\n"
        L"</html>");

	wchar_t *ptr = wcsstr(buf, L"StartHTML");
	wsprintfW(ptr+10, L"%08u", wcsstr(buf, L"<html>") - buf);
	*(ptr+10+8) = L'\r';

    ptr = wcsstr(buf, L"EndHTML");
    wsprintfW(ptr+8, L"%08u", wcslen(buf));
    *(ptr+8+8) = '\r';

    ptr = wcsstr(buf, L"StartFragment");
    wsprintfW(ptr+14, L"%08u", wcsstr(buf, L"<!--StartFrag") - buf);
    *(ptr+14+8) = '\r';

    ptr = wcsstr(buf, L"EndFragment");
    wsprintfW(ptr+12, L"%08u", wcsstr(buf, L"<!--EndFrag") - buf);
    *(ptr+12+8) = '\r';

	// Open the clipboard...
    if(OpenClipboard(0)) {
        EmptyClipboard();
        HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, wcslen(buf)+4);
        wchar_t *ptr = (wchar_t *)GlobalLock(hText);
        wcscpy(ptr, buf);
        GlobalUnlock(hText);
        SetClipboardData(cfid, hText);
        CloseClipboard();
        GlobalFree(hText);
    }

    // Clean up...
    //delete [] buf;
	return true;
}





我在SetClipboardData函数中遇到以下错误:



HEAP [Project1.exe]:堆积块00378038修改为0037813E过去请求的大小$



如何解决这个问题?这也是正确的方法吗?或者有更简单的方法吗?



提前感谢您的帮助。



I get the following error at SetClipboardData function:

HEAP[Project1.exe]: Heap block at 00378038 modified at 0037813E past requested size of fe
Project1.exe has triggered a breakpoint.

Any idea on how to solve this? Also is it the right way to do this? Or is there a simpler way to do it?

Thanks in advance for all your help.

推荐答案

我没有看到堆错误的来源,但您也要求正确或更简单的方法。首先,您应该知道HTML剪贴板格式必须包含UTF-8编码的文本。因此输入文本必须转换为UTF-8并以HTML格式标题为前缀。我的一个项目的片段( lpszWide 是HTML文本,包括HTML和片段开头和结尾标记的宽字符串):



I did not see the source for the heap error but you also asked for the right or simpler way. At first you should know that the HTML clipboard format must contain UTF-8 encoded text. So the input text must be converted to UTF-8 and prefixed with the HTML format header. A snippet from one of my projects (lpszWide is the HTML text as wide string including the HTML and fragment start and end markers):

// Get length of UTF-8 string including terminating NULL char.
// The MSDN does not mention if the string should be NULL terminated or not.
// Providing the NULL terminator is safe and always better.
int nUtf8Size = ::WideCharToMultiByte(CP_UTF8, 0, lpszWide, -1, NULL, 0, NULL, NULL);
if (nUtf8Size < 1)
{
    TRACE0("CHtmlFormat::SetHtml: UTF-8 conversion failed\n");
    ASSERT(0);
    return false;
}
// Allocate buffer for HTML format descriptor and UTF-8 content. 
// NOTE: Adjust length definition when changing the descriptor!
const int nDescLen = 105;
hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, nDescLen + nUtf8Size);
if (NULL != hGlobal)
{
    // Convert to UTF-8
    bool bErr = false;
    LPSTR lpszBuf = static_cast<LPSTR>(::GlobalLock(hGlobal));
    LPSTR lpszUtf8 = lpszBuf + nDescLen;
    if (::WideCharToMultiByte(CP_UTF8, 0, lpszWide, -1, lpszUtf8, nUtf8Size, NULL, NULL) <= 0)
    {
        TRACE0("CHtmlFormat::SetHtml: UTF-8 conversion failed\n");
        bErr = true;
    }
    else
    {
        // Get the fragment marker positions
        LPCSTR lpszStartFrag = strstr(lpszUtf8, "<!--StartFragment-->");
        LPCSTR lpszEndFrag = strstr(lpszUtf8, "<!--EndFragment-->");
        ASSERT(lpszStartFrag);
        ASSERT(lpszEndFrag);
        // Adjust pointer to be behind inserted string followed by CR-LF
        lpszStartFrag += strlen("<!--StartFragment-->") + 2;
        // Create descriptor and prepend it to the string.
        // Use only ASCII chars here!
        // Using leading zeroes with the offsets here to avoid iterative calculations.
        // With valid nDescLen, _snprintf() will not append the NULL terminator here.
        VERIFY(nDescLen == _snprintf(
            lpszBuf, nDescLen,
            "Version:1.0\r\nStartHTML:%010d\r\nEndHTML:%010d\r\nStartFragment:%010d\r\nEndFragment:%010d\r\n",
            nDescLen, 
            nDescLen + nUtf8Size - 1,		// offset to next char behind string
            nDescLen + static_cast<int>(lpszStartFrag - lpszUtf8), 
            nDescLen + static_cast<int>(lpszEndFrag - lpszUtf8)));
    }
    ::GlobalUnlock(hGlobal);
    if (bErr)
    {
        ::GlobalFree(hGlobal);
        hGlobal = NULL;
    }
}
return NULL != hGlobal;


这篇关于如何解决错误:007A8530的堆块在007A860A修改过去要求的d2大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆