📄 subject_36175.htm
字号:
<p>
序号:36175 发表者:包建荣 发表日期:2003-04-12 19:01:48
<br>主题:请问专家,为什么会出现内存不能读的错误???
<br>内容:我做的一个VC剪切板位图象素读取的程序如下:<BR> DWORD lWidth;<BR> DWORD lHeight,offbit,bicount;<BR> BYTE *lpPtr;<BR> DWORD LineBytes;<BR> if( OpenClipboard()) { <BR> HANDLE hDibLink;<BR> hDibLink=GetClipboardData(CF_DIB);<BR> BITMAPINFO *lpbmi;<BR> lpbmi=(BITMAPINFO *)GlobalLock(hDibLink);<BR> <BR> offbit=lpbmi->bmiHeader.biSize-<BR> lpbmi->bmiHeader.biSizeImage;<BR> lHeight=lpbmi->bmiHeader.biHeight;<BR> lWidth=lpbmi->bmiHeader.biWidth;<BR> bicount=lpbmi->bmiHeader.biBitCount;<BR><BR> CString tempchar="",ultrachar="";<BR> UINT imageidx;<BR> LineBytes=(DWORD)((lWidth*bicount+31)/32*4; <BR> for(kj=0;kj<lHeight;kj++){<BR> lpPtr=(BYTE *)lpbmi+ offbit+kj*LineBytes;<BR> for(ki=0;ki<lWidth;ki++){<BR> <BR> imageidx=(UINT)(*lpPtr);//为256色位图,所以就是1字节表示一位象素的颜色<BR><BR> lpPtr++;<BR> <BR> tempchar.Format("(%d,%d): %d",ki,kj,imageidx);<BR> ultrachar+=tempchar;<BR> <BR> }<BR> tempchar.Format("\r\n");<BR> ultrachar+=tempchar;<BR> }<BR> <BR> GlobalUnlock(hDibLink);<BR> CloseClipboard();<BR><BR> AfxMessageBox(ultrachar);<BR><BR>}<BR><BR>但是,程序运行的结果是死机,及提示 ****内存单元不能读 的错误提示,不知道哪里出错,望各位高手多多指点!!(单独读一个指针的内容是可以读取的,但读这么多就不行了???)
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:上海到深圳 回复日期:2003-04-12 20:10:36
<br>内容:基本没有问题,只是缺少了必要的对剪贴板格式的判断<BR> DWORD lWidth;<BR> DWORD lHeight,offbit,bicount;<BR> BYTE *lpPtr;<BR> DWORD LineBytes;<BR><BR> if (OpenClipboard()) <BR> { <BR> HANDLE hDibLink = 0;<BR> hDibLink = GetClipboardData(CF_DIB);<BR><BR> if (hDibLink == 0)<BR> {<BR> AfxMessageBox("不是位图!");<BR> return;<BR> }<BR> <BR> BITMAPINFO *lpbmi;<BR> lpbmi=(BITMAPINFO *)GlobalLock(hDibLink);<BR> <BR> offbit = lpbmi->bmiHeader.biSize - lpbmi->bmiHeader.biSizeImage;<BR> lHeight = lpbmi->bmiHeader.biHeight;<BR> lWidth = lpbmi->bmiHeader.biWidth;<BR> bicount = lpbmi->bmiHeader.biBitCount;<BR> <BR> CString tempchar = "", ultrachar = "";<BR> UINT imageidx;<BR> LineBytes=(DWORD)((lWidth * bicount + 31) / 32 * 4); <BR> <BR> for(int kj = 0; kj < lHeight; kj++)<BR> {<BR> lpPtr=(BYTE *)lpbmi + offbit + kj * LineBytes;<BR> for(int ki = 0; ki < lWidth; ki++)<BR> { <BR> imageidx = (UINT)(*lpPtr);//为256色位图,所以就是1字节表示一位象素的颜色<BR> <BR> lpPtr++;<BR> <BR> tempchar.Format("(%d,%d): %d", ki, kj, imageidx);<BR> ultrachar += tempchar;<BR> <BR> }<BR> tempchar.Format("\r\n");<BR> ultrachar += tempchar;<BR> }<BR> <BR> GlobalUnlock(hDibLink);<BR> CloseClipboard();<BR> <BR> AfxMessageBox(ultrachar); <BR> }<BR><BR><BR>顺便提一下,你的编程规范很差<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:包建荣 回复日期:2003-04-12 21:30:43
<br>内容:兄弟,多谢回答,但原因是:明确是位图,8位,256色,可以正确读出 lWidth; lHeight,offbit,bicount(=8)等信息,就是循环读取位图时出错,单独读一个指针的位图象素数据没有问题,不知道是不是位图数据的长度或则其他什么有问题???(读的时候出现不能引用***地址的内存,内存不能读类似的错误信息,然后死机)<BR>可能不是指针返回错误非法地址的缘故吧!<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -