• Advertisement
Sign in to follow this  

Program Won't Terminate

This topic is 3875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can't seem to get this program to terminate normally. It can close down, but it still remains a program in my computer's processes. I have to ctrl + alt +delete and go through that whole tidbit in order to shut it down. What's going on here. Here's the code, though i've checked it several times.

#include <windows.h>
#include <d3d9.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
bool InitDirect3D(void);
void RunProgram(void);
void CleanUp(void);

/*  Make the class name into a global variable  */
char szClassName[ ] = "CodeBlocksWindowsApp";
LPDIRECT3D9 pd3d;
LPDIRECT3DDEVICE9 pd3dDevice;
HWND hwnd;               /* This is the handle for our window */

int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)

{

  MSG messages;            /* Here messages to the application are saved */
  WNDCLASSEX wincl;        /* Data structure for the windowclass */

  /* The Window structure */
  wincl.hInstance = hThisInstance;
  wincl.lpszClassName = szClassName;
  wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  wincl.cbSize = sizeof (WNDCLASSEX);

  /* Use default icon and mouse-pointer */
  wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  wincl.lpszMenuName = NULL;                 /* No menu */
  wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  wincl.cbWndExtra = 0;                      /* structure or the window instance */
  /* Use Windows's default color as the background of the window */
  wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND + 2;

  /* Register the window class, and if it fails quit the program */
  if (!RegisterClassEx (&wincl))
    return 0;

  /* The class is registered, let's create the program*/
  hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "DirectX Template",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           800,                 /* The programs width */
           600,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
         );

  /* Make the window visible on the screen */
  ShowWindow (hwnd, nFunsterStil);
  InitDirect3D();

  /* Run the message loop. It will run until GetMessage() returns 0 */
  while (1)
    {
      if(PeekMessage( &messages, NULL,0,0,PM_REMOVE))
        {
          /* Translate virtual-key messages into character messages */
          TranslateMessage(&messages);
          /* Send message to WindowProcedure */
          DispatchMessage(&messages);
        }
      else
        {
          RunProgram();
        }

    }

  /* The program return-value is 0 - The value that PostQuitMessage() gave */
  CleanUp();
  return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

  switch (message)                  /* handle the messages */
    {
      case WM_DESTROY:
      PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
      break;
      default:                      /* for messages that we don't deal with */
      return DefWindowProc (hwnd, message, wParam, lParam);
    }

  return 0;
}

bool InitDirect3D(void)
{
  pd3d = NULL;
  pd3dDevice = NULL;

  pd3d = Direct3DCreate9(D3D_SDK_VERSION);


  D3DPRESENT_PARAMETERS d3dpp;
  ZeroMemory(&d3dpp,sizeof(d3dpp));
  d3dpp.Windowed = false;
  d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  d3dpp.BackBufferCount = 1;
  d3dpp.BackBufferHeight = 600;
  d3dpp.BackBufferWidth = 800;
  d3dpp.hDeviceWindow = hwnd;

  if(FAILED( pd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice)))
    {
      return false;
    }

  return true;
}

void RunProgram(void)
{
  if(pd3dDevice == NULL)
    return;

  pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,255,0),1.0f,0);
  pd3dDevice->Present(NULL,NULL,NULL,NULL);
}

void CleanUp(void)
{
  if(pd3dDevice != NULL)
    pd3dDevice->Release();

  if(pd3d != NULL)
    pd3d->Release();
}


Share this post


Link to post
Share on other sites
Advertisement
You send WM_QUIT, but never test for it in the main loop.

Change your loop to the following, and it should work:


while (1)
{
if(PeekMessage( &messages, NULL,0,0,PM_REMOVE))
{
if (messages.message==WM_QUIT)
break;

/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
else
{
RunProgram();
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement