HOW TO: Debug Common Gateway Interface Applications Running Under IIS by Using Visual Studio
This article was previously published under Q238788
This step-by-step article describes how to debug Common Gateway Interface (CGI) applications that are running under Internet Information Services (IIS) by using any version of Visual Studio.
Debug a Visual C CGI Application by Using Visual StudioTo debug a Visual C CGI application by using any version of Visual Studio, follow these steps:
- The IIS Admin Service must interact with the desktop. To configure this, follow these steps:
- Open Control Panel and follow these steps:
- For Windows NT: click Services.
- For Windows 2000, Windows XP, and .NET Server: click Administrative Tools, and then click Services.
- Double-click IIS Admin Service.
- On the Log On tab, select the Allow Service to Interact with Desktop check box.
Note This article assumes that the IIS Admin Service runs as a local system.
- Stop and restart the IIS Admin Service.
- Open Control Panel and follow these steps:
- Before you include the Windows.h file, add the following code, and then build a debug version of the CGI:
#define _WIN32_WINNT 0x0400
- Paste the following code at the beginning of the CGI:
#ifdef _DEBUGchar szMessage ;wsprintf (szMessage, "Please attach a debugger to the process 0x%X (%s) and click OK", GetCurrentProcessId(), argv);MessageBox(NULL, szMessage, "CGI Debug Time!", MB_OK|MB_SERVICE_NOTIFICATION);#endif
- Start the CGI application.
- Attach a debugger (such as WinDBG or Visual C) to the CGI process while a message box is displayed.
- When the debugger is attached, open the source file and set a break point, and then click OK.
When the message box is closed, the CGI program resumes and the break point will be hit.
Using WinDBGTo debug a CGI application with WinDBG, although you do not have to change any code, you do have to be familiar with WinDBG and install the debugging tools. You can download the debugging tools from the following Microsoft Web site:
Microsoft Debugging Tools
Set Service OptionsSet the Image File Execution options so that when the service (or the CGI program) starts, it starts under the debugger. Make sure that the service can interact with the desktop, as instructed earlier in this article. Otherwise, when the CGI program starts, it loads the debugger and you will not see it.
- Start Registry Editor (Regedt32.exe):
Click Start, click Run, and then type regedt32.
- Locate the following registry key:[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\]
- Create a subkey with the name of your CGI executable file (for example, MyCGI.exe).
- To this subkey, add the REG_SZ value named Debugger and the full path to the debugger, but omit the ".exe" extension. You can also add any commands for the debugger. The default location to install the WinDBG is the following: "Debugger"="C:\\Debuggers\\windbg.exe"Note The path that points to the WinDBG debugger must not contain any spaces or file extensions. For example, windbg, as shown in the following:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\yourCGI.exe "Debugger"="C:\windbg"
- Close Registry Editor.
- Stop and start the IISADMIN service, and then run the CGI program.
Note For the debugger to appear on the default desktop, the IIS Admin service must run as Local System, and you must select the Allow Service to Interact with Desktop check box. To configure this, go to step 1 of the instructions earlier in this article.
- The debugger will start when the image for the Service executable file is loaded into memory.
adsutil.vbs w3svc/CreateProcessAsUser "false"
For additional information about debugging, click the article numbers below to view the articles in the Microsoft Knowledge Base:
273639 HOW TO: Start IISAdmin and Dependent Services Under a Debugger
286350 HOW TO: Use ADPlus to Troubleshoot "Hangs" and "Crashes"
261871 INFO: COM+ and MTS Debugging Resources
183480 HOW TO: Debug ISAPI DLLs Under IIS 4.0 and IIS 5.0
Article ID: 238788 - Last Review: 12/03/2007 21:26:24 - Revision: 4.7
Microsoft Internet Information Server 2.0, Microsoft Internet Information Server 3.0, Microsoft Internet Information Server 4.0, Microsoft Internet Information Services 5.0, Microsoft Internet Information Services version 5.1, Microsoft Internet Information Services 6.0
- kbcgi kbhowto kbhowtomaster KB238788