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

📄 eiu_obj.c

📁 基于EthernetIP协议的应用程序,可以读取AB公司Controllogix系列Ethernetip协议PLC数据. 此软件代码可用于工业控制.
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*****************************************************************************
**
** File Name
** ---------
**
** EIU_OBJ.C
**
*****************************************************************************
*****************************************************************************
**
** Description
** -----------
**
** User provided routines for the TCP/IP Interface Object.
**
*****************************************************************************
*****************************************************************************
**
** Source Change Indices
** ---------------------
**
** Porting: <none>----4<major>         Customization: <none>----4<major>
**
*****************************************************************************
*****************************************************************************
**                                                                         **
** ETHERNET/IP EXAMPLE CODE                                                **
** COPYRIGHT (c) 2000-2005 ODVA (Open DeviceNet Vendor Association)		   **
**						   & ControlNet International Ltd.                 **
**                                                                         **
** All rights reserved, except as specifically licensed in writing.        **
** Use of the Ethernet/IP Example Protocol Software is subject to          **
** ODVA's and ControlNet International's Terms of Use Agreement.           **
** The following work constitutes example program code and is intended     **
** merely to illustrate useful programming techniques.  The user is        **
** responsible for applying the code correctly.  The code is provided      **
** AS IS without warranty and is in no way guaranteed to be error-free.    **
**                                                                         **
*****************************************************************************
*****************************************************************************
*/

/****************************************************************************
*****************************************************************************
**
** Change Log
** ----------
**
**
*****************************************************************************
*****************************************************************************
*/

/*
** See if this object is to be included.
*/

#include "ab.h"

#ifdef TCPIP_INTERFACE_OBJECT

#include "cd.h"         /* Communications Device public interfaces         */
#include "mr.h"         /* Message Router public interfaces                */
#include "ei.h"         /* TCP/IP Interface Object public interfaces       */
#include "ei_obj.h"     /* TCP/IP Interface Object private interfaces      */

/****************************************************************************
**
** Public Globals
**
*****************************************************************************
*/


/****************************************************************************
**
** Private Services
**
*****************************************************************************
*/

/*---------------------------------------------------------------------------
** eiu_FetchDhcpState()
**---------------------------------------------------------------------------
*/

BOOL eiu_FetchDhcpState( void )
{
   HKEY   hKey;
   HKEY   hNetCardKey;
   HKEY   hTcpipKey;
   UINT8  ach[ 256 ];
   DWORD  dwSize;
   DWORD  dwType;
   BOOL   bDone;
   UINT8  i;
   DWORD  dwDhcp = 0;

   /*
   ** For Windows NT 4.0, the DHCP enable/disable flag is stored in the
   ** registry under the TCP/IP adapter.  First, go to the network cards
   ** key to enumerate the adapters.
   */

   if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                     "Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",
                     0,
                     KEY_READ,
                     &hKey ) == ERROR_SUCCESS )
   {
      i = 0;

      /*
      ** Continue enumerating the network cards, until we find one which has
      ** a TCP/IP port.
      */

      bDone = FALSE;
      while( !bDone )
      {
         i++;
         sprintf( ach, "%d", i );
         /*
         ** Open the network card key under services
         */

         if( RegOpenKeyEx( hKey,
                           ach,
                           0,
                           KEY_READ,
                           &hNetCardKey ) == ERROR_SUCCESS )
         {
            dwType = REG_SZ;
            strcpy( ach, "System\\CurrentControlSet\\Services\\" );
            dwSize = sizeof( ach ) - strlen( ach );
            if( RegQueryValueEx( hNetCardKey,
                                 "ServiceName",
                                 NULL,
                                 &dwType,
                                 &ach[ strlen( ach ) ],
                                 &dwSize ) == ERROR_SUCCESS )
            {
               /*
               ** See if there is a TCP/IP key under the service
               */

               strcat( ach, "\\Parameters\\Tcpip" );
               if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                                 ach,
                                 0,
                                 KEY_READ,
                                 &hTcpipKey ) == ERROR_SUCCESS )
               {
                  /*
                  ** Found a TCP/IP key, read the DHCP enable value
                  */

                  dwType = REG_DWORD;
                  dwSize = sizeof( dwDhcp );
                  if( RegQueryValueEx( hTcpipKey,
                                       "EnableDHCP",
                                       NULL,
                                       &dwType,
                                       ( unsigned char * ) &dwDhcp,
                                       &dwSize ) == ERROR_SUCCESS )
                  {
                     bDone = TRUE;
                  }

                  RegCloseKey( hTcpipKey );
               }
            }

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

      }

      RegCloseKey( hKey );
   }

   return( dwDhcp );
}

/*---------------------------------------------------------------------------
** eiu_FetchGatewayAddr()
**---------------------------------------------------------------------------
*/

UINT32 eiu_FetchGatewayAddr( void )
{
   HKEY   hKey;
   HKEY   hNetCardKey;
   HKEY   hTcpipKey;
   UINT32 lAddr = 0;
   UINT8  ach[ 256 ];
   DWORD  dwSize;
   DWORD  dwType;
   BOOL   bDone;
   UINT8  i;

   /*
   ** For Windows NT 4.0, the default gateway address is stored in the
   ** registry under the TCP/IP adapter.  First, go to the network cards
   ** key to enumerate the adapters.
   */

   if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                     "Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",
                     0,
                     KEY_READ,
                     &hKey ) == ERROR_SUCCESS )
   {
      i = 0;

      /*
      ** Continue enumerating the network cards, until we find one which has
      ** a TCP/IP port.
      */

      bDone = FALSE;
      while( !bDone )
      {
         i++;
         sprintf( ach, "%d", i );
         /*
         ** Open the network card key under services
         */

         if( RegOpenKeyEx( hKey,
                           ach,
                           0,
                           KEY_READ,
                           &hNetCardKey ) == ERROR_SUCCESS )
         {
            dwType = REG_SZ;
            strcpy( ach, "System\\CurrentControlSet\\Services\\" );
            dwSize = sizeof( ach ) - strlen( ach );
            if( RegQueryValueEx( hNetCardKey,
                                "ServiceName",
                                NULL,
                                &dwType,
                                &ach[ strlen( ach ) ],
                                &dwSize ) == ERROR_SUCCESS )
            {
               /*
               ** See if there is a TCP/IP key under the service
               */

               strcat( ach, "\\Parameters\\Tcpip" );
               if( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                                 ach,
                                 0,
                                 KEY_READ,
                                 &hTcpipKey ) == ERROR_SUCCESS )
               {
                  /*
                  ** Found a TCP/IP key, read the default gateway
                  */

                  dwType = REG_BINARY;

⌨️ 快捷键说明

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