home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / winsock / rnr / rnrsetup / rnrsetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-18  |  8.6 KB  |  312 lines

  1. /*++
  2.  
  3. Copyright (c) 1992 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     RnrClnt.c
  8.  
  9. Abstract:
  10.  
  11.     Setup program for installing/removing the "EchoExample" service.
  12.  
  13. --*/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <windows.h>
  18. #include <winsock2.h>
  19. #include <nspapi.h>
  20.  
  21. WSADATA WsaData;
  22.  
  23. #ifndef NS_NTDS
  24. #define NS_NTDS 9999
  25. #endif
  26. //
  27. // GUID for Echo-Example created with uuidgen:
  28. //     "47da8500-96a1-11cd-901d-204c4f4f5020"
  29. //
  30.  
  31. GUID ServiceGuid = { 0x47da8500, 0x96a1, 0x11cd, 0x90, 0x1d,
  32.                      0x20, 0x4c, 0x4f, 0x4f, 0x50, 0x20 };
  33.  
  34. #define ECHO_SERVICE_TYPE_NAME "EchoExample"
  35. #define ECHO_SERVICE_SAPID     999
  36. #define ECHO_SERVICE_TCPPORT   999
  37. #define RNR_SERVICE_NAME       "RnrSvc"
  38. #define RNR_DISPLAY_NAME       "RnrSampleService"
  39.  
  40. void
  41. DoServiceSetup(
  42.     char * Path
  43.     )
  44. {
  45.     SC_HANDLE ServiceManagerHandle;
  46.     SC_HANDLE ServiceHandle;
  47.     LPSTR KeyName = "System\\CurrentControlSet\\Services\\EventLog\\System\\RnrSvc";
  48.     HKEY RnrKey;
  49.     LONG err;
  50.     DWORD Disposition;
  51.  
  52.     //
  53.     //  Create the service.
  54.     //
  55.  
  56.     ServiceManagerHandle = OpenSCManager( NULL,
  57.                                           NULL,
  58.                                           STANDARD_RIGHTS_REQUIRED
  59.                                           | SC_MANAGER_CREATE_SERVICE );
  60.  
  61.     if( ServiceManagerHandle == NULL ) {
  62.         printf( "OpenSCManager failed: %ld\n", GetLastError() );
  63.         exit(1);
  64.     }
  65.  
  66.     ServiceHandle = CreateService( ServiceManagerHandle,
  67.                                    RNR_SERVICE_NAME,
  68.                                    RNR_DISPLAY_NAME,
  69.                                    GENERIC_READ | GENERIC_WRITE,
  70.                                    SERVICE_WIN32_OWN_PROCESS,
  71.                                    SERVICE_DEMAND_START,
  72.                                    SERVICE_ERROR_NORMAL,
  73.                                    Path,
  74.                                    NULL,
  75.                                    NULL,
  76.                                    NULL,
  77.                                    NULL,
  78.                                    NULL );
  79.  
  80.     if( ServiceHandle == NULL ) {
  81.         printf( "CreateService failed: %ld\n", GetLastError() );
  82.         CloseServiceHandle( ServiceManagerHandle );
  83.         exit(1);
  84.     }
  85.  
  86.     CloseServiceHandle( ServiceHandle );
  87.     CloseServiceHandle( ServiceManagerHandle );
  88.  
  89.     printf( "%s created with path %s\n",
  90.             RNR_SERVICE_NAME,
  91.             Path );
  92.  
  93.     //
  94.     //  Add the data to the EventLog's registry key so that the
  95.     //  log insertion strings may be found by the Event Viewer.
  96.     //
  97.  
  98.     err = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
  99.                           KeyName,
  100.                           0,
  101.                           NULL,
  102.                           REG_OPTION_NON_VOLATILE,
  103.                           KEY_WRITE,
  104.                           NULL,
  105.                           &RnrKey,
  106.                           &Disposition );
  107.  
  108.     if( err != 0 ) {
  109.         printf( "RegCreateKeyEx failed: %ld\n", err );
  110.         exit(1);
  111.     }
  112.  
  113.     err = RegSetValueEx( RnrKey,
  114.                          "EventMessageFile",
  115.                          0,
  116.                          REG_EXPAND_SZ,
  117.                          Path,
  118.                          strlen( Path ) + 1 );
  119.  
  120.     if( err == 0 ) {
  121.         DWORD Value;
  122.  
  123.         Value = EVENTLOG_ERROR_TYPE
  124.                 | EVENTLOG_WARNING_TYPE
  125.                 | EVENTLOG_INFORMATION_TYPE;
  126.  
  127.         err = RegSetValueEx( RnrKey,
  128.                              "TypesSupported",
  129.                              0,
  130.                              REG_DWORD,
  131.                              (CONST BYTE *)&Value,
  132.                              sizeof(Value) );
  133.     }
  134.  
  135.     RegCloseKey( RnrKey );
  136.  
  137.     if( err != 0 ) {
  138.         printf( "RegSetValueEx failed: %ld\n", err );
  139.         exit(1);
  140.     }
  141.  
  142.     exit(0);
  143. }
  144.  
  145. void _CRTAPI1
  146. main (
  147.     int argc,
  148.     char *argv[]
  149.     )
  150. {
  151.     INT err;
  152.  
  153.     WSASERVICECLASSINFO ServiceClassInfo;
  154.     WSANSCLASSINFO      lpNSClassInfo[6];
  155.  
  156.     DWORD Value1 = 1 ;
  157.     DWORD SapValue = ECHO_SERVICE_SAPID ;
  158.     DWORD TcpPortValue = ECHO_SERVICE_TCPPORT ;
  159.     DWORD operation = SERVICE_ADD_TYPE;
  160.  
  161.     //
  162.     // Initilize the Windows Sockets DLL.
  163.     //
  164.  
  165.     err = WSAStartup( 0x0202, &WsaData );
  166.     if ( err == SOCKET_ERROR ) {
  167.         printf( "WSAStartup() failed: %ld\n", GetLastError( ) );
  168.         exit(1);
  169.     }
  170.  
  171.     //
  172.     // Parse command-line arguments.
  173.     //
  174.  
  175.     if (argc > 2) {
  176.         printf( "usage: rnrsetup [/ADD | /DEL | /SVC:path]\n") ;
  177.         exit(1);
  178.     }
  179.  
  180.     if (argc == 2)
  181.     {
  182.         if ( _strnicmp( argv[1], "/add", 4 ) == 0 )
  183.         {
  184.             printf( "\nAdding service types to Rnr name spaces.\n" );
  185.         }
  186.         else if ( _strnicmp( argv[1], "/delete", 4 ) == 0 )
  187.         {
  188.             err = WSARemoveServiceClass( &ServiceGuid );
  189.  
  190.             WSACleanup();
  191.  
  192.             if ( err != NO_ERROR )
  193.             {
  194.                 printf( "\nWSARemoveServiceClass failed: %ld\n",
  195.                         GetLastError( ) );
  196.                 exit(1);
  197.             }
  198.  
  199.             printf( "\nWSARemoveServiceClass succeeded\n" );
  200.  
  201.             exit(0);
  202.         }
  203.         else if ( _strnicmp( argv[1], "/svc:", 5 ) == 0 )
  204.         {
  205.             printf( "\nAdding service entry to service control manager.\n" );
  206.             DoServiceSetup( strchr( argv[1], ':' ) + 1 );
  207.             printf( "Adding service types to Rnr name spaces.\n" );
  208.         }
  209.         else
  210.         {
  211.             printf( "usage: rnrsetup [/ADD | /DEL | /SVC:path]\n") ;
  212.             exit(1);
  213.         }
  214.     }
  215.  
  216.     //
  217.     // Set up information to pass to NSPInstallServiceClass() or
  218.     // NSPRemoveServiceClass() to add or delete this
  219.     // service type.
  220.     //
  221.  
  222.     ServiceClassInfo.lpServiceClassId = &ServiceGuid;
  223.     ServiceClassInfo.lpszServiceClassName = ECHO_SERVICE_TYPE_NAME;
  224.     ServiceClassInfo.dwCount = 6;
  225.     ServiceClassInfo.lpClassInfos = lpNSClassInfo;
  226.  
  227.     //
  228.     // - - - SAP provider setup - - -
  229.     //
  230.     // The first value tells SAP that this is a connection-oriented
  231.     // service.
  232.     //
  233.     lpNSClassInfo[0].lpszName = SERVICE_TYPE_VALUE_CONN ;
  234.     lpNSClassInfo[0].dwNameSpace = NS_SAP ;
  235.     lpNSClassInfo[0].dwValueType = REG_DWORD ;
  236.     lpNSClassInfo[0].dwValueSize = 4 ;
  237.     lpNSClassInfo[0].lpValue     = &Value1 ;
  238.  
  239.     //
  240.     // Next, give SAP the object type to use when broadcasting the
  241.     // service name.
  242.     //
  243.     lpNSClassInfo[1].lpszName = SERVICE_TYPE_VALUE_SAPID ;
  244.     lpNSClassInfo[1].dwNameSpace = NS_SAP ;
  245.     lpNSClassInfo[1].dwValueType = REG_DWORD ;
  246.     lpNSClassInfo[1].dwValueSize = sizeof(DWORD) ;
  247.     lpNSClassInfo[1].lpValue     = &SapValue ;
  248.  
  249.     //
  250.     // - - - TCPIP provider setup - - -
  251.     //
  252.     // Tell the TCPIP name space provider that we will be using TCP
  253.     // port 0x999.
  254.     //
  255.     lpNSClassInfo[2].lpszName = SERVICE_TYPE_VALUE_TCPPORT ;
  256.     lpNSClassInfo[2].dwNameSpace = NS_DNS ;
  257.     lpNSClassInfo[2].dwValueType = REG_DWORD ;
  258.     lpNSClassInfo[2].dwValueSize = sizeof(DWORD) ;
  259.     lpNSClassInfo[2].lpValue     = &TcpPortValue ;
  260.  
  261.     //
  262.     // - - - NTDS provider setup - - -
  263.     //
  264.     // The first value tells SAP that this is a connection-oriented
  265.     // service.
  266.     //
  267.     lpNSClassInfo[3].lpszName = SERVICE_TYPE_VALUE_CONN ;
  268.     lpNSClassInfo[3].dwNameSpace = NS_NTDS ;
  269.     lpNSClassInfo[3].dwValueType = REG_DWORD ;
  270.     lpNSClassInfo[3].dwValueSize = 4 ;
  271.     lpNSClassInfo[3].lpValue     = &Value1 ;
  272.  
  273.     //
  274.     // Next, give SAP the object type to use when broadcasting the
  275.     // service name.
  276.     //
  277.     lpNSClassInfo[4].lpszName = SERVICE_TYPE_VALUE_SAPID ;
  278.     lpNSClassInfo[4].dwNameSpace = NS_NTDS ;
  279.     lpNSClassInfo[4].dwValueType = REG_DWORD ;
  280.     lpNSClassInfo[4].dwValueSize = sizeof(DWORD) ;
  281.     lpNSClassInfo[4].lpValue     = &SapValue ;
  282.  
  283.     //
  284.     // Tell the NTDS name space provider that we will be using TCP
  285.     // port 0x999.
  286.     //
  287.     lpNSClassInfo[5].lpszName = SERVICE_TYPE_VALUE_TCPPORT ;
  288.     lpNSClassInfo[5].dwNameSpace = NS_NTDS ;
  289.     lpNSClassInfo[5].dwValueType = REG_DWORD ;
  290.     lpNSClassInfo[5].dwValueSize = sizeof(DWORD) ;
  291.     lpNSClassInfo[5].lpValue     = &TcpPortValue ;
  292.  
  293.     //
  294.     // Finally, call WSAInstallServiceClass to actually perform the operation.
  295.     //
  296.  
  297.     err = WSAInstallServiceClass( &ServiceClassInfo );
  298.  
  299.     WSACleanup();
  300.  
  301.     if ( err != NO_ERROR )
  302.     {
  303.         printf( "WSAInstallServiceClass failed: %ld\n", GetLastError( ) );
  304.         exit(1);
  305.     }
  306.  
  307.     printf( "WSAInstallServiceClass succeeded\n" );
  308.  
  309.     exit(0);
  310.  
  311. } // main
  312.