Archived

This topic is now archived and is closed to further replies.

bmsfx

little error in my win32 appl.

Recommended Posts

i got this code
	case WM_TIMER:
	{
	unsigned long CurTickValue = GetTickCount();
    unsigned int Difference = CurTickValue - CompTime;
    unsigned int ApplicationHours, ApplicationMinutes, ApplicationSeconds;
    ApplicationHours = (Difference / (3600 * 999)) % 24;
    ApplicationMinutes = (Difference / (60 * 999)) % 60;
    ApplicationSeconds = (Difference / 999) % 60;

	char * apph = ( char * ) ApplicationHours;
	char * appm = ( char * ) ApplicationMinutes;
	char * apps = ( char * ) ApplicationSeconds;
	
	char *str1 = " hrs";
    char *str2 = " mins";
	char *str3 = " secs";

	char Strbuffer[256];
    strcpy(Strbuffer, apph);
    strcat(Strbuffer, str1);
	strcat(Strbuffer, appm);
    strcat(Strbuffer, str2);
	strcat(Strbuffer, apps);
	strcat(Strbuffer, str3);

	SendMessage(hAppTime, WM_SETTEXT, 0, (LPARAM)(LPSTR)Strbuffer);
	return TRUE;
	}
	break;
problem is, when i start the program, it crashes, im pretty sjure it got something to do with the : char * apph = ( char * ) ApplicationHours; char * appm = ( char * ) ApplicationMinutes; char * apps = ( char * ) ApplicationSeconds; and me putting it into my editbox (unsigned int to char) anyone have an idea how i can pack the text + time together so i get a: 0 hrs 0 mins 1 secs in my edit box ? (i prefer without the program crashing ) i looked true string.h and some of the other .h files, i cant really find anything, i am kinda new

Share this post


Link to post
Share on other sites
You are basically turning a value into a pointer :

ApplicationHours is of type unsigned long. It''s a variable (it contains a value).
apph is of type char*. It''s a pointer to a variable (it contains a memory address).

What your program is doing now, is computing a new value in "ApplicationHours" (suppose the value is X), then trying to read a string at address X in memory. Obviously the chance of the memory at position X actually belonging to you is small, and the chance of it containing what you want is smaller.

What you MIGHT want to do however is :

sprintf( StrBuffer, "%d hrs %d mins %d secs, ApplicationHours, ApplicationMinutes, ApplicationSeconds );

Victor Nicollet, INT13 game programmer

Share this post


Link to post
Share on other sites