logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Building & Member Downloads View previous topic :: View next topic
Reply to topic   Topic: apache as a DLL
Author
cartoper



Joined: 01 Mar 2007
Posts: 5

PostPosted: Sun 11 Mar '07 4:09    Post subject: apache as a DLL Reply with quote

I am using apache in a kiosk type of system. When the operator starts the kiosk server software, the server software is currently starting apache (httpd.exe) as a console app. In trying to compile apache myself(which I am still working on doing), I noticed there is a libhttpd. Does anyone know where I can go to learn more about the libhttpd and see if it might be possible for my program to simply load libhttpd rather than span an external process? If this is not the correct forum, what is?

Thanks a million!
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Sun 11 Mar '07 6:48    Post subject: Reply with quote

I don't think libhttpd is what you want. It contains functions that Apache uses, but not the Apache main program.

Here's some code to start and stop Apache without showing a console window.
Apache will still be a separate process (actually, it will be a pair of processes both named httpd).
Code:
#include <windows.h>
#include <stdio.h>
int main(int argc, char **argv)
{   int result;
    PROCESS_INFORMATION procInfo;
    STARTUPINFO startInfo;
    HANDLE shutdownEvent;
    char shutdownEventName[32];

    GetStartupInfo(&startInfo);             // same as current process's startInfo
    result = CreateProcess(                 // start up Apache
          "C:\\Apache2\\bin\\httpd.exe",        // program name
          "",                                   // command line (excluding name)
          NULL,                                 // security attributes (default)
          NULL,                                 // thread attributes (default)
          FALSE,                                // inherit handles (no)
          CREATE_NO_WINDOW,                     // flags - no console window
          NULL,                                 // environment (same as parent)
          "C:\\Apache2\\bin",                   // current directory for proc
          &startInfo,
          &procInfo
    );
    if (result) {
        CloseHandle(procInfo.hProcess);     // close these handles because they are not needed
        CloseHandle(procInfo.hThread);
    }

    /****  do whatever the kiosk system does here  ****/

    sprintf_s(shutdownEventName, sizeof(shutdownEventName), "ap%d_shutdown", procInfo.dwProcessId);
    shutdownEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, shutdownEventName);
    if (shutdownEvent != NULL)  {
        result = SetEvent(shutdownEvent);   // shutdown Apache if it's still running
    }
}

Maybe this will do what you need.

It is probably possible to make Apache callable instead of an .exe, but I think it would take a lot of work.

One problem with this plan is that if you call the Apache main function from your kiosk program, the call won't return until Apache shuts down.

You would need to start a new thread and call the Apache main function from the new thread, while your kiosk program continues in the original thread.

-tom-
Back to top
cartoper



Joined: 01 Mar 2007
Posts: 5

PostPosted: Thu 15 Mar '07 14:35    Post subject: Reply with quote

Thank you for the code, but my GUI needs to start apache once it is loaded, for it generates the httpd.conf and the php.ini file for the server, along with setting some data for the PHP scripts to do their thing. I do currently have code that start, monitors and stops apache. This is all .Net code. Currently the issue is to shutdown apache2 nicely, I have to run it in a minimized window to send the <Ctrl>+C to the window.

But I think you gave me the key thing I was looking for in the following code:
Code:

sprintf_s(shutdownEventName, sizeof(shutdownEventName), "ap%d_shutdown", procInfo.dwProcessId);
shutdownEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, shutdownEventName);

Is my impression correct that the Windows version of apache 2 has a good old Windows named event that, once set, will kill apache? If I understand the code snip above, the named event is simply the process id and then d_shutdown, correct? So if the process id was say 4200, the name would be 4200d_shutdown?
Back to top
tdonovan
Moderator


Joined: 17 Dec 2005
Posts: 611
Location: Milford, MA, USA

PostPosted: Thu 15 Mar '07 15:58    Post subject: Reply with quote

Almost right.

The name is "ap" + process_ID + "d_shutdown".

For example, if your Apache PID is 4200, the event name is:
    ap4200d_shutdown
FYI: Apache could be restarted with the event named:
    ap4200d_restart
Unlike using a Ctrl-C signal, Apache can run without a console window and still react to these events.

-tom-
Back to top
BobCaTT



Joined: 13 Apr 2007
Posts: 1

PostPosted: Fri 13 Apr '07 9:00    Post subject: Reply with quote

Hello

A small caveat in the code...
The original code was:
Code:
sprintf_s(shutdownEventName, sizeof(shutdownEventName), "ap%d_shutdown", procInfo.dwProcessId);
shutdownEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, shutdownEventName);


But you should use _T("") macro in the sprintf_s
Code:
sprintf_s(shutdownEventName, sizeof(shutdownEventName), _T("ap%d_shutdown"), procInfo.dwProcessId);
shutdownEvent = OpenEvent(EVENT_MODIFY_STATE, FALSE, shutdownEventName);


This is usefull to be sure the string is pure ascii.
Back to top
cartoper



Joined: 01 Mar 2007
Posts: 5

PostPosted: Mon 16 Apr '07 15:43    Post subject: Reply with quote

Bob,

Actually you have it backwards. In Microsoft C/C++ Land, a string simply in quotes is ALWAYS an 8-bit character string (ASCII). If you want a UNICODE (16-bit) string, you have to refix the string with a capital L, like this: L"UNICODE String". No matter how things are set, the L will ALWAYS make the string a UNICODE string.

Now there are two different versions of any WinAPI function that takes a C String, such as CreateFile, there is one that ends with a capital A for ASCII (CreateFileA), one with a W for UNICODE (CreateFileW). In the header files, you will find this:
Code:
#ifdef UNICODE
#define CreateFile  CreateFileW
#else
#define CreateFile  CreateFileA
#endif // !UNICODE

So that if you have UNICODE defined, you get the "wide" version.

Of course sprintf_s is for ANCII and swprintf_s for UNICODE (I am assuming that swprintf_s is not Microsoft specific). Well, when you include the tchar.h header, Microsoft abstracts the C String functions the same way it abstracts the WinAPI call. Rather then using sprintf_s or swprintf_s, you simply use _stprintf_s and the UNICODE macro defines which version is used.

This brings up the issue of: Do you use a L to prefix a literal string or not? This is what the _T macro solves, when UNICODE is defined, it refixes all literal strings with an L, when it is not defined, no changes are made!

Thus, by including tchar.h, using the t version of all the c string functions, and wrapping all your string in the _T macro, you can quickly and easily switch between a UNICODE and ASCII build of your program. Back in the days of Win 95/98/ME, you needed this because they could only run ASCII versions, but the NT OS native is UNICODE. Today I make ALL my own C/C++ Unicode because that is what is native to Windows NT. I continue to follow this rule because there are times when, integrating with other libraries and/or programs, like Apache, one must operate in an ANSII world and this approach makes it 100% seemless to compile my code either way.
Back to top
ashwani_ap



Joined: 29 Aug 2007
Posts: 47
Location: Bangalore

PostPosted: Wed 29 Aug '07 18:43    Post subject: Reply with quote

It is done
Back to top
rola



Joined: 03 Jan 2008
Posts: 3

PostPosted: Thu 03 Jan '08 9:35    Post subject: Reply with quote

How warmhearted you are.I'm studying Apache,so come here to learn more form you.
Back to top


Reply to topic   Topic: apache as a DLL View previous topic :: View next topic
Post new topic   Forum Index -> Building & Member Downloads