これは私のWinMainです:
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
if(SUCCEEDED(CoInitialize(NULL)))
{
{
Game game;
game.CreateRessources(hInst);
game.ShowMainScreen();
HWND hwnd=game.Getm_hWnd();
HWND* pHWND=&hwnd;
game.pWinsock->Initialize(hwnd);
ShowWindow(game.Getm_hWnd(),SW_MAXIMIZE);
UpdateWindow(game.Getm_hWnd());
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
}
return 0;
}
私のウィンドウ作成クラスがあります:
void Game::CreateWindowClass(HINSTANCE hInst)
{
// Create a window class
WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=NULL;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=NULL;
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=WinProc;
wClass.lpszClassName="Window Class";
wClass.lpszMenuName=NULL;
wClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&wClass))
{
int nResult=GetLastError();
MessageBox(NULL,"Failed to register window class","Window Class Failed",MB_ICONERROR);
}
m_hWnd=CreateWindowEx(NULL,
"Window Class",
"Game",
WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
this);
if(!m_hWnd)
{
int nResult=GetLastError();
MessageBox(NULL,"Window class creation failed","Window Class Failed",MB_ICONERROR);
}
}
これはGame::CreateRessourcesです:
void Game::CreateRessources(HINSTANCE hInst)
{
CreateWindowClass(hInst);
pD2DResources=CreateD2DResources(m_hWnd);
pMessageLog=CreateMessageLog();
pWinsock=CreateWinsock();
}
そして最後に、私の WinProc は次の行の少し後まで: if(pGame==NULL) MessageBox(NULL,"Pointer pGame is NULL","Error",NULL);
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(msg==WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
Game* pGame = (Game*)pcs->lpCreateParams;
::SetWindowLongPtrW(
hWnd,
GWLP_USERDATA,
PtrToUlong(pGame)
);
}
else if(msg==WM_DESTROY)
{
PostQuitMessage(0);
return 0;
}
else
{
Game* pGame = reinterpret_cast<Game*>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hWnd,
GWLP_USERDATA
)));
if(pGame==NULL)
MessageBox(NULL,"Pointer pGame is NULL","Error",NULL);
if(pGame->pD2DResources!=NULL)
{
switch(msg)
{
何らかの理由で、MessageBox がポップアップするため、pGame は NULL です。なんでそうなの?
編集:ちなみに、私はいくつかのオブジェクトを動的に作成しており、ゲームオブジェクト内のプライベートポインターはこれらの動的に作成されたオブジェクトに割り当てられています。それは問題と関係がありますか?