Solved

Implementing SetTimer from ISAPI Filter DLL

Posted on 1997-07-18
10
473 Views
Last Modified: 2006-11-17
I'm trying to set up a timer callback function with SetTimer in an ISAPI Filter DLL. In the CWinApp Constructor, I call SetTimer passing it the address of my TimerProc => SetTimer(NULL,NULL,300,TimerProc);

the TimerProc is defined as Follows:

extern "C" void CALLBACK EXPORT TimerProc(HWND hWnd,UINT nMsg,UINT nEvt, DWORD dwSysTime);

the SetTimer gets called but TimerProc doesn't ever get called.)

This one's worth all the points I have.
0
Comment
Question by:vince123
  • 5
  • 5
10 Comments
 
LVL 3

Accepted Solution

by:
jaba earned 200 total points
ID: 1165346
How is timer working ? When you installing timer ( even without hwnd param and with callback function) it implementing by the messages. I.e.  you message loop translating message to call of this callback funtion. Timer even ( event when you xalling SetTimer without hwnd param) is a WM_TIMER message. You callback function calling while TranslateMessage function processing. So , you cant get timer without message loop. You ISAPI dll dont have it :-( So , ther is 2 solutions of you problem:
1 - create message loop ( but its not good solution)
2 - Uee Sleep funtion as shown in sampel below :
do {
[You processing code ]
Sleep(1000 ) ; // waiting 1 second
} while (...) ;

This methodis best because it dont eat processor resources while waiting next event

0
 

Author Comment

by:vince123
ID: 1165347
   I think this is still a problem. where would you have me put the sleep call? if in the OnAuthentication method, every request would end up calling the cleanup function. Conversely when there were no requests to the server, the function wouldn't run. Both undesirable situations. I'll try to clarify the scenario.
    An Isapi filter DLL for authentication will get called for every request that comes to the server. My routine must decide to allow the request or deny. User information is stored in SQL Server. It wouldn't be feasible to check the database every request. A single page of html with a dozen graphics would generates 13 requests. To make the process more efficient I maintain in memory a linked list of user objects which have already been authenticated. Each time they make a request, a time field in the user object is updated. If the user object is not updated within a certain time, they are removed from the list and would again have to be authenticated against the database in order to regain entry. I need for the timer function to run at an interval that I specify, without needing to be triggered by a client request. Imagine that I wish to examine the list once a minute and remove every user idle for longer than 15 minutes. If no requests come in for 4 or 5 minutes, I have no way to force the cleanup function to run.
0
 
LVL 3

Expert Comment

by:jaba
ID: 1165348
Nice scenario. You need to create idle thread while initialization of DLL and write somthing like this :
DWORD WINAPI MyIdleCleaner( LPVOID pParam)
{
   do {
    // Do my list cleaning  checks
   // Dont forget about syncronization
   Sleep(1000);
   } while (...)
}

0
 
LVL 3

Expert Comment

by:jaba
ID: 1165349
Obvious, MyIdleCleaner function start working after CreateThread call :-)
Do this call in DllMain on DLL_PROCESS_ATTACH .
Good luck
0
 

Author Comment

by:vince123
ID: 1165350
I like where you're headed with this, but did we switch to Win32? Remember I'm doing this with MFC and I don't have access to DllMain, or do I??? If I do, I'll try and do that and give you the points. I think, though, that I probably need to override something in CWinApp, can you clarify this?.

BTW, I know I'm making you work for these points, but this is the last part of my project before I wrap the current release. :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 3

Expert Comment

by:jaba
ID: 1165351
I am sorry , i forgot about MFC usage :-)
When you created ISAPI project , thewr is CWinApp object in you CPP file. You need create new class , derived from CWinApp , override InitInstance function and start you idle thred in it.
Like this :
class CMyISapi : public CWinApp {
public :
virtual BOOL InitInstance( );
};

BOOL CMyISapi::InitInstance()
{
CWinApp::InitInstance();
DWORD id;
CreateThread(NULL , 0 , MyIdleFunc , 0 , 0 , &id);
}


0
 

Author Comment

by:vince123
ID: 1165352
OK we're almost there I think. if we derive from CWinApp, is it better to create an idle thread or override CWinApp::OnIdle() ?
0
 
LVL 3

Expert Comment

by:jaba
ID: 1165353
Dont forget change
CWinApp theApp;
to CMyApp theApp; in you source code.
About OnIdle. This function never be called in DLL :-(
This function calling by CWinApp Run function when message loop empty. DLLs have no message loop and Run method not calling in DLLs .
0
 

Author Comment

by:vince123
ID: 1165354
ok, jaba, one more clarification and you get all the points and an A!

i'm using create thread just like you said but I get very strange compilation errors about not being able to convert the parameter to unsigned long (__stdcall *)(void *). I've tried to declare MyIdleFunc every way I can think of and it doesn't match up.
0
 

Author Comment

by:vince123
ID: 1165355
ok, jaba, never mind the implementation stuff, i figured that out on my own. you get the points and an A.

Ray@vbnetwork.com
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now