VC++ 6.0 windows programming problem

I tried a example of my visual c++ 6.0 book.
The code is shown below:
--------------------------------------------
#include

long CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASS WindowClass;
static char szAppName[]= "OFWin";
HWND hWnd;
MSG msg;

WindowClass.style = CS_HREDRAW|CS_VREDRAW;
WindowClass.lpfnWndProc = WindowProc;

WindowClass.cbClsExtra =0;
WindowClass.cbWndExtra=0;
WindowClass.hInstance = hInstance;

WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);

WindowClass.hCursor= LoadCursor(0, IDC_ARROW);

WindowClass.hbrBackground =static_cast(GetStockObject(GRAY_BRUSH));
WindowClass.lpszMenuName =0;
WindowClass.lpszClassName =szAppName;

RegisterClass(&WindowClass);

hWnd=CreateWindow(
szAppName,
"A Basic Window the Hard Way",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
hInstance,
0
);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

while(GetMessage(&msg,0,0,0)== TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

long CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT PaintSt;
RECT aRect;

switch(message)
{
case WM_PAINT:
hDC= BeginPaint(hWnd, &PaintSt);
GetClientRect(hWnd,&aRect);
SetBkMode(hDC, TRANSPARENT);

DrawText(
hDC,
"But, soft! What light through yonder window breaks?",
-1,
&aRect,
DT_SINGLELINE|
DT_CENTER|
DT_VCENTER);
EndPaint(hWnd, &PaintSt);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;

default:
return DefWindowProc(hWnd, message,wParam, lParam);
}
}

--------------------------------------------------------
After I compiled the code and tried to run the .exe file, I got the error message:
LINK : fatal error LNK1168: cannot open Debug/OFWin.exe for writing
Error executing link.exe.

OFWin.exe - 1 error(s), 0 warning(s)

Not sure what's wrong with it. Can anybody help me? Thanks.

Comments

  • sounds like the program is already running. at least thats when i regularly get this error message.


    : I tried a example of my visual c++ 6.0 book.
    : The code is shown below:
    : --------------------------------------------
    : #include
    :
    : long CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    : {
    : WNDCLASS WindowClass;
    : static char szAppName[]= "OFWin";
    : HWND hWnd;
    : MSG msg;
    :
    : WindowClass.style = CS_HREDRAW|CS_VREDRAW;
    : WindowClass.lpfnWndProc = WindowProc;
    :
    : WindowClass.cbClsExtra =0;
    : WindowClass.cbWndExtra=0;
    : WindowClass.hInstance = hInstance;
    :
    : WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
    :
    : WindowClass.hCursor= LoadCursor(0, IDC_ARROW);
    :
    : WindowClass.hbrBackground =static_cast(GetStockObject(GRAY_BRUSH));
    : WindowClass.lpszMenuName =0;
    : WindowClass.lpszClassName =szAppName;
    :
    : RegisterClass(&WindowClass);
    :
    : hWnd=CreateWindow(
    : szAppName,
    : "A Basic Window the Hard Way",
    : WS_OVERLAPPEDWINDOW,
    : CW_USEDEFAULT,
    : CW_USEDEFAULT,
    : CW_USEDEFAULT,
    : CW_USEDEFAULT,
    : 0,
    : 0,
    : hInstance,
    : 0
    : );
    : ShowWindow(hWnd,nCmdShow);
    : UpdateWindow(hWnd);
    :
    : while(GetMessage(&msg,0,0,0)== TRUE)
    : {
    : TranslateMessage(&msg);
    : DispatchMessage(&msg);
    : }
    : return msg.wParam;
    : }
    :
    : long CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    : {
    : HDC hDC;
    : PAINTSTRUCT PaintSt;
    : RECT aRect;
    :
    : switch(message)
    : {
    : case WM_PAINT:
    : hDC= BeginPaint(hWnd, &PaintSt);
    : GetClientRect(hWnd,&aRect);
    : SetBkMode(hDC, TRANSPARENT);
    :
    : DrawText(
    : hDC,
    : "But, soft! What light through yonder window breaks?",
    : -1,
    : &aRect,
    : DT_SINGLELINE|
    : DT_CENTER|
    : DT_VCENTER);
    : EndPaint(hWnd, &PaintSt);
    : return 0;
    : case WM_DESTROY:
    : PostQuitMessage(0);
    : return 0;
    :
    : default:
    : return DefWindowProc(hWnd, message,wParam, lParam);
    : }
    : }
    :
    : --------------------------------------------------------
    : After I compiled the code and tried to run the .exe file, I got the error message:
    : LINK : fatal error LNK1168: cannot open Debug/OFWin.exe for writing
    : Error executing link.exe.
    :
    : OFWin.exe - 1 error(s), 0 warning(s)
    :
    : Not sure what's wrong with it. Can anybody help me? Thanks.
    :

  • What he said.

    But:

    while(GetMessage(&msg,0,0,0)[b]== TRUE[/b])

    is bad, bad, bad! You should never get in the habbit of checking an imaginary boolean variable (like Win32 BOOL which is really an integer) against the #defines TRUE and FALSE. Morever, MSDN says GetMessage returns a non-zero value, it never says it returns TRUE.

    A better approach is:
    while(GetMessage(&msg, 0, 0, 0))

    Or if you must, since the boolean "false" is almost always 0:
    while(GetMessage(&msg, 0, 0, 0) != 0)

    A few weeks ago I fixed a very nasty bug in our code since one of our programmers had if(something == TRUE), this particular code was called from VB.NET and something passed as a .NET bool which is -1. Needless to say the above condition never got satisfied not with true not with a false.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories