Skip to content

Commit

Permalink
Added SetErrorMode(SEM_FAILCRITICALERRORS) on program startup.
Browse files Browse the repository at this point in the history
Removed SetErrorMode() call from Drive and DriveGet commands.
  • Loading branch information
Lexikos committed May 1, 2015
1 parent be7ff2d commit 8b66870
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
9 changes: 9 additions & 0 deletions source/AutoHotkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ int WINAPI _tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmd
g_hInstance = hInstance;
InitializeCriticalSection(&g_CriticalRegExCache); // v1.0.45.04: Must be done early so that it's unconditional, so that DeleteCriticalSection() in the script destructor can also be unconditional (deleting when never initialized can crash, at least on Win 9x).

// v1.1.22+: This is done unconditionally, on startup, so that any attempts to read a drive
// that has no media (and possibly other errors) won't cause the system to display an error
// dialog that the script can't suppress. This is known to affect floppy drives and some
// but not all CD/DVD drives. MSDN says: "Best practice is that all applications call the
// process-wide SetErrorMode function with a parameter of SEM_FAILCRITICALERRORS at startup."
// Note that in previous versions, this was done by the Drive/DriveGet commands and not
// reverted afterward, so it affected all subsequent commands.
SetErrorMode(SEM_FAILCRITICALERRORS);

if (!GetCurrentDirectory(_countof(g_WorkingDir), g_WorkingDir)) // Needed for the FileSelectFile() workaround.
*g_WorkingDir = '\0';
// Unlike the below, the above must not be Malloc'd because the contents can later change to something
Expand Down
9 changes: 0 additions & 9 deletions source/script2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8086,8 +8086,6 @@ ResultType Line::DriveSpace(LPTSTR aPath, bool aGetFreeSpace)
buf[length] = '\0';
}

SetErrorMode(SEM_FAILCRITICALERRORS); // If target drive is a floppy, this avoids a dialog prompting to insert a disk.

// The program won't launch at all on Win95a (original Win95) unless the function address is resolved
// at runtime:
typedef BOOL (WINAPI *GetDiskFreeSpaceExType)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
Expand Down Expand Up @@ -8191,7 +8189,6 @@ ResultType Line::Drive(LPTSTR aCmd, LPTSTR aValue, LPTSTR aValue2) // aValue not

case DRIVE_CMD_LABEL: // Note that it is possible and allowed for the new label to be blank.
DRIVE_SET_PATH
SetErrorMode(SEM_FAILCRITICALERRORS); // So that a floppy drive doesn't prompt for a disk
return SetErrorLevelOrThrowBool(!SetVolumeLabel(path, aValue2));

} // switch()
Expand Down Expand Up @@ -8302,7 +8299,6 @@ ResultType Line::DriveGet(LPTSTR aCmd, LPTSTR aValue)
if (drive_get_cmd == DRIVEGET_CMD_SETLABEL) // The is retained for backward compatibility even though the Drive cmd is normally used.
{
DRIVE_SET_PATH
SetErrorMode(SEM_FAILCRITICALERRORS); // If drive is a floppy, prevents pop-up dialog prompting to insert disk.
LPTSTR new_label = omit_leading_whitespace(aCmd + 9); // Example: SetLabel:MyLabel
return g_ErrorLevel->Assign(SetVolumeLabel(path, new_label) ? ERRORLEVEL_NONE : ERRORLEVEL_ERROR);
}
Expand Down Expand Up @@ -8339,8 +8335,6 @@ ResultType Line::DriveGet(LPTSTR aCmd, LPTSTR aValue)
UCHAR letter;
TCHAR buf[128], *buf_ptr;

SetErrorMode(SEM_FAILCRITICALERRORS); // If drive is a floppy, prevents pop-up dialog prompting to insert disk.

for (found_drives_count = 0, letter = 'A'; letter <= 'Z'; ++letter)
{
buf_ptr = buf;
Expand All @@ -8366,7 +8360,6 @@ ResultType Line::DriveGet(LPTSTR aCmd, LPTSTR aValue)
TCHAR volume_name[256];
TCHAR file_system[256];
DRIVE_SET_PATH
SetErrorMode(SEM_FAILCRITICALERRORS); // If drive is a floppy, prevents pop-up dialog prompting to insert disk.
DWORD serial_number, max_component_length, file_system_flags;
if (!GetVolumeInformation(path, volume_name, _countof(volume_name) - 1, &serial_number, &max_component_length
, &file_system_flags, file_system, _countof(file_system) - 1))
Expand All @@ -8383,7 +8376,6 @@ ResultType Line::DriveGet(LPTSTR aCmd, LPTSTR aValue)
case DRIVEGET_CMD_TYPE:
{
DRIVE_SET_PATH
SetErrorMode(SEM_FAILCRITICALERRORS); // If drive is a floppy, prevents pop-up dialog prompting to insert disk.
switch (GetDriveType(path))
{
case DRIVE_UNKNOWN: output_var.Assign(_T("Unknown")); break;
Expand All @@ -8401,7 +8393,6 @@ ResultType Line::DriveGet(LPTSTR aCmd, LPTSTR aValue)
case DRIVEGET_CMD_STATUS:
{
DRIVE_SET_PATH
SetErrorMode(SEM_FAILCRITICALERRORS); // If drive is a floppy, prevents pop-up dialog prompting to insert disk.
DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
switch (GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)
? ERROR_SUCCESS : GetLastError())
Expand Down

0 comments on commit 8b66870

Please sign in to comment.