DLL permanently running

Hi all,

Is it possible to have a DLL permanently running?

What I want is to have a piece of software that is permanently receiving msmq messages and performing calculations and asyncronously responds to external calls.

Can I do this with a DLL?

Thanks.

ScreenFoxAsked:
Who is Participating?
 
Subrat (C++ windows/Linux)Connect With a Mentor Software EngineerCommented:
I hope a windows service can do your job.
0
 
sarabandeConnect With a Mentor Commented:
you need an application (.exe) which either could run from startup folder or is defined as entry below  Software\Microsoft\Windows\CurrentVersion\Run (in HKCU or HKLM) or was added to Windows\Tasks. you also could run it as an installed service though that would need to write a special interface for being managed by SCM (service control manager). the application could call an dll immediately in main (or WinMain if Win32) and all the work could be done in the dll then.

Sara
0
 
ScreenFoxAuthor Commented:
Thanks for your replies.

Sarabande, I don't think that fits me. I'll tell you with more detail so that you can understand better what I need.

I have an application, let's call it the Data Hub, which receives msmq messages carrying Stock Prices data. It receives around 1000 messages per second. It also performs some calculation with incoming data. For example, it counts the number of received messages (a simple example).

What I want is that this application can respond asyncronously to another application asking for data. For example a standard Form (VB6) application, call it Desktop Application, retreiving the updated number of received messages.

I thought first that the Data Hub could write it's output on a database or a simlpe file, but bearing in mind that the frequence of calculation is high and may be higher in the future, I don't think it is a good idea.

Using a DLL sounds good to me ni case It can be permanently running updating the data and respond to data requests (maybe twice a second) from one or more applications.

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jkrConnect With a Mentor Commented:
All you need for that is basically an executable that loads the DLL and calls an entry point to transfer control to the DLL, from where you could e.g. spawn a thread and then return to the calling application. That could also be a VB app.
0
 
sarabandeConnect With a Mentor Commented:
a dll is an executable file same as an application (.exe). the difference only is that the application has a main function and an own process created while the dll runs in the context of another process. the dll is a library dynamically linked to another executable. it cannot run stand-alone.

the DataHub application could run in an infinite loop and can do both, receiving stock values AND respond on requests from foreground applications.

int main()
{
...
   while (!user_requested_exit)
   {
          get_new_stock_prices();

          handle_foreground_requests();

          Sleep(0); 
   }
   return 0;
}

Open in new window


the above design would require that both functions get_new_stock_prices and handle_foreground_requests are not blocking. that means those functions would check whether there are new tasks to do and if not would return immediately. alos none of these functions would do lengthy jobs. of course you also could have two independent threads which may block but that would make it more difficult to synchronize the access to the shared data and to terminate on user request.

Sara
0
 
ScreenFoxAuthor Commented:
Understood. Thank you.

But how do I respond to foreground requests?
0
 
sarabandeConnect With a Mentor Commented:
note, a service is a special executable mostly realized by an application. the main function of the executable normally would "install" the program for access by the SCM. the SCM (normally) would start the service at boot-time and use the executable similar like a dll, that means it calls functions out of the program that were registered like callback function pointers. a service has the advantage that it automatically was a background process. however that makes it more difficult to communicate with a foreground process. if you would use a normal application you easily could use windows messages for that cause both background and foreground processes belong to the same owner and share the same desktop.

anyway, you first can start with a normal windows or console application and later turn it to a service if it worked.

Sara
0
 
sarabandeConnect With a Mentor Commented:
the handle_foreground_requests could for example instantiate a pseudo window and call PeekMessage(...) on that. the foreground application would send requests by calling PostMessage on the window handle of the pseudo window. the data could be passed via shared memory or via global memory handles.

if using a service you probably better would have a p2p (process-to-process) communication via shared memory only.

Sara
0
 
sarabandeConnect With a Mentor Commented:
communication via windows messages and PostMessage has the advantage that the messaging already is synchronized and thread-safe. so you can send two messages and need not to fear the second message overwrites anything passed with the first message.

a first prototype of handle_foreground_requests could look like

void handle_foreground_requests()
{
    ....
    if (PeekMessage(...))
    {
         GetMessage(...);
         DispatchMessage(...);
    }
}

Open in new window


The DispatchMessage would call the WindowProc function associated withe the pseudo window. it would handle the different requests like

LRESULT CALLBACK WindowProc(UINT msg, WPARAM wp, LPARAM lp)
{
      switch (msg)
      {
         case WM_GET_STOCK_PRICE:
               StockPrice price = get_stock_price(wp, lp);
               PostMessage(hwnd_client, WM_RET_STOCK_PRICE, (WPARAM)price.price_as_int(), lp);
               break;

         ...

Open in new window


in the sample it is assumed that onyl a single price was requested. if a request wants more data you would need to use global or shared memory and pass only the handles to that memory via PostMessage.

the WM_ message codes could be defined freely as WM_USER + nnn in some (shared) header file.

Sara
0
 
tampnicConnect With a Mentor Commented:
"I thought first that the Data Hub could write it's output on a database or a simlpe file, but bearing in mind that the frequence of calculation is high and may be higher in the future, I don't think it is a good idea."

Have you considered an in-memory database - SQLite could accomplish this very well. All the benefits of SQL to retrieve/process your data! You can delete old records and vacuum if the memory footprint becomes too large

Cheers,
  Chris
0
All Courses

From novice to tech pro — start learning today.