PDA

View Full Version : C-Dev-C++ mi compila il programma ma non lo esegue.


Malta
10-10-2007, 14:47
Ciao a tutti!!!
Ieri ho installato Dev-C++.
Io voglio imparare il C.
Allora ho seguito la guida di HTML.it che presenta come costruire il semplicissimo programmino Hello World.
Ho creato un progetto per windows.
Mi ha creato delle righe di codice, e dopodiche ho inserito il codice che dovrebbe far eseguire il semplice programma.
Provate a vedere un po' voi:

Tutto:
#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

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

{
HWND hwnd; /* This is the handle for our window */
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;

/* 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 */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* 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);

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

/* The program return-value is 0 - The value that PostQuitMessage() gave */
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;
}
#include <stdio.h>

main ()
{
printf("Hello World!");
}


Righe da lui compilate:
#include <windows.h>

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

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

{
HWND hwnd; /* This is the handle for our window */
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;

/* 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 */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* 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);

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

/* The program return-value is 0 - The value that PostQuitMessage() gave */
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;
}

Righe da me inserite:
#include <stdio.h>

main ()
{
printf("Hello World!");
}

Dopodiche ho compilato.
Infine ho eseguito il programma.
Peccato che non accadia niente!!!
A vedere la cosa così le ragioni per cui potrebbe non funzionare a mio punto di vista sono 3:

1)quando ho creato il progetto ha creato del codice in C++ mentre io ho scritto le righe di codice in C.
2)Ho inserito le mie riche di codice in una posizione sbagliata.
3)Esegue il programma ma non lo scrive per l'assenza di un form.

Magari sono tutti dubbi sbagliati, ma io non mi intendo per niente di C.
Mi potete aiutare???
Grazie, ciao.

andbin
10-10-2007, 14:56
Ciao a tutti!!!
Mi potete aiutare???Innanzitutto c'è una questione importante: hai creato un progetto per una applicazione Win32 "console" o non "console"?
Perché le applicazioni console hanno l'entry-point classico che è main(), mentre le applicazioni grafiche ("gui" o non console, chiamale come vuoi) hanno come entry point WinMain(). Tu hai messo tutte e due le funzioni .....

Poi comunque, in genere, gli include degli header si mettono tutti all'inizio.

La creazione e gestione della finestra ad occhio mi sembra corretta.

pas85
10-10-2007, 15:00
prova ad aggiungere getchar(); alla fine e vedi ke va

Malta
10-10-2007, 15:06
@andbin
grazie della risposta.
Forse ho capito.
Io ho creato un applicazione GUI e ho inserito di seguito le righe suggerite dalla guida che evidentemente si riferivano a un applicazione console (però non l'avevano specificato:D ).
Quindi alla creazione del progetto GUI mi ha inserito l'entry point della GUI e in più, dopo ho inserito quello della console.
Ora provo a inserire le righe in un applicazione console.
Ti ringrazio per l'aiuto.
Ciao.

marko.fatto
10-10-2007, 15:07
anzichè iniziare facendo direttamente un progetto fai semplicemente file-->nuovo-->file sorgente

e ci incolli quelle due righe...

fai compila e esegui

ps: quando lo salvi cambia il tipo in "c source file" e aggiungi il .c al nome

Malta
10-10-2007, 15:08
@marko.fatto
ringrazio anche te per il consiglio.
Ciao.

pas85
10-10-2007, 23:38
#include <stdio.h>

main ()
{
printf("Hello World!");
getchar();
}

Il codice giusto dovrebbe essere questo xkè se non metti getchar o System "PAUSE" il messaggio a video non riesci a vederlo xkè compare x un millesimo di secondo...
Spero d'aver risolto il tuo problema, prima avevo dato una risp affrettata xkè dovevo scappare;)

71104
11-10-2007, 10:19
1)quando ho creato il progetto ha creato del codice in C++ mentre io ho scritto le righe di codice in C.
2)Ho inserito le mie riche di codice in una posizione sbagliata.
3)Esegue il programma ma non lo scrive per l'assenza di un form.
4) hai messo due main :D

nel codice autogenerato (che tra l'altro non controlla il valore di ritorno della CreateWindowEx) c'era già un WinMain, tu ci hai aggiunto un main. l'esempio che hai trovato (quelle poche righe col main semplice) è corretto ma deve essere compilato da solo, senza nessun altro codice.

71104
11-10-2007, 10:19
ecco, vedo che te n'eri già accorto :p

@andbin
grazie della risposta.
Forse ho capito.
Io ho creato un applicazione GUI e ho inserito di seguito le righe suggerite dalla guida che evidentemente si riferivano a un applicazione console (però non l'avevano specificato:D ).
Quindi alla creazione del progetto GUI mi ha inserito l'entry point della GUI e in più, dopo ho inserito quello della console.
Ora provo a inserire le righe in un applicazione console.
Ti ringrazio per l'aiuto.
Ciao.

Malta
13-10-2007, 10:42
Ringrazio tutti per i suggerimenti dati.
Ciao.