Solved

DLL permanently running

Posted on 2011-09-12
10
316 Views
Last Modified: 2012-05-12
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.

0
Comment
Question by:ScreenFox
10 Comments
 
LVL 8

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 63 total points
ID: 36522154
I hope a windows service can do your job.
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 312 total points
ID: 36522270
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
 

Author Comment

by:ScreenFox
ID: 36522776
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 63 total points
ID: 36523236
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
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 312 total points
ID: 36523259
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ScreenFox
ID: 36523281
Understood. Thank you.

But how do I respond to foreground requests?
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 312 total points
ID: 36523354
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
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 312 total points
ID: 36523404
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
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 312 total points
ID: 36523652
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
 
LVL 7

Assisted Solution

by:tampnic
tampnic earned 62 total points
ID: 36523941
"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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

9 Experts available now in Live!

Get 1:1 Help Now