⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eiu_obj.c

📁 基于EthernetIP协议的应用程序,可以读取AB公司Controllogix系列Ethernetip协议PLC数据. 此软件代码可用于工业控制.
💻 C
📖 第 1 页 / 共 2 页
字号:
                  dwSize = sizeof( ach );
                  if( RegQueryValueEx( hTcpipKey,
                                   "DefaultGateway",
                                   NULL,
                                   &dwType,
                                   ach,
                                   &dwSize ) == ERROR_SUCCESS )
                  {
                     /*
                     ** If the default gateway string is empty, check
                     ** the dhcp default gateway
                     */

                     if( ach[ 0 ] == 0 )
                     {
                        dwType = REG_BINARY;
                        dwSize = sizeof( ach );
                        RegQueryValueEx( hTcpipKey,
                                         "DhcpDefaultGateway",
                                         NULL,
                                         &dwType,
                                         ach,
                                         &dwSize );
                     }

                     bDone = TRUE;
                  }

                  RegCloseKey( hTcpipKey );
               }
            }

            RegCloseKey( hNetCardKey );
         }
         else
         {
            ach[ 0 ] = 0;
            bDone = TRUE;
         }

      }

      RegCloseKey( hKey );
   }

   lAddr = inet_addr( ach );
   return lAddr;
}

/*---------------------------------------------------------------------------
** eiu_FetchRegistryString()
**---------------------------------------------------------------------------
*/
void eiu_FetchRegistryString( const char *pachValue, char *pachDest, UINT16 iDestSize )
{
   HKEY   hKey;
   DWORD  dwType;
   UINT8 *pach;
   DWORD  dwSize;

   pachDest[ 0 ] = 0;

   /*
   ** For windows NT 4.0, several interesing values are stored under the following
   ** key.  This function will read the passed in value into the passed in destination
   ** buffer.
   */

   if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                    "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
                    0,
                    KEY_READ,
                    &hKey ) == ERROR_SUCCESS )
   {
      /*
      ** Attempt to read the value string
      */

      dwType = REG_SZ;
      dwSize = iDestSize;
      RegQueryValueEx( hKey,
                       pachValue,
                       NULL,
                       &dwType,
                       pachDest,
                       &dwSize );

      /*
      ** If string not there, then check the corresponding DHCP string
      */

      if( pachDest[ 0 ] == 0 )
      {
         /*
         ** Allocate memory for a dhcp string, and create the Dhcp string to
         ** query the Dhcp registry value
         */

         pach = malloc( strlen( pachValue ) + 1 + strlen( "Dhcp" ) );
         if( pach )
         {
            /*
            ** Prepend "Dhcp" to the passed in value
            */

            strcpy( pach, "Dhcp" );
            strcat( pach, pachValue );

            dwSize = iDestSize;
            RegQueryValueEx( hKey,
                             pach,
                             NULL,
                             &dwType,
                             pachDest,
                             &dwSize );

            free( pach );
         }
      }

      RegCloseKey( hKey );
   }
}

/*---------------------------------------------------------------------------
** eiu_GetCurrentNetworkConfig()
**---------------------------------------------------------------------------
*/

void eiu_GetCurrentNetworkConfig( void )
{
   /*
   ** Get the current network configuration from non-volatile storage
   ** and store it in the ei_s.sInterfaceConfig structure.
   */

   /*********************************************************/
   /*** PORTING ALERT!                                    ***/
   /*** ADD APPLICATION SPECIFIC CODE HERE                ***/
   /*********************************************************/

   /*
   ** Update the private TCP/IP interface object data "ei_s".
   */

   UINT8 ach[ 256 ];
   struct in_addr sIPAddr;
   struct in_addr sSubnet;

   /*
   ** Note that the following is example code, and works on a PC which
   ** only has a single TCP/IP interface.  If your code needs to run on
   ** a machine with multiple TCP/IP interfaces, changes to this code
   ** are required.
   */

   if( en_cd_IFGetConfig( &sIPAddr, &sSubnet, NULL ) == OE_ERROR )
   {
      GS_LogEvent( FAILURE, WSAGetLastError( ), NULL, FATAL );
   }

   ei_s.sInterfaceConfig.lIpAddr         = htonl( sIPAddr.s_addr );
   ei_s.sInterfaceConfig.lSubnetMask     = htonl( sSubnet.s_addr );

   /*
   ** Note that the following works for NT 4.0, may not work for other flavors of
   ** windows.
   */

   eiu_FetchRegistryString( "Domain", ach, sizeof( ach ) );
   ei_s.sInterfaceConfig.iDomainNameSize = UC_iTOiLe( min( EI_MAX_DOMAIN_LENGTH, strlen( ach ) ) );

   UC_CopyMem( &ei_s.sInterfaceConfig.achDomainName[0],
               ach,
               ei_s.sInterfaceConfig.iDomainNameSize );

   eiu_FetchRegistryString( "NameServer", ach , sizeof( ach ) );
   ei_s.sInterfaceConfig.lNameServer     = htonl( inet_addr( ach ) );
   if( strchr( ach, ' ' ) )
   {
      ei_s.sInterfaceConfig.lNameServer_2= htonl( inet_addr( strchr( ach, ' ' ) + 1 ) );
   }
   else
   {
      ei_s.sInterfaceConfig.lNameServer_2   = 0;
   }

   ei_s.sInterfaceConfig.lGatewayAddr    = htonl( eiu_FetchGatewayAddr( ) );

   gethostname( ach, sizeof( ach ) );

   ei_s.iHostNameSize = UC_iTOiLe( min( EI_MAX_HOST_LENGTH, strlen( ach ) ) );

   UC_CopyMem( &ei_s.achHostName[0],
               ach,
               ei_s.iHostNameSize );

   /*
   ** Mask off the Configuration bits, and set as indicated.
   */

   ei_s.lConfigControl &= ~EI_CONTROL_CONFIG_BITMASK;
   if( eiu_FetchDhcpState( ) )
   {
      ei_s.lConfigControl |= EI_CONTROL_DHCP;
   }
   else
   {
      ei_s.lConfigControl |= EI_CONTROL_USE_STORED_CONFIG;
   }

   /*
   ** Set the InterfaceStatus based on if a valid IP address has been set
   */

   if( ei_s.sInterfaceConfig.lIpAddr )
   {
      ei_s.lInterfaceStatus = EI_STATUS_VALID_CONFIGURATION;
   }
   else
   {
      ei_s.lInterfaceStatus = EI_STATUS_NOT_CONFIGURED;
   }

} /* end of eiu_GetCurrentNetworkConfig() */

#if ( EIU_CONFIG_CAPABILITY & EI_CAPABILITY_SETTABLE_CONFIG )
/*---------------------------------------------------------------------------
** eiu_SetCurrentNetworkConfig()
**---------------------------------------------------------------------------
*/

UINT16 eiu_SetCurrentNetworkConfig( void )
{
   /*
   ** If your device has the ability to set the EthernetIP config,
   ** then this function must take the values in ei_s.sInterfaceConfig,
   ** and save them to non-volatile storage.
   */

   /*********************************************************/
   /*** PORTING ALERT!                                    ***/
   /*** ADD APPLICATION SPECIFIC CODE HERE                ***/
   /*********************************************************/


	#error( "EI_CAPABILITY_SETTABLE_CONFIG set, must write this function." )
	

   UINT16 iStatus;

   return( iStatus );

} /* end of eiu_SetCurrentNetworkConfig() */
#endif

#endif  /* #ifdef TCPIP_INTERFACE_OBJECT */

/****************************************************************************
**
** End of EIU_OBJ.C
**
*****************************************************************************
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -