变量“xyz"周围的堆栈已损坏 [英] Stack around the variable 'xyz' was corrupted

查看:38
本文介绍了变量“xyz"周围的堆栈已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一些我在网站上找到的简单代码,以便在 Windows Vista 64 上的 VC++ 2010 中运行:

I'm trying to get some simple piece of code I found on a website to work in VC++ 2010 on windows vista 64:

#include "stdafx.h"
#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
 DWORD dResult;
 BOOL result;
 char oldWallPaper[MAX_PATH];

 result = SystemParametersInfo(SPI_GETDESKWALLPAPER, sizeof(oldWallPaper)-1, oldWallPaper, 0);

 fprintf(stderr, "Current desktop background is %s\n", oldWallPaper);

 return 0;
}

它确实可以编译,但是当我运行它时,总是出现此错误:

it does compile, but when I run it, I always get this error:

Run-Time Check Failure #2 - Stack around the variable 'oldWallPaper' was corrupted.

我不确定出了什么问题,但我注意到,oldWallPaper 的值看起来像 "C\0:\0\0U\0s\0e\0r\0s[...]" --我想知道所有的 \0 是从哪里来的.

I'm not sure what is going wrong, but I noticed, that the value of oldWallPaper looks something like "C\0:\0\0U\0s\0e\0r\0s[...]" -- I'm wondering where all the \0s come from.

  • 我的一个朋友在 windows xp 32(也是 VC++ 2010)上编译了它,并且能够毫无问题地运行它

任何线索/提示/意见?

any clues/hints/opinions?

谢谢

推荐答案

文档不是很清楚.返回的字符串是 WCHAR,每个字符两个字节而不是一个,因此您需要分配两倍的空间,否则会导致缓冲区溢出.试试:

The doc isn't very clear. The returned string is a WCHAR, two bytes per character not one, so you need to allocate twice as much space otherwise you get a buffer overrun. Try:

BOOL result; 
WCHAR oldWallPaper[(MAX_PATH + 1)]; 

result = SystemParametersInfo(SPI_GETDESKWALLPAPER,
_tcslen(oldWallPaper), oldWallPaper, 0); 

另见:

http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx

http://msdn.microsoft.com/en-us/library/ms235631(VS.80).aspx(字符串转换)

这篇关于变量“xyz"周围的堆栈已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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