Problem with SetTimer() in another cpp file than main.

I have a problem with SetTimer when I split the main (Test13.cpp) file up into 3 files. Test13.cpp, display.cpp and general.h. It works fine when all is in one file. Compile fine in both cases but it seem like the timer don't get started when called from Test13.cpp in display.cpp. My quiestion is. Can't SetTimer() not bee set in an external file?

Here is a print from the 3 files. First Test13.cpp then display.cpp and general.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        HWND hWndButton;
        switch(Message) {
                case WM_PAINT:
                        WriteText(iXpos, iYpos, szMessage);
                        break;

        		case WM_TIMER:
                        PrintOut();
                        break;

                case WM_CLOSE:
                        DestroyWindow(hWnd);
                        break;

                case WM_DESTROY:
                        PostQuitMessage(0);
                        break;

                case WM_CREATE:{
                        hWndButton = CreateWindowEx(0,          /* more or ''extended'' styles */
                        TEXT("BUTTON"),                         /* GUI ''class'' to create */
                        TEXT("Run "),                           /* GUI caption */
                        WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   /* control styles separated by | */
                        260,                                      /* LEFT POSITION (Position from left) */
                        185,                                    /* TOP POSITION  (Position from Top) */
                        47,                                     /* WIDTH OF CONTROL */
                        25,                                     /* HEIGHT OF CONTROL */
                        hWnd,                                   /* Parent window handle */
                        (HMENU)ID_BUTTON,                       /* control''s ID for WM_COMMAND */
                        ghInstance,                             /* application instance */
                        NULL);
                        break;
                }

                case WM_COMMAND:{
                        if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED)){
                            int iMID;
                            iMID = LOWORD(wParam);
                            switch(iMID){
                                case ID_BUTTON:{
                                    strcpy(szMessage, "PowerBall Numbers:                                        ");
                                    iXpos = 10;
                                    iYpos = 40;
                                    InvalidateRect(hWnd, NULL, NULL);
                                    iStr_X = 0;
                                    Randomize();
                                    break;
                                }
                                default:
                                break;
                            }
                        }
                        break;
                }
                default:
                return DefWindowProc(hWnd, Message, wParam, lParam);
        }


This is display.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <windows.h>
#include "general.h"

/*****************************************************************************/

int sort_function( const void *a, const void *b)
{
        return( strcmp((char *)a,(char *)b) );
}
/*****************************************************************************/
void Randomize()
{

int  iPtr;

   /* Setup a random seed number from computer time */

    srand(time(NULL));

   /* Get random numbers 1 to 5 between 1 and 59 */

    for (iPtr = 0; iPtr < 5; iPtr++) {

       /* Check for numbers are the same. If so redo */

        do {
            iRand[iPtr] = ((rand() % 59) + 1);
        } while (iRand[iPtr] == (iRand[0] || iRand[1] ||
                                 iRand[2] || iRand[3] ||
                                 iRand[4]));
    }

   // Sort the numbers
    qsort(iRand, 5, sizeof(iRand[0]), sort_function);

   // Do the powerball. 1 to 39
    iRand[5] = ((rand() % 39) + 1);
   // set up our timer
    Timer_ID = SetTimer(hWnd , ID_TIMER_1, 500 , NULL);
    iLevelAnimation = 0;
}

/*****************************************************************************/
void PrintOut()
{

    switch(iStr_X) {
        case 0:
            SetAnimation(150, 40, iRand[0]);
            break;

        case 1:
            SetAnimation(180, 40, iRand[1]);
            break;

        case 2:
            SetAnimation(210, 40, iRand[2]);
            break;

        case 3:
            SetAnimation(240, 40, iRand[3]);
            break;

        case 4:
            SetAnimation(270, 40, iRand[4]);
            break;

        case 5:
           // Convert random number to ascii string
            strcpy(szMessage, "PowerBall:                        ");
            iXpos = 10;
            iYpos = 70;
           // Print out
            InvalidateRect(hWnd, NULL, NULL);
            iStr_X++;
            break;

        case 6:
            SetAnimation(150, 70, iRand[5]);
            break;

        case 7:
           // Convert random number to ascii string
            strcpy(szMessage, "*** GOOD LUCK ***");
            iXpos = 85;
            iYpos = 140;
           // Print out
            InvalidateRect(hWnd, NULL, NULL);
            iStr_X++;
            break;

        default:
        KillTimer(hWnd, ID_TIMER_1);
    }
}


