Sunday, April 15, 2012

Debug ISAPI extensions in Windows 7 and IIS 7.5 using Delphi XE2

Many things have changed in IIS from version 6 to 7 (and 7.5 that comes with Windows 7). If you are not familiarized with IIS 7 (and 7.5) debugging, maybe this post may help you. In fact, debugging ISAPI in Winodws 7 is easy if you follow a few steps:

Setting up IIS

First, you have to create a new application in IIS and set it up as usual. Two things are important during IIS and application setup:

1) In IIS, create the web application under Default Web Site.

2) Every application under Default Web Site should be using the same application pool, DefaultAppPool.

If you ignore these two rules, you will have additional configuration steps to run w3wp later.

Setting up the application in Delphi XE2

1) First and more important: Run Delphi XE2 as administrator! If you don't, Delphi may not be able to start the IIS Worker process (W3WP.exe).

2) In Delphi XE2 Ide, choose Run -> Parameters. Inform Host application and Parameters as you can see in the following picture:



W3WP.exe is the IIS Worker Process executable, and we will run it interactively to debug the ISAPI app. If you didn´t follow rules (1) and (2) of IIS Setup, then you will have to do additional configuration:
- Inform the web site using the parameter: -s "TheSiteId"
- Inform the application pool using the parameter: -ap "TheAppPoolName"
(This doesn't work with IIS 7 and above. The -ap parameter is not recognized by w3wp, so it's better to stay with tip (2) above)

Ready to go

Now that everything is ready, you have to stop the WWW publication service (W3SVC). You may use "net stop W3SVC" from an elevated command prompt, or use the Windows services console. Once the W3SVC is stopped, just run the application from Delphi XE2 IDE and call it from your browser. When the application is loaded all your breakpoints will be activated and you can easily debug the application.

Additional notes

I´ve found some references saying that you have to set the linking options "Map file -> Detailed" and "Include remote symbols -> On", in your project options. Well, in fact, the debuggin of ISAPI apps as explained here works perfeclty without the map file and the remote symbols.

8 comments:

Júlio Furquim said...

Here's a helpful post to complement yours:
http://chee-yang.blogspot.com.br/2009/10/configure-windows-7-iis7-for-isapi-dll.html

Alexandre Caldas Machado said...

Yes Júlio, Chee Yang's post about setting up IIS 7 for ISAPI is the best I've found.

Anonymous said...

Alteratively, you can use IIS Express:
http://paul.klink.id.au/Software/Delphi/DebuggingIsapiWithIisExpress.htm

Anonymous said...

You can also use ISAPI Debugger host from http://www.eggcentric.com

Anonymous said...

Good Job, perfect!!!

Anonymous said...

Hi,
I have test this with XE6 but don't work.
The Virtual Directory is under Default Web Site under iis 7.5 on windows 7 64 bit.
If i debug with "Attach to process" works fine.
Any suggestions ?

Alexandre Caldas Machado said...

@Anonymous: Are you starting XE6 as administrator? You need to start Delphi IDE using elevation, otherwise it won't work this way...

Anonymous said...

Hi Alexandre,
it works perfectly, the problem was the host.
I pointed to C: \ Windows \ SysWOW64 \ inetsrv \ w3wp.exe
I had to add the-s parameter siteid because I have a web site other than the default web site.
I had no problems with the application pool other than Default Application Pool
Many compliments for the very valuable suggestion.

Greetings