Seltsames Verhalten



  • Folgender Code funktioniert perfekt:

    #include <windows.h>
    
    class Window
    {
      private:
       HWND hWindow;
      public:
       void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
       void SetLong(int nIndex, LONG dwNewLong);
       void SetPos(HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
       void Destroy();
       void Show(int nCmdShow);
       void Update();
    };
    
    void Window::Create(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
    {
        hWindow = ::CreateWindow(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
        if(!hWindow) throw(GetLastError());
    }
    void Window::SetLong(int nIndex, LONG dwNewLong)
    {
        if(!::SetWindowLong(hWindow, nIndex, dwNewLong)) throw(GetLastError());
    }
    void Window::Destroy()
    {
        ::DestroyWindow(hWindow);
    }
    void Window::SetPos(HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
    {
        if(!::SetWindowPos(hWindow, hWndInsertAfter, X, Y, cx, cy, uFlags)) throw(GetLastError);
    }
    void Window::Show(int nCmdShow)
    {
       ::ShowWindow(hWindow, nCmdShow);
    }
    void Window::Update()
    {
       ::UpdateWindow(hWindow);
    }
    
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       switch (message)
       {
       case WM_DESTROY:
          {
             PostQuitMessage(0);
             return 0;
          }
       }
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        MSG msg;
        WNDCLASS wndclass;
        wndclass.style = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc = WindowProc;
        wndclass.cbClsExtra = 0 ;
        wndclass.cbWndExtra = 0 ;
        wndclass.hInstance = hInstance ;
        wndclass.hIcon = ::LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor = ::LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground = (HBRUSH) ::GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName = NULL ;
        wndclass.lpszClassName = "Die erste WNDCLASS";
        RegisterClass(&wndclass);
    
        Window win;
        win.Create("Die erste WNDCLASS", "Die erste WNDCLASS", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance,  NULL);
        win.Show(SW_SHOWNORMAL);
        win.Update();
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return 0;
    }
    

    Der Code hingegen (sind 3 Files) führt zu folgendem Fehler (fast nur auf meinem PC):
    Runtime Error!
    Progam: [schnipp].exe
    The Application has requested the Runtime to terminate it in an unusual way. Please contect...

    #ifndef WINDOW_HPP
    #define WINDOW_HPP
    #include <windows.h>
    namespace las
    {
    class Window
    {
      private:
        HWND hWindow;
      public:
        void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
        void SetLong(int nIndex, LONG dwNewLong);
        void SetPos(HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
        void Destroy();
        void Update();
        void Show(int nCmdShow);
    };
    }
    #endif
    #include "window.hpp"
    using namespace las;
    
    void Window::Create(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
    {
        hWindow = ::CreateWindow(lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
        if(!hWindow) throw(GetLastError());
    }
    void Window::SetLong(int nIndex, LONG dwNewLong)
    {
        if(!::SetWindowLong(hWindow, nIndex, dwNewLong)) throw(GetLastError());
    }
    void Window::Destroy()
    {
        ::DestroyWindow(hWindow);
    }
    void Window::SetPos(HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
    {
        if(!::SetWindowPos(hWindow, hWndInsertAfter, X, Y, cx, cy, uFlags)) throw(GetLastError);
    }
    void Window::Update()
    {
        if(!::UpdateWindow(hWindow)) throw(GetLastError);
    }
    void Window::Show(int nCmdShow)
    {
        if(!::ShowWindow(hWindow, nCmdShow)) throw(GetLastError);
    }
    #include "window.hpp"
    #include <windows.h>
    using namespace las;
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        MSG msg;
        WNDCLASS wndclass;
        wndclass.style = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc = WindowProc;
        wndclass.cbClsExtra = 0 ;
        wndclass.cbWndExtra = 0 ;
        wndclass.hInstance = hInstance ;
        wndclass.hIcon = ::LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor = ::LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground = (HBRUSH) ::GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName = NULL ;
        wndclass.lpszClassName = "Die erste WNDCLASS"; 
        RegisterClass(&wndclass);
        Window win;
        win.Create("Die erste WNDCLASS", "Die erste WNDCLASS", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance,  NULL);
        win.Show(SW_SHOWNORMAL);
        win.Update();
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       switch (message)
       {
       case WM_DESTROY:
          {
             PostQuitMessage(0);
             return 0;
          }
       }
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    

    Ich hatte mal Visual Studio .NET drauf, deinstalliert...
    Dann Visual C++6 Autorenedition deinstalliert....



  • namespace las
    {
    class Window
    {
      private:
        HWND hWindow;
      public:
        void Create(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam);
        void SetLong(int nIndex, LONG dwNewLong);
        void SetPos(HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
        void Destroy();
        void Update();
        void Show(int nCmdShow);
    };
    } <------- Semikolon fehlt!!
    

    Ob das der Grund für den Fehler ist, glaub ich aber eher net ...



  • Nein es liegt an etwas das selbst kein WinAPI Progger gesehen hat:
    if(!::ShowWindow(hWindow, nCmdShow)) throw(GetLastError());
    und damit:
    Return Values
    If the window was previously visible, the return value is nonzero.
    If the window was previously hidden, the return value is zero.



  • Daher habe ich den "Blödsinn" in meinem Code weg gelassen. 😉


Anmelden zum Antworten