GetOpenFileName() fails under Windows 95/98/NT/ME due to incorrect OPENFILENAME structure size

概要: GetOpenFileName() fails under Windows 95/98/NT/ME due to incorrect OPENFILENAME structure size

GetOpenFileName() fails under Windows 95/98/NT/ME due to incorrect OPENFILENAME structure size

  • Product Name:��C++Builder�
  • Product Version:��6�
  • Product Component:��Platform SDK�
  • Platform/OS Version:� Windows 95, Windows 98, Windows NT, Windows ME

    Description:

    ���� I am using C++Builder 6, and GetOpenFileName() is failing under Windows 95/98/NT/ME. When I call CommDlgExtendedError(), CDERR_STRUCTSIZE is returned. I am initializing OPENFILENAME's lStructSize member using sizeof(OPENFILENAME). What is the problem and how can I get around this issue?

    Answer/Solution:

    ���� This problem originates from a difference in the size of OPENFILENAME between Windows 95/98/NT/ME and Windows 2000/XP. Under Windows 95/98/NT/ME, the size of the OPENFILENAME structure is 76 bytes. However, under Windows 2000/XP, the structure's size is 88 bytes, due to several members that were added later.

    ���� By default, C++Builder 6 uses _WIN32_WINNT=0x0500 and WINVER=0x0500, which results in use of the Windows 2000/XP structure size of 88 bytes. This, of course, is not consistent with the smaller Windows 95/98/NT/ME size of 76 bytes.

    ���� To specify the smaller desired OPENFILENAME size, you need to define _WIN32_WINNT=0x0400 and WINVER=0x0400. One way you can do this is by entering them into Project | Options... | Directories/Conditionals | Conditional Defines.

    ���� Note that this will not have any adverse effect under Windows 2000/XP. They are backwards-compatible and should be able to handle the smaller structure size of 76 bytes. It is only the older Windows 95/98/NT/ME operating systems that have no knowledge of, and therefore do not know how to handle, the larger size of 88 bytes.

    ���� An alternative to the above would be to use the OPENFILENAME_NT4 structure in place of OPENFILENAME. OPENFILENAME_NT4 is provided for backwards-compatibility with older versions of Windows, and it is 76 bytes in size, since it lacks the additional members that were added to OPENFILENAME. If you use this, you will not need to add any conditional defines.

    Author:� Yu-Chen Hsueh