home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cnetrsrc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  5.1 KB  |  255 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. /*
  18. ** CNetResourceInformation stuff
  19. */
  20.  
  21. IMPLEMENT_SERIAL( CNetworkResourceInformation, CObject, 1 )
  22.  
  23. CNetworkResourceInformation::CNetworkResourceInformation()
  24. {
  25.    m_Initialize();
  26. }
  27.  
  28. CNetworkResourceInformation::~CNetworkResourceInformation()
  29. {
  30.    m_Initialize();
  31. }
  32.  
  33. void CNetworkResourceInformation::Copy( const NETRESOURCE *source )
  34. {
  35.    ASSERT( source != NULL );
  36.  
  37.    if ( source == NULL )
  38.    {
  39.       m_Initialize();
  40.       return;
  41.    }
  42.  
  43.    if ( source->lpLocalName != (LPTSTR) NULL )
  44.    {
  45.       LocalName = source->lpLocalName;
  46.    }
  47.    else
  48.    {
  49.       LocalName.Empty();
  50.    }
  51.  
  52.    if ( source->lpRemoteName != (LPTSTR) NULL )
  53.    {
  54.       RemoteName = source->lpRemoteName;
  55.    }
  56.    else
  57.    {
  58.       RemoteName.Empty();
  59.    }
  60.  
  61.    if ( source->lpComment != (LPTSTR) NULL )
  62.    {
  63.       Comment = source->lpComment;
  64.    }
  65.    else
  66.    {
  67.       Comment.Empty();
  68.    }
  69.  
  70.    if ( source->lpProvider != (LPTSTR) NULL )
  71.    {
  72.       Provider = source->lpProvider;
  73.    }                               
  74.    else
  75.    {
  76.       Provider.Empty();
  77.    }
  78.  
  79.    Scope       = source->dwScope;
  80.    Type        = source->dwType;
  81.    DisplayType = source->dwDisplayType;
  82.    Usage       = source->dwUsage;
  83. }
  84.  
  85. void CNetworkResourceInformation::Empty( void )
  86. {
  87.    m_Initialize();
  88. }
  89.  
  90. void CNetworkResourceInformation::m_Initialize( void )
  91. {
  92.    LocalName.Empty();
  93.    RemoteName.Empty();
  94.    Comment.Empty();
  95.    Provider.Empty();
  96.    Scope       = 0;
  97.    Type        = 0;
  98.    DisplayType = 0;
  99.    Usage       = 0;
  100. }
  101.  
  102. void CNetworkResourceInformation::Serialize( CArchive& archive )
  103. {
  104.    CObject::Serialize( archive );
  105.  
  106.    if ( archive.IsStoring() )
  107.    {
  108.       archive << LocalName;
  109.       archive << RemoteName;
  110.       archive << Comment;
  111.       archive << Provider;
  112.       archive << Scope;
  113.       archive << Type;
  114.       archive << DisplayType;
  115.       archive << Usage;
  116.    }
  117.    else
  118.    {
  119.       archive >> LocalName;
  120.       archive >> RemoteName;
  121.       archive >> Comment;
  122.       archive >> Provider;
  123.       archive >> Scope;
  124.       archive >> Type;
  125.       archive >> DisplayType;
  126.       archive >> Usage;
  127.    }
  128. }
  129.  
  130. /*
  131. ** CNetworkResources Stuff
  132. */
  133.  
  134. IMPLEMENT_DYNAMIC( CNetworkResources, CNetwork )
  135.  
  136. CNetworkResources::CNetworkResources()
  137. {
  138.    m_Initialize();
  139. }
  140.  
  141. CNetworkResources::~CNetworkResources()
  142. {
  143.    if ( m_ResumeHandle != NULL )
  144.    {
  145.       ::WNetCloseEnum( m_ResumeHandle );
  146.       m_ResumeHandle = NULL;
  147.    }
  148.  
  149.    m_Initialize();
  150. }
  151.  
  152. void CNetworkResources::m_Initialize( void )
  153. {
  154.    m_ErrorCode    = 0;
  155.    m_ResumeHandle = (HANDLE) NULL;
  156.    ::ZeroMemory( &m_NetResource, sizeof( m_NetResource ) );
  157. }
  158.  
  159. BOOL CNetworkResources::Enumerate( CNetworkResourceInformation& information )
  160. {
  161.    if ( m_ResumeHandle != NULL )
  162.    {
  163.       ::WNetCloseEnum( m_ResumeHandle );
  164.       m_ResumeHandle = NULL;
  165.    }
  166.  
  167.    ::ZeroMemory( &m_NetResource, sizeof( m_NetResource ) );
  168.    m_NetResource.dwUsage = usageContainer;
  169.  
  170.    NETRESOURCE *net_resource_parameter = (NETRESOURCE *) NULL;
  171.  
  172.    /*
  173.    ** Let's see what we want to enumerate
  174.    */
  175.  
  176.    switch( information.Scope )
  177.    {
  178.       case scopeConnected: // information.Usage is ignored
  179.  
  180.          break;
  181.             
  182.       case scopePersistent:  // information.Usage is ignored
  183.  
  184.          break;
  185.  
  186.       case scopeAll:
  187.  
  188.          break;
  189.  
  190.       default:
  191.  
  192.          m_ErrorCode = ERROR_INVALID_PARAMETER;
  193.          return( FALSE );
  194.    }
  195.  
  196.    switch( information.Usage )
  197.    {
  198.       case usageAll:
  199.  
  200.          net_resource_parameter = (NETRESOURCE *) NULL;
  201.          break;
  202.  
  203.       case usageConnectable:
  204.       case usageContainer:
  205.  
  206.          net_resource_parameter = &m_NetResource;
  207.          break;
  208.  
  209.       default:
  210.  
  211.          m_ErrorCode = ERROR_INVALID_PARAMETER;
  212.          return( FALSE );
  213.    }
  214.  
  215.    m_ErrorCode = ::WNetOpenEnum( information.Scope, 
  216.                                  information.Type, 
  217.                                  information.Usage, 
  218.                                  net_resource_parameter,
  219.                                 &m_ResumeHandle );
  220.  
  221.    if ( m_ErrorCode == NO_ERROR )
  222.    {
  223.       return( TRUE );
  224.    }
  225.    else if ( m_ErrorCode == ERROR_EXTENDED_ERROR )
  226.    {
  227.       m_ErrorCode = ::GetLastError();
  228.    }
  229.  
  230.    return( FALSE );
  231. }
  232.  
  233. BOOL CNetworkResources::GetNext( CNetworkResourceInformation& information )
  234. {
  235.    DWORD number_of_entries = 1;
  236.    DWORD size_of_buffer    = sizeof( m_NetResource );
  237.  
  238.    m_ErrorCode = ::WNetEnumResource( m_ResumeHandle,
  239.                                     &number_of_entries,
  240.                                     &m_NetResource,
  241.                                     &size_of_buffer );
  242.  
  243.    if ( m_ErrorCode == NO_ERROR )
  244.    {
  245.       information.Copy( &m_NetResource );
  246.       return( TRUE );
  247.    }
  248.    else if ( m_ErrorCode == ERROR_EXTENDED_ERROR )
  249.    {
  250.       m_ErrorCode = ::GetLastError();
  251.    }
  252.                                         
  253.    return( FALSE );
  254. }
  255.