我剛開始學習C++的Win,現在我在tut的三部分。所以我有這樣的代碼:我需要幫助修復代碼C++ Win32API
#include <Windows.h>
#include <stdio.h>
#define WIN_WIDTH 300
#define WIN_HEIGHT 200
#define class_name L"HDC"
LRESULT CALLBACK WinProc(HWND hwnd , UINT Message ,WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hinstance ,HINSTANCE hPrev,PSTR cmdline ,int ishow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex ={0};
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.cbSize = sizeof(wndclassex);
wndclassex.lpfnWndProc;
wndclassex.hInstance = hinstance;
wndclassex.lpszClassName = class_name;
wndclassex.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
RegisterClassEx(&wndclassex);
hwnd = CreateWindow(class_name,L"My Second WinDow Application",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
WIN_WIDTH,WIN_HEIGHT,NULL,NULL,hinstance,NULL);
if(!hwnd)
return EXIT_FAILURE;
HDC hdc = GetDC(hwnd);
if(!hdc)
return EXIT_FAILURE;
ShowWindow(hwnd,ishow);
UpdateWindow(hwnd);
RECT shen;
GetClientRect(hwnd,&shen);
FillRect(hdc,&shen,(HBRUSH)GetStockObject(WHITE_BRUSH));
while(1)
{
if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
ReleaseDC(hwnd,hdc);
UnregisterClass(class_name,hinstance);
return msg.wParam;
}
LRESULT CALLBACK DefWinProc(HWND hwnd,UINT Message,WPARAM wParam ,LPARAM lParam)
{
switch(Message)
{
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWinProc(hwnd,Message,wParam,lParam);
}
但我不幸運的系統公告稱:
'device context.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'device context.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Symbols loaded (source information stripped).
'device context.exe': Loaded 'C:\Windows\SysWOW64\uxtheme.dll', Symbols loaded (source information stripped).
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
First-chance exception at 0x00000000 in device context.exe: 0xC0000005: Access violation.
The program '[15148] device context.exe: Native' has exited with code 1 (0x1).
請有人知道,錯誤只是幫我解決這個問題,我只是想依靠窗口編程。先謝謝你。
你需要學習如何使用調試器 - 一個非常有用的軟件開發工具。 –
我會在調試器中啓用訪問衝突。然後看看它停在哪條線上。您正在使用空指針。但我不知道在30秒內我讀你的代碼.. – drescherjm
它看起來像程序退出。你也許應該做一些追蹤來找出它正在退出的位置。 –