Can a DLL use timer function?

This is a discussion on Can a DLL use timer function? within the C Programming forums, part of the General Programming Boards category; Hi all, For some reasons I have to write a C dll library that to be called by a C# ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    4

    Can a DLL use timer function?

    Hi all,

    For some reasons I have to write a C dll library that to be called by a C# application. My problem is as follow: Can a C DLL use timer function? What I want to achieve is that I want to set a predfined amount of time(let say, 10 ms), when it expires, it will generate an interrupt and call my function? Would it be possible to achieve it using C under win XP? If it is so, can anyone point me where I can find the code to achieve it? Thanks a lot!~

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Can a C DLL use timer function?
    yes, look at the SetTimer() function.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Thanks for the reply. Actually I am not quite understand how to use setTimer function. The function protype is as follow:

    UINT_PTR SetTimer( HWND hWnd,
    UINT_PTR nIDEvent,
    UINT uElapse,
    TIMERPROC lpTimerFunc
    );

    What should I pass to hWnd? Since I am in a dll, I tried to pass NULL to it, the code did compile but it won't generate an interrupt when the time expires. Can you say something more about that? Thanks a lot!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You can pass NULL as the hwnd parameter. Here is an example on how to use it:

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent,DWORD dwTime)
    {
    	printf("timer expired\n");
    }
    
    int main(void)
    {
    	MSG Msg;
    
    	/* Set up the timer to go off every second */
    	SetTimer(NULL,0,1000,TimerProc);
    
    	/* Process all messages */
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return 0;
    }

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It should be emphasised that, as in bithub's sample, SetTimer requires a message loop to be run on the same thread. For a DLL, this means either that you start a new thread that calls SetTimer and runs a message loop, or you have a requirement that the calling thread runs a message loop.

    There are some other alternatives. Timer queues were introduced with Windows 2000. These use a thread from the thread-pool to run the callback and are fairly simple to set up:
    Code:
    #define _WIN32_WINNT 0x0500
    #include <windows.h>
    #include <stdio.h>
    
    /* Functions in your dll. */
    HANDLE g_hTimer;
    
    VOID CALLBACK TimerCallback(PVOID lpParameter, BOOLEAN reserved)
    {
    	/* Remember this callback is executed in a different thread,
    	 * so use synchronization if needed. */
    
    	printf("Timer called\n");
    }
    
    void Start(void)
    {
    	PVOID param    = NULL; /* You can use this to pass a value to the callback. */
    	DWORD interval = 100;  /* Interval in milliseconds. */
    
    	CreateTimerQueueTimer(&g_hTimer, NULL, TimerCallback, param, interval, interval, 0);
    }
    
    void End(void)
    {
    	DeleteTimerQueueTimer(NULL, g_hTimer, INVALID_HANDLE_VALUE);
    }
    
    
    /* Calling function. */
    int main(void)
    {
    	Start();
    
    	getchar();
    
    	End();
    
    	getchar();
    	return 0;
    }
    Another option is CreateWaitableTimer. You would have to start a new thread and wait on your created timer in a loop. It has the advantage over timer queues that it is available on earlier platforms.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    4
    Thanks all of you. You all are really very helpful. I appreciate much with all of your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21