On Friday, 20 August 2021 at 05:22:20 UTC, nov wrote:
On Friday, 20 August 2021 at 04:27:34 UTC, Jesse Phillips wrote:
For me, this code generates the Message Box. Does this happen for you?

no errors
https://run.dlang.io/is/4tlm3p
```D
void main() {
    try {
       import std.stdio: File;
       File file = File(__FILE__,"r");
       file.close();
   }
   catch(Throwable e) {
       import std.stdio: writefln;
       writefln("err=%s", e.msg);
   }
}
```

This is not a console App. It's a Win32 one. Also, it turns out to be a bug. If I compile my application(with a couple of 'imports' removed under dmd 2.060), it works just fine, with no Access Violation Error. Try the following on dmd 2.060:
```d
import core.runtime;

pragma(lib, "gdi32.lib");

import core.sys.windows.windows;
import std.stdio;

extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    int result;

    try
    {
        Runtime.initialize();
result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
        Runtime.terminate();
    }
    catch(Throwable o)
    {
        MessageBoxA(null, o.msg, "Error", MB_OK | MB_ICONERROR);
        result = 1;
    }

    return result;
}

int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    HWND hwnd;
    MSG  msg;
    WNDCLASSA wndclass;

    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = &WndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIconA(NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursorA(NULL, IDC_ARROW);
wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = appName.toUTF16z;

    if(!RegisterClassA(&wndclass))
    {

        return 0;
    }

    hwnd = CreateWindowA( "Test",
                         "Test",
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         NULL,
                         NULL,
                         hInstance,
                         NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessageA(&msg, NULL, 0, 0))
    {
        TranslateMessageA(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

extern(Windows)
LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
{
    switch (message)
    {
        case WM_CREATE:
            try
            {
               File file = File("test","r");
               file.close();
            }
            catch(Throwable e)
            {
MessageBoxA(null, "Error", cast(char)[])e.msg,MB_OK | ICON_ERROR);
               PostQuitMessage(1);
            }
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        default:
    }

    return DefWindowProcA(hwnd, message, wParam, lParam);
}
```
Sorry if I misnamed(forgot an 'A' at the end of the function name) something in above code.

Now, edit above code(provided that it has been fixed if necessary) by adding:
```d
import core.sys.windows.wingdi;
```
And remove:
```d
import core.sys.windows.windows;
```
And compile it with dmd 2.080.0 or later.

See the results(what matters is if you get the "Access Violation" error or not, everything else is irrelevant).

Reply via email to