本文介绍了的LoadImage()返回NULL和GetLastError()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找各地在不同的论坛净回答,但似乎敌不过我的情况...

我的工作在Windows 7中,VS2010。

我有一个使用计时器来调用任务栏提神功能的应用程序。在这一任务栏功能在于)调用的LoadImage(从资源文件和最终的taskber(带shell_notifyicon)得到一个图标图像。运行应用程序时,这似乎在几个小时之内做工精细,但所有的突然的LoadImage()的开始出现问题(这是永远不变的.ico文件它会尝试加载),并返回NULL。我后直接插入电话GetLastError函数,但它始终返回0(表示成功)。
图像本身还是不错的,有效的,而且我也没有解释的方式。

任何线索?
任何帮助是非常非常AP preciated!

下面是一个code片断:

 如果(ghInst&安培;&安培; HWND)
{
    DWORD犯错;
//更新托盘图标这里
    SMALL_ICON =的LoadImage(ghInst,MAKEINTRESOURCE(IconId),IMAGE_ICON,
        GetSystemMetrics的(SM_CXSMICON),GetSystemMetrics的(SM_CYSMICON),0);
    ERR = GetLastError函数();
    如果(!SMALL_ICON)
    {
        LPVOID lpMsgBuf;
        // DWORD ERR = GetLastError函数();
        的FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                      FORMAT_MESSAGE_FROM_SYSTEM |
                      FORMAT_MESSAGE_IGNORE_INSERTS,
                      空值,
                      呃,
                      MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT)
                      (LPTSTR)及lpMsgBuf,
                      0,NULL);        输出(的LoadImage失败的错误消息:%d个%S \\ n,呃,lpMsgBuf);
    }    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage = UWM_SYSTRAY;
    nid.hIcon = SMALL_ICON; / * 16x16的图标* /    如果(bIconExist)
        Shell_NotifyIcon(NIM_MODIFY,&安培; NID);
    其他
        Shell_NotifyIcon(NIM_ADD,&安培; NID);
}


解决方案

我认为这个问题几乎可以肯定的是,你正在泄漏GDI对象,并运行了GDI对象句柄。标准的Windows任务管理器可以告诉你GDI对象计数的过程。

您不是叫的LoadImage LR_SHARED ,所以你的必须的释放图标与 DestroyIcon 之后。请参阅的LoadImage 文档中的备注部分:

(顺便说一句,你也应该释放由的FormatMessage 分配的字符串)。

I've been searching around the net in different forums for an answer, but there seems to be no match to my case...

I am working on Windows 7, VS2010.

I have an application that uses a timer to call a taskbar refreshing function. Within that taskbar function lies a call to LoadImage() that gets an icon image from the resource files and eventually to the taskber (with shell_notifyicon). When running the application this seems to work fine for the first couple of hours, but then all of the sudden the LoadImage() starts failing (it is always the same .ico file it tries to load) and returns NULL. I've inserted a GetLastError call straight after but it always returns 0 (indicating success).The image itself is still good and valid, and I have no way of explaining that.

Any clue?Any help is much much appreciated!

Here's a code snippet:

if (ghInst && hwnd)
{
    DWORD err;
// Update Tray Icon Here
    small_icon=LoadImage(ghInst, MAKEINTRESOURCE(IconId), IMAGE_ICON,
        GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), 0);
    err = GetLastError();
    if (!small_icon)
    {
        LPVOID lpMsgBuf;
        //DWORD err = GetLastError();
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                      FORMAT_MESSAGE_FROM_SYSTEM |
                      FORMAT_MESSAGE_IGNORE_INSERTS,
                      NULL,
                      err,
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                      (LPTSTR) &lpMsgBuf,
                      0, NULL );

        printf("LoadImage FAILED error message: %d %s\n" ,err ,lpMsgBuf);
    }

    nid.uFlags=NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage=UWM_SYSTRAY;
    nid.hIcon=small_icon;   /* 16x16 icon */

    if (bIconExist)
        Shell_NotifyIcon(NIM_MODIFY, &nid);
    else
        Shell_NotifyIcon(NIM_ADD, &nid);
}
解决方案

I think the problem almost certainly is that you are leaking GDI objects and are running out of GDI object handles. The standard Windows Task Manager can show you the GDI object count for your process.

You aren't calling LoadImage with LR_SHARED, so you must free the icon with DestroyIcon afterward. See the "Remarks" section for the LoadImage documentation:

http://msdn.microsoft.com/en-us/library/ms648045%28v=VS.85%29.aspx

(Incidentally, you also should free the string allocated by FormatMessage.)

这篇关于的LoadImage()返回NULL和GetLastError()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 16:06