Switch - 为什么使用break会给我一个访问违规写入位置?

时间:2015-10-12 17:02:33

标签: c++ switch-statement access-violation

好的,所以这个代码在一个短暂的问题上相当长,所以我会使用< ---箭头为你指出它,但是在我的开关盒中用尽WM_COMMAND:

我对使用return 0 / break感到困惑。

return 0;似乎让我跳过整个WndProc函数(跳过一些代码)

break;突然断开了switch语句,但仍在继续。

那么这是否意味着我应该在我的switch语句的最后使用return 0;以阻止继续前进?或者我的WM_MAXMININFO有什么问题,因为在我将返回0更改为break;之前我从未遇到过这个问题。我问的原因是因为一旦它遇到WM_MAXMININFO我就会遇到访问冲突。 (这可能是所有这一切的原因)。

// Win32Project2.cpp : Defines the entry point for the application.

#include "stdafx.h"
#include "Win32Project2.h"
#include <Windows.h>
#include <Windowsx.h>
#include <shellapi.h>
#include "ProcessFind.h"
#include <WinUser.h>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#define BUTTON L"button"
#define szDefault L"Awaiting Commands..."

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdShow, int iCmdLine)
{
    wchar_t szAppName[] = L"Game Launcher";
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;

RegisterClass(&wndclass);

hwnd = CreateWindow(szAppName, L"GameLauncher v1.0", WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 375, 100, NULL, NULL, hInstance, 0);

ShowWindow(hwnd, iCmdLine);
UpdateWindow(hwnd);

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{

static HWND hUserBox;
static HWND hWindowButton;
HBITMAP hImage = (HBITMAP)LoadImage(NULL, L"?C:\\Users\\chaos\\Desktop\\Capture.PNG", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT);

 switch (message)
 {

 case WM_CREATE:
     CreateWindowEx(NULL, BUTTON, L"Check for process", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX , 5, 35, 150, 25, hwnd, (HMENU)2, NULL, NULL);

    CreateWindow(BUTTON, L"Minecraft", WS_CHILD | WS_VISIBLE, 250, 5, 100, 25, hwnd, (HMENU)1, NULL, NULL);
    CreateWindow(BUTTON, L"Options", WS_CHILD | WS_VISIBLE | WS_DISABLED, 250, 30, 100, 25, hwnd, (HMENU)3, NULL, NULL);

    hUserBox = CreateWindow(L"static", L"Awaiting Commands...", WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 240, 25, hwnd, (HMENU) 2, NULL, NULL);
    return 0;

case WM_COMMAND:

    switch (LOWORD(wparam))
    {
        case 1:
        {
            wchar_t* szProccessToKill = new wchar_t[20];
            GetWindowText(hUserBox, szProccessToKill, 20);


            // Checking checked status on checkbox. (Say that 5 times fast lol)

            if (IsDlgButtonChecked(hwnd, 2) == BST_CHECKED)
            {
                ProcessFind testFunction;
                SetWindowText(hUserBox, L"Ending process");
                Sleep(1000);
                testFunction.EndProcess(L"java.exe");
                SetWindowText(hUserBox, L"Launching Minecraft...");
                Sleep(1000);
                ShellExecute(NULL, L"open", L"MinecraftLauncher", NULL, L"C:\\Program Files (x86)\\Minecraft", 6);
                SetWindowText(hUserBox, L"Minecraft has launched sucessfully!");
                Sleep(1000);
                SetWindowText(hUserBox, szDefault);
            }

            else if (IsDlgButtonChecked(hwnd, 2) == BST_UNCHECKED)
            {

                SetWindowText(hUserBox, L"Launching Minecraft...");
                HINSTANCE ErrorCode = ShellExecute(NULL, L"open", L"C:\\Program Files (x86)\\Minecraft\\MinecraftLauncher", NULL, NULL, 1);

                int ErrorCodeInt = (int)ErrorCode;

                switch (ErrorCodeInt)
                    {
                    case 0:
                        MessageBox(NULL, L"The operating system is out of memory or resources.", L"Error", MB_OK | MB_ICONERROR);
                        break;

                    case ERROR_FILE_NOT_FOUND:
                        MessageBox(NULL, L"The specified file was not found.", L"Error", MB_OK | MB_ICONERROR);
                        break;

                    case ERROR_PATH_NOT_FOUND: 
                        MessageBox(NULL, L"The specified path was not found.", L"Error", MB_OK | MB_ICONERROR);
                        break;

                    default: 
                        SetWindowText(hUserBox, L"Minecraft has launched successfully");
                        Sleep(1500);
                        break;
                    }
                SetWindowText(hUserBox, szDefault);
            }
            delete[] szProccessToKill;
            break; <--- **If this is not a return 0;**
        }

        case 3:
        {
            break;
        }

    default:
        break;
    }

    //Set Min / Max Width/Height size.
case WM_GETMINMAXINFO: <-- It will jump down here and give me an access violation error:
{
    MINMAXINFO* MMI = (MINMAXINFO*)lparam; 
    MMI->ptMaxTrackSize.x = 375;
    MMI->ptMaxTrackSize.y = 100;
    MMI->ptMinTrackSize.x = 375;
    MMI->ptMinTrackSize.y = 100;
    return 0;
}

case WM_DESTROY:
    PostQuitMessage(0);
    return 0;

default:
    return DefWindowProc(hwnd, message, wparam, lparam);
}

return 0;

}

我会直截了当地解决问题:

    SetWindowText(hUserBox, szDefault);
            }
            delete[] szProccessToKill;
            break; <--- **If this is not a return 0;**
        }

        case 3:
        {
            break;
        }

    default:
        break;
    }

    //Set Min / Max Width/Height size.
case WM_GETMINMAXINFO: <-- It will jump down here and give me an access violation error:
{
    MINMAXINFO* MMI = (MINMAXINFO*)lparam; 
    MMI->ptMaxTrackSize.x = 375;
    MMI->ptMaxTrackSize.y = 100;
    MMI->ptMinTrackSize.x = 375;
    MMI->ptMinTrackSize.y = 100;
    return 0;
}

0 个答案:

没有答案