Release运行失败实例看编程习惯

   编程过程中运到一些不知所谓的问题总是令人无比烦恼,感觉无从下手,恨不得把机器砸了,对于程序设计的初学者,会经常遇到这样的问题。
  有一个问题就是这样:Debug版本运行一切如你所愿般正常有序,而Release版本执行确没有预期的效果。

  下面是我编程时遇到的一个问题,在Debug里忙够了,正想发行爽一下,但却出问题了。当时感觉很怪异,但仔细想想这个问题还是有章可循的,只是作为程序开发的初级者,一些不好的编程习惯导致了问题的发生。

看具体问题:
========================================================================================================================
//这段代码在Debug里运行正常,在Release里运行失败

HKEY    Hkey;
char     ValueBuff [MAX_PATH] = { 0 };
unsigned 
long     ValueSize;
DWORD   RegType
= REG_SZ;

if  (ERROR_SUCCESS == RegOpenKeyA(HKEY_LOCAL_MACHINE,Regpath, & Hkey))
{
    
if  (ERROR_SUCCESS == RegQueryValueExA(Hkey, " Path " ,NULL, & RegType,(unsigned  char   * )ValueBuff, & ValueSize))
    {

    }
}

   问题的原因是ValueSize未被初始化的问题,在Debug里被默认设置为0xCCCCCCCC,而在Release里则为0x0。这样,RegQueryValueEx首先检查ValueSize时,未通过存储空间大小验证而运行失败。

=======================================================================================================================
//而这段代码则是正确的

HKEY    Hkey;
char     ValueBuff [MAX_PATH] = { 0 };
unsigned 
long     ValueSize = MAX_PATH;
DWORD   RegType
= REG_SZ;

if  (ERROR_SUCCESS == RegOpenKeyA(HKEY_LOCAL_MACHINE,Regpath, & Hkey))
{
    
if  (ERROR_SUCCESS == RegQueryValueExA(Hkey, " Path " ,NULL, & RegType,(unsigned  char   * )ValueBuff, & ValueSize))
    {

    }
}

========================================================================================================================
  上面这个问题其实在执行程序之前就可以解决,第一个途径是认真查看编译和链接时编译器给出的的提示信息,不要满足于没有错误就可以。在我编译时有下面这个警告,不过我没在意,因为调试没有问题,而这个警告则是问题的关键。

  warning C4700: local variable 'ValueSize' used without having been initialized.


========================================================================================================================
//MSDN
   Windows编程,MSDN是个宝典,所出的问题应该在MSDN里都有解答。但有时候总是没耐心看完MSDN的内容,只是关注参数类型等少量信息,也会导致出现这类问题,而且查看MSDN你还可以获得解决问题的办法。上面这个例子,MSDN对lpcbData有如下详细介绍,而且出错了你还可以知道怎么去查找错误。

lpcbData
  [in, out] Pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. When the function returns, this variable contains the size of the data copied to lpData.
  If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, then lpcbData will also include the size of the terminating null character or characters.

The lpcbData parameter can be NULL only if lpData is NULL.

  If the buffer specified by lpData parameter is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of the lpData buffer are undefined.

  If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This enables an application to determine the best way to allocate a buffer for the value's data.

if  (ERROR_SUCCESS == RegOpenKeyA(HKEY_LOCAL_MACHINE,Regpath, & Hkey))
{
    DWORD  Ret
= RegQueryValueExA(Hkey, " Path " ,NULL, & RegType,(unsigned  char   * )ValueBuff, & ValueSize);
    
if  (ERROR_SUCCESS == Ret)
    {

    }
    
else
        
if (ERROR_MORE_DATA == Ret)     // 这里可以判断存储空间大小,进而发现问题所在
        {

        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值