Microsoft KB Archive/295840

= How To Determine if a Service Exists Using Visual FoxPro =

Article ID: 295840

Article Last Modified on 6/30/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q295840



SUMMARY
Occasionally, the installation, configuration, or successful execution of a custom application written with Visual FoxPro (VFP) is dependent on the existence of a particular service. Given a valid computer and service name, the code in this article will let you determine if a particular service exists.



MORE INFORMATION
Services are a component of the Microsoft Windows NT and Microsoft Windows 2000 operating systems. A service is defined as:

&quot;A program, routine, or process that performs a specific system function to support other programs, particularly at a low (close to the hardware) level.&quot;

When executed on a computer running Microsoft Windows NT or Microsoft Windows 2000, the following VFP code determines if the &quot;IISADMIN&quot; service is present on the local computer (regardless of whether it is running or not).

To use this code, follow these steps:  Start Visual FoxPro and create a new program.  Paste the following code in the program window:
 * 1) DEFINE ThisMachine SUBSTR(SYS(0),1,ATC(&quot; &quot;,SYS(0))-1)

? ServiceExist(&quot;IISADMIN&quot;,ThisMachine)

PROCEDURE ServiceExist(lcServiceName, lcMachineName) IF !INLIST(UPPER(OS),&quot;WINDOWS 5.00&quot;,&quot;WINDOWS NT 4.00&quot;) RETURN .F.   ENDIF
 * ~ Prevent this procedure from running
 * ~ if we're not on NT/2000.

#DEFINE GENERIC_READ   0x80000000 DECLARE INTEGER OpenSCManager IN advapi32.DLL ; STRING, STRING, INTEGER DECLARE INTEGER OpenService IN advapi32.DLL ; INTEGER, STRING, INTEGER

LOCAL liSCM, liService liSCM = OpenSCManager(lcMachineName, NULL, GENERIC_READ) IF !(liSCM > 0) RETURN .F.   ENDIF liService = OpenService(liSCM, lcServiceName, GENERIC_READ)

RETURN IIF(liService > 0, .T.,.F.) ENDPROC  Save and then run the program. If the IISADMIN service is present on the local computer, a .T. is printed to _SCREEN; if not, a .F. is printed.