And at last general.h


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define ID_TIMER_1  101
#define ID_BUTTON   102

UINT Timer_ID;
HWND hWnd;

// Create randomize numbers
void Randomize(void);

// Do the numbers on the screen
void PrintOut(void);

// Setup animation
void SetAnimation(int Xp, int Yp, int Number);

// Do the numbers make numbers scrolling unt reach value
void Animation(int Xp, int Yp, int Number);

// Help function for quick sort
int sort_function( const void *a, const void *b);

// Global variables

BOOL bErr = FALSE;
static int  iXpos;
static int  iYpos;
static int  iStr_X;
static int  iLevelAnimation;
static int  iRefRand;
       char szMessage[100];
       int  iRand[6];
Just for kicks try using extern UINT Timer_ID; in the source files to reference your global in general.h
Same result. This is what I did. Declared it in main and made it extern in general.h. Compile fine. Damm weird. Thanks for the input.

#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "general.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
// Make global

UINT Timer_ID;
HWND hWnd;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static char gszClassName[]  = "PowerBall"; //"darkblue";
static HINSTANCE ghInstance = NULL;

/*****************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {



1
2
3
4
5
6
7
8
#define ID_TIMER_1  101
#define ID_BUTTON   102

extern UINT Timer_ID;
extern HWND hWnd;

// Create randomize numbers
void Randomize(void);
Sorry. Didn't see you wrote source file. Will try that too.
Same result. Well you should think so because it declared extern in general.h but you never know about those compilers. By the way it's Borland C++ ver 5.03.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <windows.h>
#include "general.h"

extern UINT Timer_ID;
extern HWND hWnd;

/*****************************************************************************/

int sort_function( const void *a, const void *b)
{
        return( strcmp((char *)a,(char *)b) );
}
/*****************************************************************************/
void Randomize()
Now it's getting real weird. I took the code from display.cpp and coy/paste into the main file. Delete display.cpp from the project and compiled it. Still kept the general.h file. Now the code works fine. What is the different????? Help. I don't understand. Maybe a beer will help out.
Good news. Got it to work. Change all the variables in general.h to extern. Moved all the variables into display.cpp and got rid of the static declaresions. I think it was that static declare that did it. Thanks for your response. This was driving me nut's.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <windows.h>
#include "general.h"

// Global varible

BOOL bErr = FALSE;
int  iXpos;
int  iYpos;
int  iStr_X;
int  iLevelAnimation;
int  iRefRand;
char szMessage[100];
int  iRand[6];

/*****************************************************************************/

int sort_function( const void *a, const void *b)
{
        return( strcmp((char *)a,(char *)b) );
}
/*****************************************************************************/
void Randomize()



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#define ID_TIMER_1  101
#define ID_BUTTON   102

extern UINT Timer_ID;
extern HWND hWnd;

// Create randomize numbers
void Randomize(void);

// Do the numbers on the screen
void PrintOut(void);

// Setup animation
void SetAnimation(int Xp, int Yp, int Number);

// Do the numbers make numbers scrolling unt reach value
void Animation(int Xp, int Yp, int Number);

// Help function for quick sort
int sort_function( const void *a, const void *b);

// Global variables

extern int  iXpos;
extern int  iYpos;
extern int  iStr_X;
extern int  iLevelAnimation;
extern int  iRefRand;
extern char szMessage[100];
extern int  iRand[6];
I forgot what static means. Ha Ha.

A static variable inside a function keeps its value between invocations.
A static global variable or a function is "seen" only in the file it's declared in
Only use static in function's to keep the value between calls.
Last edited on
I'm 99% sure that the variables you declare as extern in general.h are not the same variable you've defined in display. Meaning the ones in general aren't even used.
Topic archived. No new replies allowed.