#740 GetLastError doesn't work for GDI calls

Known_bugs
closed-invalid
nobody
2006-04-28
2005-03-16
mjg
No

A sample code snippet:

HBITMAP ddb = CreateDIBitmap (dc, &(bmi->bmiHeader),
CBM_INIT,
&bitmap_memory, bmi, DIB_RGB_COLORS);
if (ddb == NULL)
{
DWORD err = GetLastError ();
dlog (_T ("CreateDIBitmap failed: 0x%x\n"), err);
}

Produces the following output:

CreateDIBitmap failed: 0x0

ie. GetLastError returns 0 ("No Error").

This happens for an annoyingly large number of GDI
calls. Using Microsoft compiler toolset (VC6) this
correctly reports an error from GetLastError.

Full source code available on request...

Any thoughts?

Discussion

  • Danny Smith

    Danny Smith - 2005-03-16

    Logged In: YES
    user_id=11494

    A small testcase would help.
    The problem may be with the very minimalistic
    implementation of WinMainCRTStartup in mingw.

    Danny

     
  • mjg

    mjg - 2005-03-16

    Sample of fault. Compile with gcc -o delme.exe delme.cc -mwindows

     
  • mjg

    mjg - 2005-03-16

    Logged In: YES
    user_id=1240141

    Refer to attached source code (delme.cc) for example. Code
    writes status of operation to window.

     
  • Danny Smith

    Danny Smith - 2005-03-16

    Logged In: YES
    user_id=11494

    I get this:
    CreateDIBitmap returned 0x2 (and failed)

    Accoding to MSDN, GetLastError is only useful after
    CreateDIBitmap for NT.
    Could this be the problem?

    Danny

     
  • Danny Smith

    Danny Smith - 2005-03-16

    Logged In: YES
    user_id=11494

    I get this:
    CreateDIBitmap returned 0x2 (and failed)

    Accoding to MSDN, GetLastError is only useful after
    CreateDIBitmap for NT.
    Could this be the problem?

    Danny

     
  • mjg

    mjg - 2005-03-17

    Logged In: YES
    user_id=1240141

    Taken from MSDN:

    > Return Values
    >
    > If the function succeeds, the return value is a handle to the
    > compatible bitmap.
    >
    > If the function fails, the return value is NULL.
    >
    > Windows NT/2000/XP: To get extended error information, call
    > GetLastError.

    I'm running the test on a Windows XP (SP1) platform, so that
    shouldn't be an issue. All the same, it looks like the bug
    isn't apparent for you... What other platform details do you
    need from me to make a valid comparison?

    gcc -v output:

    > Reading specs from c:/mingw/bin/../lib/gcc/mingw32/3.4.2/specs
    > Configured with: ../gcc/configure --with-gcc --with-gnu-ld
    --with-gnu-as --host=
    > mingw32 --target=mingw32 --prefix=/mingw --enable-threads
    --disable-nls --enable
    > -languages=c,c++,f77,ada,objc,java
    --disable-win32-registry --disable-shared --e
    > nable-sjlj-exceptions --enable-libgcj --disable-java-awt
    --without-x --enable-ja
    > va-gc=boehm --disable-libgcj-debug --enable-interpreter
    --enable-hash-synchroniz
    > ation --enable-libstdcxx-debug
    > Thread model: win32
    > gcc version 3.4.2 (mingw-special)

     
  • Luke Dunstan

    Luke Dunstan - 2005-03-17

    Logged In: YES
    user_id=30442

    I get "CreateDIBitmap failed: 0x0" too. However, I get the
    same result with both MinGW and MSVC 6.

    (Windows 2000 SP4)

     
  • mjg

    mjg - 2005-03-22

    Logged In: YES
    user_id=1240141

    ...so maybe not a MingW bug, but a platform issue?

     
  • Hideki A. Ikeda

    Hideki A. Ikeda - 2005-03-31

    Logged In: YES
    user_id=637951

    The problem with GetLastError is that it's based on a
    internally global register/variable (per thread). In MSVC6 and
    7, you can even add @ERR to the watch window to monitor
    the last error value at the break point. (see:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-
    us/vcug98/html/vcrefviewinglasterrorcodeforcurrentthread.asp)

    With that said, if CreateDIBitmap() "may possibly" call
    internally some other function which also affects @ERR
    register, you'll likely get a 0 if that internal function was called
    after @ERR was set which resets to 0. I've seen few cases
    on WinAPI functions which returns 0 for GetLastError() even
    though it has failed... Maybe the effect differs with different
    video drivers, who knows... (but please don't quote me).

     
  • mjg

    mjg - 2005-03-31

    Logged In: YES
    user_id=1240141

    ...so it sounds like this is a platform issue & that i need
    to learn to not rely on GetLastError for anything useful. yay.

    if that's the case, this "bug report" can probably be closed.

     
  • Dimitri Papadopoulos

    • status: open --> closed-invalid
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks