windows program  
Author Message
fighter92





PostPosted: Visual C++ General, windows program Top

I build my windows program and it tells me, that building is finished correctly without errors. When I try to run my program, I get error: test.exe has encountered a problem and needs to close. We are sorry for the inconvenience.


My code is here:

main.cpp:

#define WIN32_LEAN_AND_MEAN
#include "MyWin.h"

#pragma comment(lib,"User32.lib")

WNDPROC wndProc;
HINSTANCE hInstance;

char className [] = "Window Class";
char const caption [] = "Window";
int width=500;
int height=500;

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow) {
ReggWin (wndProc, className, hInst);

MkWin (caption, className, hInst, cmdShow, width, height);

TPump();
}


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

switch (message) {
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam );
}


MyWin.h:

#ifndef MYWIN_H
#define MYWIN_H
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

void ReggWin (WNDPROC wndProc, char const * className, HINSTANCE hInst);
void MkWin (char const * caption, char const * className, HINSTANCE hInstance, int cmdShow, int laius, int korgus);
int TPump (void);

#endif


MyWin.cpp:

#include "MyWin.h"
#include <windows.h>
WNDCLASS _class;
HWND _hwnd;

void ReggAken (WNDPROC wndProc, char const * className, HINSTANCE hInst) {

_class.style = 0;
_class.lpfnWndProc = wndProc;
_class.cbClsExtra = 0;
_class.cbWndExtra = 0;
_class.hInstance = hInst;
_class.hIcon = 0;
_class.hCursor = ::LoadCursor (0, IDC_ARROW);
_class.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
_class.lpszMenuName = 0;
_class.lpszClassName = className;

RegisterClass (&_class);
}


void MkWin (char const * caption, char const * className, HINSTANCE hInstance, int cmdShow, int width, int height) {

_hwnd = CreateWindow (
className,
caption,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
0,
0,
hInstance,
0 );



ShowWindow (_hwnd, cmdShow);
UpdateWindow (_hwnd);
}


int TPump (void) {

MSG msg;
int status;
while ((status = GetMessage (&msg, 0, 0, 0)) != 0) {
if (status == -1)
return -1;
DispatchMessage (&msg);
}

return msg.wParam;
}


Visual C++2  
 
 
einaros





PostPosted: Visual C++ General, windows program Top

That means that the function accepts 4 parameters, but is only naming the first one of them. The unnamed parameters cannot be reached or referenced.

void myFunction(int)
{
// an int is passed to this function,
// but we've got no name for it, and cannot use it
}

void myMainFunction()
{
myFunction(10);
}



 
 
Mike Danes





PostPosted: Visual C++ General, windows program Top

You did not initialize wndProc global variable anywhere so it will always be NULL. You need to set it to WindowProcedure before registering the window class.

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow) {

wndProc = WindowProcedure;
ReggWin (wndProc, className, hInst);


 
 
Simple Samples





PostPosted: Visual C++ General, windows program Top

WinMain is documented; the documentation explains what the four parameters are.