转贴一些程序:
#if !defined(_NetService_h__)
#define _NetService_h__//////////////////////////////////////////////////////////////////////////////////#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000//////////////////////////////////////////////////////////////////////////////////
//
// CNetService Class
//
//////////////////////////////////////////////////////////////////////////////////
//
// Written by Jay Wheeler
// EarthWalk Software
//
// Version 1 - October, 2000.
//
//////////////////////////////////////////////////////////////////////////////////
//
// CNetService is copyright ?2000. EarthWalk Software.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////////#include <winnetwk.h>//////////////////////////////////////////////////////////////////////////////////
//
// NOTE: Project must also include the following libraries:
// mpr.lib
//
//////////////////////////////////////////////////////////////////////////////////typedef struct _OSVERSTRUCT
{
union
{
OSVERSIONINFOEX OSVerX;
OSVERSIONINFO OSVer;
};
} OSVERSTRUCT;
//////////////////////////////////////////////////////////////////////////////////#define MAX_ENUM_BUFFER 16000//////////////////////////////////////////////////////////////////////////////////
//
// NetService Error Codes
//
//////////////////////////////////////////////////////////////////////////////////#define NETSERVICE_NOERROR 0
#define NETSERVICE_OPEN_ENUM NETSERVICE_NOERROR + 1
#define NETSERVICE_ENUM_RESOURCE NETSERVICE_OPEN_ENUM + 1
#define NETSERVICE_ADDCONN_ERROR NETSERVICE_ENUM_RESOURCE + 1
#define NETSERVICE_GET_OSVER NETSERVICE_ADDCONN_ERROR + 1
#define NETSERVICE_GETUSER NETSERVICE_GET_OSVER + 1
#define NETSERVICE_CANCELCONNECT NETSERVICE_GETUSER + 1
#define NETSERVICE_GETUNC_INFO NETSERVICE_CANCELCONNECT + 1
#define NETSERVICE_GETREMOTE_INFO NETSERVICE_GETUNC_INFO + 1
#define NETSERVICE_GETPROVIDER NETSERVICE_GETREMOTE_INFO + 1
#define NETSERVICE_CONN_DLG NETSERVICE_GETPROVIDER + 1
#define NETSERVICE_DISCONN_DLG NETSERVICE_CONN_DLG + 1
#define NETSERVICE_MEM_ALLOC NETSERVICE_DISCONN_DLG + 1
#define NETSERVICE_HEAP_ALLOC NETSERVICE_MEM_ALLOC + 1
#define NETSERVICE_NETWORKINFO NETSERVICE_HEAP_ALLOC + 1//////////////////////////////////////////////////////////////////////////////////
//
// Windows OS Types
//
//////////////////////////////////////////////////////////////////////////////////#define WIN_UNKNOWN 0
#define WIN32_S WIN_UNKNOWN + 1
#define WIN32_NT_PRO WIN32_S + 1
#define WIN32_NT_SERVER WIN32_NT_PRO + 1
#define WIN32_2000_PRO WIN32_NT_SERVER + 1
#define WIN32_2000_SERVER WIN32_2000_PRO + 1
#define WIN32_WINDOWS_95 WIN32_2000_SERVER + 1
#define WIN32_WINDOWS_98 WIN32_WINDOWS_95 + 1//////////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////////
typedef struct _UNCInfo
{
union
{
UNIVERSAL_NAME_INFO UNC_NameInfo;
REMOTE_NAME_INFO UNC_RemoteInfo;
} Info;
} UNCInfo;#define MaxUNCInfo 4000//////////////////////////////////////////////////////////////////////////////////
//
// CNetService Class
//
//////////////////////////////////////////////////////////////////////////////////
class CNetService
{public:
CNetService(DWORD p_MemSize=MAX_ENUM_BUFFER);
virtual ~CNetService(); //////////////////////////////////////////////////////////////////////////////public:
virtual bool OpenEnum(DWORD p_Scope,
DWORD p_Type,
DWORD p_Usage,
NETRESOURCE * p_pNetResource=NULL);
virtual void CloseEnum(void); virtual bool EnumerateResource(DWORD p_Count);
virtual NETRESOURCE * EnumerateNext(void); virtual bool GetUNCInfo(CString p_LocalPath);
CString UniversalName(void); CString UNCRemoteServer(void);
CString UNCRemotePath(void); CString GetProviderName(DWORD p_NetType); virtual bool AddConnection(NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent=0);
virtual bool AddConnection(HWND p_hWnd,
NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent); virtual bool NetConnect(DWORD p_NetType,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent);
virtual bool NetConnect(HWND p_hWnd,
DWORD p_NetType,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent); virtual bool NetDisconnect(CString p_Local,
DWORD p_DisconnectType,
bool p_ForceDisconnect); CString NetUser(CString p_Remote); int OSVersion(void); long Result(void);
bool OpenState(void); DWORD Error(void);
CString ErrorMessage(DWORD p_Error); DWORD ServiceError(void);
CString ServiceErrorMessage(DWORD p_Error); DWORD NetError(void);
CString NetErrorMessage(void);
CString NetErrorProvider(void); DWORD ResourceDisplayType(void); bool NetworkInformation(CString p_ProviderName);
bool NetworkInformation(CString p_ProviderName,
NETINFOSTRUCT * p_pNetInfoStruct); NETINFOSTRUCT * GetNetInfo(void);
DWORD CNetService::GetNetStatus(void);
WORD CNetService::GetNetType(void);
DWORD CNetService::GetNetInfoPrinters(void);
DWORD CNetService::GetNetInfoDrives(void);protected:
bool GetNetError(int p_ServiceError);
private:
LPVOID GetMem(DWORD p_MemSize);
void FreeMem(LPVOID p_pMem); //////////////////////////////////////////////////////////////////////////////
public:protected:
long g_Result;
bool g_Open;
DWORD g_Error; int g_ServiceError;
DWORD g_Count; DWORD g_NetError;
CString g_NetErrorMessage;
CString g_NetErrorProvider; CString g_UserName;
CString g_ProviderName; HANDLE g_hEnum;
HANDLE g_hMem;
DWORD g_MemSize;
DWORD g_EnumMemSize; NETRESOURCE * g_pNetResource;
NETINFOSTRUCT g_NetInfoStruct; CString g_UniversalName;
CString g_RemainingPath;
CString g_ConnectionName; int g_WindowsType; DWORD g_EnumIndex;
DWORD g_EnumBufferSize;
char * g_pEnumBuffer;private:
};//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////#endif
#include "stdafx.h"
#include "NetService.h"//////////////////////////////////////////////////////////////////////////////////#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif//////////////////////////////////////////////////////////////////////////////////
//
// CNetService Class
//
//////////////////////////////////////////////////////////////////////////////////
//
// Written by Jay Wheeler
// EarthWalk Software
//
// Version 1 - October, 2000.
//
//////////////////////////////////////////////////////////////////////////////////
//
// CNetService is copyright ?2000. EarthWalk Software.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.,
// or download a copy of the license from the Free Software Foundation at
// http://www.fsf.org/
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
//////////////////////////////////////////////////////////
CNetService::CNetService(DWORD p_MemSize)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_Open = false; g_MemSize = p_MemSize + 4096;
g_hMem = NULL;
g_ServiceError = NETSERVICE_NOERROR;
g_hMem = HeapCreate(0,
g_MemSize,
0);
if (g_hMem == NULL)
{
g_Error = GetLastError();
g_ServiceError = NETSERVICE_HEAP_ALLOC;
return;
} g_pEnumBuffer = (char *)GetMem(p_MemSize);
g_EnumMemSize = p_MemSize;
g_pNetResource = (NETRESOURCE *)g_pEnumBuffer;}//////////////////////////////////////////////////////////
//
// Destructor
//
//////////////////////////////////////////////////////////
//
// Extend this method with destructor for
// extended class. The code here will execute
// prior to the new class destructor
//
//////////////////////////////////////////////////////////
CNetService::~CNetService()
{
if (g_Open)
CloseEnum();
if (g_hMem != NULL)
{
if (g_pEnumBuffer != NULL)
FreeMem(g_pEnumBuffer);
HeapDestroy(g_hMem);
}
}//////////////////////////////////////////////////////////
//
// FreeMem
//
//////////////////////////////////////////////////////////
//
// Return Value:
// None.
// Parameters:
// p_pMem
// pointer to memory to deallocate
// Res
// Deallocates memory allocated by GetMem.
//
//////////////////////////////////////////////////////////
void CNetService::FreeMem(void * p_pMem)
{
HeapFree(g_hMem,
0,
p_pMem);
p_pMem = NULL;
}//////////////////////////////////////////////////////////
//
// GetMem
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Pointer to allocated memory or NULL if error.
// Parameters:
// p_MemSize
// Size of memory, in bytes, to allocate.
// Res
// GetMem calls HeapAlloc to allocate memory on
// the heap pointed to by g_hMem.
//
//////////////////////////////////////////////////////////
LPVOID CNetService::GetMem(DWORD p_MemSize)
{
LPVOID l_pMem; l_pMem = HeapAlloc(g_hMem,
HEAP_ZERO_MEMORY,
p_MemSize);
if (l_pMem == NULL)
{
g_Error += 1;
g_ServiceError = NETSERVICE_MEM_ALLOC;
return NULL;
}
return l_pMem;
}//////////////////////////////////////////////////////////
//
// OpenEnum
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false.
// Parameters:
// p_Scope
// Specifies the scope of the enumeration:
// RESOURCE_CONNECTED
// RESOURCE_CONTEXT
// RESOURCE_GLOBALNET
// RESOURCE_REMEMBERED
// p_Type
// Specifies the resource types to enumerate:
// RESOURCETYPE_ANY
// RESOURCETYPE_DISK
// RESOURCETYPE_PRINT
// p_Usage
// If 0 = ALL resources
// Specifies the resource usage type to enumerate:
// 0 = ALL resources
// RESOURCEUSAGE_CONNECTABLE
// RESOURCEUSAGE_CONTAINER
// RESOURCEUSAGE_ATTACHED
// RESOURCEUSAGE_ALL
// p_pNetResource
// Pointer to an optional NETRESOURCE structure to
// apply the open to.
// Res
// Refer to WNetOpenEnum for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
bool CNetService::OpenEnum(DWORD p_Scope,
DWORD p_Type,
DWORD p_Usage,
NETRESOURCE * p_pNetResource)
{
g_pNetResource = p_pNetResource;
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_Open = false; g_Result = WNetOpenEnum(p_Scope,
p_Type,
p_Usage,
g_pNetResource,
&g_hEnum);
if (g_Result != NO_ERROR)
return GetNetError(NETSERVICE_OPEN_ENUM);
g_Open = true;
return true;
}//////////////////////////////////////////////////////////
//
// CloseEnum
//
//////////////////////////////////////////////////////////
//
// Return Value:
// None
// Parameters:
// None
// Res
// Closes the current enumeration handle.
//
// Refer to WNetCloseEnum for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
void CNetService::CloseEnum()
{
if (! g_Open)
return;
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_Open = false; g_Result = WNetCloseEnum(g_hEnum);
}//////////////////////////////////////////////////////////
//
// EnumerateResource
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false.
// Parameters:
// p_Count
// The number of enumerations to fetch to
// the default EnumBuffer.
//
// Set to -1 to retrieve as many as will
// fit in EnumBuffer.
// Res
// Fetches another resource enumeration buffer to
// continue an enumeration of network resources
// that was started by a call to OpenEnum.
//
// Refer to WNetEnumResource for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
bool CNetService::EnumerateResource(DWORD p_Count)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0; g_Count = p_Count;
g_EnumBufferSize = MAX_ENUM_BUFFER; g_Result = WNetEnumResource(g_hEnum,
&g_Count,
g_pEnumBuffer,
&g_EnumBufferSize);
if (g_Result != NO_ERROR)
{
if (g_Result == ERROR_NO_MORE_ITEMS)
return false;
return GetNetError(NETSERVICE_ENUM_RESOURCE);
}
g_pNetResource = (NETRESOURCE *)g_pEnumBuffer;
g_EnumIndex = 0; return true;
}//////////////////////////////////////////////////////////
//
// EnumerateNext
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Pointer to the next NETRESOURCE structure
// in the enumeration buffer, or NULL if at
// the end of the buffer (not necessarily
// the end of the enumeration).
// Parameters:
// None
// Res
// Returns a pointer to the next NETRESOURCE
// structure in the resource enumeration buffer.
//
// Refer to WNetEnumResource for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
NETRESOURCE * CNetService::EnumerateNext()
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0; if (g_EnumIndex >= g_Count)
return NULL;
NETRESOURCE * l_pNetResource = &g_pNetResource[g_EnumIndex];
g_EnumIndex++;
return l_pNetResource;
}//////////////////////////////////////////////////////////
//
// GetUNC
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful
// Parameters:
// p_LocalPath
// Path to a local device to query.
// p_InfoLevel
// Type of information to retrieve:
// UNIVERSAL_NAME_INFO_LEVEL
// REMOTE_NAME_INFO_LEVEL
// Res
// Retrieves UNC (Universal Naming Convention)
// information representing the specified network
// connection.
//
// The following Methods expose the contents
// returned by this Method:
//
// UniversalName
// UNCRemoteServer
// UNCRemotePath
//
//////////////////////////////////////////////////////////
bool CNetService::GetUNCInfo(CString p_LocalPath)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
UNCInfo * l_pUNC = (UNCInfo *)GetMem(MaxUNCInfo);
if (l_pUNC == NULL)
return false; DWORD l_InfoSize = MaxUNCInfo;
if ((g_Result = WNetGetUniversalName(p_LocalPath,
UNIVERSAL_NAME_INFO_LEVEL,
l_pUNC,
&l_InfoSize)) != NO_ERROR)
{
GetNetError(NETSERVICE_GETUNC_INFO);
FreeMem(l_pUNC);
return false;
} g_UniversalName.Format("%s", l_pUNC->Info.UNC_NameInfo.lpUniversalName); l_InfoSize = MaxUNCInfo;
if ((g_Result = WNetGetUniversalName(p_LocalPath,
REMOTE_NAME_INFO_LEVEL,
l_pUNC,
&l_InfoSize)) != NO_ERROR)
{
GetNetError(NETSERVICE_GETREMOTE_INFO);
FreeMem(l_pUNC);
return false;
} g_ConnectionName.Format("%s", l_pUNC->Info.UNC_RemoteInfo.lpConnectionName);
g_UniversalName.Format("%s", l_pUNC->Info.UNC_RemoteInfo.lpUniversalName);
g_RemainingPath.Format("%s", l_pUNC->Info.UNC_RemoteInfo.lpRemainingPath); FreeMem(l_pUNC);
return true;
}//////////////////////////////////////////////////////////
//
// UNCRemoteServer
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Return the connection name from the last
// GetUNC call.
// Parameters:
// None.
// Res
// No test is made to ensure the UNC buffer
// exists or is valid.
//
//////////////////////////////////////////////////////////
CString CNetService::UNCRemoteServer()
{
return g_ConnectionName;
}//////////////////////////////////////////////////////////
//
// UNCRemotePath
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Return the remote share path from the last
// GetUNC call.
// Parameters:
// None.
// Res
// No test is made to ensure the UNC buffer
// exists or is valid.
//
//////////////////////////////////////////////////////////
CString CNetService::UNCRemotePath()
{
return g_RemainingPath;
}//////////////////////////////////////////////////////////
//
// UniversalName
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Return the UNC for the connection.
// Parameters:
// None.
// Res
// No test is made to ensure the UNC buffer
// exists or is valid.
//
//////////////////////////////////////////////////////////
CString CNetService::UniversalName()
{
return g_UniversalName;
}//////////////////////////////////////////////////////////
//
// GetProviderName
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A string containing the provider name associated
// with a specified network type.
// Parameters:
// p_NetType
// Type of network (e.g. Novell, Banyan, etc.)
// to query
// Res
// The network transport provider name is returned.
//
//////////////////////////////////////////////////////////
CString CNetService::GetProviderName(DWORD p_NetType)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_ProviderName.Empty(); DWORD l_BufferSize = 4096;
char * l_pProviderName = (char *)GetMem(l_BufferSize); if (l_pProviderName == NULL)
return g_ProviderName;
if ((g_Result = WNetGetProviderName(p_NetType,
l_pProviderName,
&l_BufferSize)) != NO_ERROR)
GetNetError(NETSERVICE_GETPROVIDER);
else
g_ProviderName.Format("%s", l_pProviderName);
FreeMem(l_pProviderName); return g_ProviderName;
}//////////////////////////////////////////////////////////
//
// NetworkInformation
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful
// Parameters:
// p_ProviderName
// Name of a network provider (as returned from
// GetProvider).
// Res
// This method fetches a NETWORKINFOSTRUC structure
// containing information about the requested
// provider. This information may be retrieved
// using one of the NetInfoxxxx methods.
//
//////////////////////////////////////////////////////////
bool CNetService::NetworkInformation(CString p_ProviderName)
{
return NetworkInformation(p_ProviderName,
&g_NetInfoStruct);
}//////////////////////////////////////////////////////////
//
// NetworkInformation
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful
// Parameters:
// p_ProviderName
// Name of a network provider (as returned from
// GetProvider).
// p_pNetInfoStruct
// Pointer to a NETINFOSTRUCT to use in the query.
// Res
// This method fetches a NETWORKINFOSTRUC structure
// containing information about the requested
// provider. A copy of the returned NETINFOSTRUCT
// is not saved locally.
//
//////////////////////////////////////////////////////////
bool CNetService::NetworkInformation(CString p_ProviderName,
NETINFOSTRUCT * p_pNetInfoStruct)
{
ZeroMemory(p_pNetInfoStruct, sizeof NETINFOSTRUCT);
p_pNetInfoStruct->cbStructure = sizeof NETINFOSTRUCT; if (p_ProviderName.IsEmpty())
return false;
if ((g_Result = WNetGetNetworkInformation(p_ProviderName,
p_pNetInfoStruct)) != NO_ERROR)
{
return GetNetError(NETSERVICE_NETWORKINFO);
}
return true;
}//////////////////////////////////////////////////////////
//
// NetConnect
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_NetDevice
// Network device type requested:
// RESOURCETYPE_DISK
// RESOURCETYPE_PRINT
// p_Local
// Specifies the name of a local device
// NULL for no local device
// p_Remote
// Specifies the remote network name for new connection, or
// network name associated with the name pointed to by
// p_LocalName for current/persistent connection.
// p_NetStack
// The name of the provider that owns the resource, or
// NULL to let the system assign.
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// Creates the specified network connection.
//
//////////////////////////////////////////////////////////
bool CNetService::NetConnect(DWORD p_NetDevice,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
return NetConnect(NULL,
p_NetDevice,
p_Local,
p_Remote,
p_NetStack,
p_UserName,
p_Password,
p_Persistent);
}//////////////////////////////////////////////////////////
//
// NetConnect
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_hWnd
// Window handle for network provider prompts,
// NULL if not Window app or not required.
// p_NetDevice
// Network device type requested:
// NET_DISK
// NET_PRINTER
// p_Local
// Specifies the name of a local device
// NULL for no local device
// p_Remote
// Specifies the remote network name for new connection, or
// network name associated with the name pointed to by
// p_LocalName for current/persistent connection.
// p_NetStack
// The name of the provider that owns the resource, or
// NULL to let the system assign.
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// Creates the specified network connection.
//
// Refer to WNetAddConnection3 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::NetConnect(HWND p_hWnd,
DWORD p_NetType,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
DWORD l_BufferSize = 4096;
void * l_pEnumBuffer = GetMem(l_BufferSize);
g_pNetResource = (NETRESOURCE *)l_pEnumBuffer;
ZeroMemory(g_pNetResource, l_BufferSize); char l_Local [MAX_PATH+1];
char l_Remote [MAX_PATH + 1];
char l_Provider [MAX_PATH + 1]; ZeroMemory(l_Local, MAX_PATH + 1);
ZeroMemory(l_Remote, MAX_PATH + 1);
ZeroMemory(l_Provider, MAX_PATH + 1); if (p_Local.IsEmpty())
g_pNetResource->lpLocalName = NULL;
else
{
if (p_Local.GetLength() > MAX_PATH)
p_Local.Delete(MAX_PATH, MAX_PATH);
memcpy(l_Local, p_Local, p_Local.GetLength());
g_pNetResource->lpLocalName = l_Local;
} if (p_Remote.IsEmpty())
g_pNetResource->lpRemoteName = NULL;
else
{
if (p_Remote.GetLength() > MAX_PATH)
p_Remote.Delete(MAX_PATH, MAX_PATH);
memcpy(l_Remote, p_Remote, p_Remote.GetLength());
g_pNetResource->lpRemoteName = l_Remote;
} if (p_NetStack.IsEmpty())
g_pNetResource->lpProvider = NULL;
else
{
if (p_NetStack.GetLength() > MAX_PATH)
p_NetStack.Delete(MAX_PATH, MAX_PATH);
memcpy(l_Provider, p_NetStack, p_NetStack.GetLength());
g_pNetResource->lpProvider = l_Provider;
} g_pNetResource->dwScope = RESOURCE_GLOBALNET;
g_pNetResource->dwType = p_NetType;
g_pNetResource->dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
g_pNetResource->dwUsage = RESOURCEUSAGE_CONNECTABLE; bool l_Result = AddConnection(p_hWnd,
g_pNetResource,
p_UserName,
p_Password,
p_Persistent);
FreeMem(l_pEnumBuffer);
return l_Result;
}//////////////////////////////////////////////////////////
//
// AddConnection
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_pNetResource
// Pointer to a NETRESOURCE structure for
// this request
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// Creates the specified network connection.
//
// refer to WNetAddConnection3 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::AddConnection(NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
return AddConnection(NULL,
p_pNetResource,
p_UserName,
p_Password,
p_Persistent);
}//////////////////////////////////////////////////////////
//
// AddConnectionW
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_hWnd
// Window handle for network provider prompts,
// NULL if not Window app or not required.
// p_pNetResource
// Pointer to a NETRESOURCE structure for
// this request
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// refer to WNetAddConnection3 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::AddConnection(HWND p_hWnd,
NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
DWORD l_dwFlags = (p_Persistent) ? 0 : CONNECT_UPDATE_PROFILE; if ((g_Result = WNetAddConnection3(p_hWnd,
p_pNetResource,
p_Password,
p_UserName,
l_dwFlags)) != NO_ERROR)
return GetNetError(NETSERVICE_ADDCONN_ERROR);
return true;
}//////////////////////////////////////////////////////////
//
// NetUser
//
//////////////////////////////////////////////////////////
//
// Return Value:
// User name associated with the network device.
// Parameters:
// p_Remote
// Specifies the network name associated with
// the requested user name.
// Res
// Creates the specified network connection.
// Sets g_UserName to return value.
//
// Refer to WNetGetUser for specifics.
//
//////////////////////////////////////////////////////////
CString CNetService::NetUser(CString p_Remote)
{
DWORD l_AccountUserSize = 256;
char * l_pAccountUser = (char *)GetMem(l_AccountUserSize); g_UserName.Empty();
ZeroMemory(l_pAccountUser, l_AccountUserSize);
if ((g_Result = WNetGetUser(p_Remote,
l_pAccountUser,
&l_AccountUserSize)) != NO_ERROR)
GetNetError(NETSERVICE_GETUSER);
else
g_UserName.Format("%s", l_pAccountUser); FreeMem(l_pAccountUser); return g_UserName;
}//////////////////////////////////////////////////////////
//
// NetDisconnect
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false.
// Parameters:
// p_Local
// specifies the name of either the redirected
// local device or the remote network resource
// to disconnect from
// p_DisconnectType
// describes the connection type
// p_ForceDisconnect
// if true, disconnects even if files
// are open.
// Res
// Refer to WNetCancelConnection2 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::NetDisconnect(CString p_Local,
DWORD p_DisconnectType,
bool p_ForceDisconnect)
{
if ((g_Result = WNetCancelConnection2(p_Local,
p_DisconnectType,
p_ForceDisconnect)) != NO_ERROR)
{
return GetNetError(NETSERVICE_CANCELCONNECT);
}
return true;
}//////////////////////////////////////////////////////////
//
// ResourceDisplayType
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Resource Display Type from the most recent
// NETRESOURCE structure.
// Parameters:
// None
// Res
// Refer to NETRESOURCE structure for specifics.
//
//////////////////////////////////////////////////////////
DWORD CNetService::ResourceDisplayType()
{
return g_pNetResource->dwDisplayType;
}//////////////////////////////////////////////////////////
//
// GetNetInfo
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A pointer to the local NETINFOSTRUCT.
// Parameters:
// None.
// Res
// Returns a pointer to the local NETINFOSTRUCT
// used in a NetworkInformation method.
//
//////////////////////////////////////////////////////////
NETINFOSTRUCT * CNetService::GetNetInfo()
{
return &g_NetInfoStruct;
}//////////////////////////////////////////////////////////
//
// GetNetInfoDrives
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A DWORD bit vector indicating the valid local
// disk devices for redirection.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
DWORD CNetService::GetNetInfoDrives()
{
return g_NetInfoStruct.dwDrives;
}//////////////////////////////////////////////////////////
//
// GetNetInfoPrinters
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A DWORD bit vector indicating the valid local
// print devices for redirection.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
DWORD CNetService::GetNetInfoPrinters()
{
return g_NetInfoStruct.dwPrinters;
}//////////////////////////////////////////////////////////
//
// GetNetType
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A WORD value indicating the network type.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
WORD CNetService::GetNetType()
{
return g_NetInfoStruct.wNetType;
}//////////////////////////////////////////////////////////
//
// GetNetStatus
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A DWORD value indicating the network status:
// NO_ERROR Network is running.
// ERROR_NO_NETWORK Network is not running.
// ERROR_BUSY Network is initializing.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
DWORD CNetService::GetNetStatus()
{
return g_NetInfoStruct.dwStatus;
}//////////////////////////////////////////////////////////
//
// GetNetError
//
//////////////////////////////////////////////////////////
//
// Return Value:
// false
// Parameters:
// NetService error number to store.
// Res
// Sets g_Error to GetLastError and, if available,
// sets g_NetError and g_NetErrorMessage to the
// last network error supplied from the Net
// Service provider.
//
//////////////////////////////////////////////////////////
bool CNetService::GetNetError(int p_ServiceError)
{
char l_NetErrorMessage[1024];
char l_NetNameBuf[1024]; g_Error = GetLastError();
g_ServiceError = p_ServiceError;
g_NetError = 0; if (g_Error != ERROR_EXTENDED_ERROR)
return false;
if (WNetGetLastError(&g_NetError,
l_NetErrorMessage,
sizeof l_NetErrorMessage,
l_NetNameBuf,
sizeof l_NetNameBuf) != NO_ERROR)
return false;
g_NetErrorMessage.Format("%s", l_NetErrorMessage);
g_NetErrorProvider.Format("%s", l_NetNameBuf); return false;
}//////////////////////////////////////////////////////////
//
// NetError
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_NetError
// Parameters:
// None
// Res
//
//////////////////////////////////////////////////////////
DWORD CNetService::NetError()
{
return g_NetError;
}//////////////////////////////////////////////////////////
//
// NetErrorMessage
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_NetErrorMessage
// Parameters:
// None
// Res
// Returns a CString containing the last
// decoded network level error message.
//
//////////////////////////////////////////////////////////
CString CNetService::NetErrorMessage()
{
return g_NetErrorMessage;
}//////////////////////////////////////////////////////////
//
// NetErrorProvider
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_NetErrorProvider
// Parameters:
// None
// Res
// Returns the name of the network service
// provider reporting an error.
//
//////////////////////////////////////////////////////////
CString CNetService::NetErrorProvider()
{
return g_NetErrorProvider;
}//////////////////////////////////////////////////////////
//
// Result
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_Result, the result of the last
// network operation.
// Parameters:
// None
// Res
//
//////////////////////////////////////////////////////////
long CNetService::Result()
{
return g_Result;
}//////////////////////////////////////////////////////////
//
// OpenState
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_Open
// Parameters:
// None
// Res
// g_Open is true if OpenEnum was successful.
// reset to false by CloseEnum
//
//////////////////////////////////////////////////////////
bool CNetService::OpenState()
{
return g_Open;
}//////////////////////////////////////////////////////////
//
// Error
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_Error
// Parameters:
// None
// Res
// g_Error contains the latest GetLastError value.
//
//////////////////////////////////////////////////////////
DWORD CNetService::Error()
{
return g_Error;
}//////////////////////////////////////////////////////////
//
// ErrorMessage
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns a string representing the value
// in p_Error
// Parameters:
// p_Error
// System error setting to lookup
// Res
//
//////////////////////////////////////////////////////////
CString CNetService::ErrorMessage(DWORD p_Error)
{
char l_ErrorBuffer[4096];
CString l_ErrorString; memset (l_ErrorBuffer, 0, sizeof(l_ErrorBuffer));
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
p_Error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
l_ErrorBuffer,
sizeof(l_ErrorBuffer),
NULL);
l_ErrorString.Format(l_ErrorBuffer);
return l_ErrorString;
}//////////////////////////////////////////////////////////
//
// ServiceError
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_ServiceError
// Parameters:
// None
// Res
// g_ServiceErrror is set by NetServices to indicate
// the type of exception detected.
//
//////////////////////////////////////////////////////////
DWORD CNetService::ServiceError()
{
return g_ServiceError;
}//////////////////////////////////////////////////////////
//
// ServiceErrorMessage
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns a string representing the latest
// g_ServiceError
// Parameters:
// None
// Res
// g_ServiceErrror is set by NetServices to indicate
// the type of exception detected.
//
//////////////////////////////////////////////////////////
CString CNetService::ServiceErrorMessage(DWORD p_Error)
{
CString l_ErrorString; switch (p_Error)
{
case NETSERVICE_NOERROR:
l_ErrorString = "No error.";
break;
case NETSERVICE_OPEN_ENUM:
l_ErrorString = "Unable to open network enumeration.";
break;
case NETSERVICE_ENUM_RESOURCE:
l_ErrorString = "Unable to perform network enumeration.";
break;
case NETSERVICE_ADDCONN_ERROR:
l_ErrorString = "Unable to add network connection.";
break;
case NETSERVICE_GET_OSVER:
l_ErrorString = "Unable to determine OS Version.";
break;
case NETSERVICE_GETUSER:
l_ErrorString = "Unable to retrieve device user name.";
break;
case NETSERVICE_CANCELCONNECT:
l_ErrorString = "Unable to cancel connection.";
break;
case NETSERVICE_GETUNC_INFO:
l_ErrorString = "UNC information unavailable.";
break;
case NETSERVICE_GETREMOTE_INFO:
l_ErrorString = "Remote information unavailable.";
break;
case NETSERVICE_GETPROVIDER:
l_ErrorString = "Unknown provider name.";
break;
case NETSERVICE_CONN_DLG:
l_ErrorString = "Connection dialog failure.";
break;
case NETSERVICE_DISCONN_DLG:
l_ErrorString = "Disconnect dialog failure.";
break;
case NETSERVICE_MEM_ALLOC:
l_ErrorString = "Unable to allocate memory block.";
break;
case NETSERVICE_HEAP_ALLOC:
l_ErrorString = "Unable to allocate memory block.";
break;
case NETSERVICE_NETWORKINFO:
l_ErrorString = "Network information error.";
break;
default:
l_ErrorString = "Unknown error.";
}
return l_ErrorString;
}//////////////////////////////////////////////////////////
//
// OSVersion
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns a value indicating the operating system
// currently running on the host system:
// WIN_UNKNOWN Unable to determine
// WIN32_S Windows 3.1 (Win32S)
// WIN32_NT_PRO WindowsNT 4 Workstation
// WIN32_NT_SERVER WindowsNT 4 Server
// WIN32_2000_PRO Windows 2000 Workstation
// WIN32_2000_SERVER Windows 2000 Server
// WIN32_WINDOWS_95 Windows 95
// WIN32_WINDOWS_98 Windows 98
// Parameters:
// None
// Res
//
//////////////////////////////////////////////////////////
int CNetService::OSVersion()
{
BOOL l_OsVer2000;
OSVERSTRUCT l_OSVer; g_WindowsType = 0; ZeroMemory(&l_OSVer, sizeof(OSVERSIONINFOEX));
l_OSVer.OSVerX.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if ((l_OsVer2000 = GetVersionEx(&l_OSVer.OSVer)) == 0)
{
l_OSVer.OSVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (! GetVersionEx(&l_OSVer.OSVer) == 0)
{
g_ServiceError = NETSERVICE_GET_OSVER;
g_Result++;
g_Error = GetLastError();
return NULL;
}
}
switch (l_OSVer.OSVer.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
if (l_OSVer.OSVer.dwMajorVersion <= 4)
g_WindowsType = WIN32_NT_PRO;
if (l_OSVer.OSVer.dwMajorVersion == 5)
g_WindowsType = WIN32_2000_PRO;
break;
case VER_PLATFORM_WIN32_WINDOWS:
if ( (l_OSVer.OSVer.dwMajorVersion > 4) ||
( (l_OSVer.OSVer.dwMajorVersion == 4) &&
(l_OSVer.OSVer.dwMinorVersion > 0) ) )
g_WindowsType = WIN32_WINDOWS_98;
else
g_WindowsType = WIN32_WINDOWS_95;
break;
case VER_PLATFORM_WIN32s:
g_WindowsType = WIN32_S;
break;
default:
g_WindowsType = WIN_UNKNOWN;
}
return g_WindowsType;
}
#if !defined(_NetService_h__)
#define _NetService_h__//////////////////////////////////////////////////////////////////////////////////#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000//////////////////////////////////////////////////////////////////////////////////
//
// CNetService Class
//
//////////////////////////////////////////////////////////////////////////////////
//
// Written by Jay Wheeler
// EarthWalk Software
//
// Version 1 - October, 2000.
//
//////////////////////////////////////////////////////////////////////////////////
//
// CNetService is copyright ?2000. EarthWalk Software.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////////#include <winnetwk.h>//////////////////////////////////////////////////////////////////////////////////
//
// NOTE: Project must also include the following libraries:
// mpr.lib
//
//////////////////////////////////////////////////////////////////////////////////typedef struct _OSVERSTRUCT
{
union
{
OSVERSIONINFOEX OSVerX;
OSVERSIONINFO OSVer;
};
} OSVERSTRUCT;
//////////////////////////////////////////////////////////////////////////////////#define MAX_ENUM_BUFFER 16000//////////////////////////////////////////////////////////////////////////////////
//
// NetService Error Codes
//
//////////////////////////////////////////////////////////////////////////////////#define NETSERVICE_NOERROR 0
#define NETSERVICE_OPEN_ENUM NETSERVICE_NOERROR + 1
#define NETSERVICE_ENUM_RESOURCE NETSERVICE_OPEN_ENUM + 1
#define NETSERVICE_ADDCONN_ERROR NETSERVICE_ENUM_RESOURCE + 1
#define NETSERVICE_GET_OSVER NETSERVICE_ADDCONN_ERROR + 1
#define NETSERVICE_GETUSER NETSERVICE_GET_OSVER + 1
#define NETSERVICE_CANCELCONNECT NETSERVICE_GETUSER + 1
#define NETSERVICE_GETUNC_INFO NETSERVICE_CANCELCONNECT + 1
#define NETSERVICE_GETREMOTE_INFO NETSERVICE_GETUNC_INFO + 1
#define NETSERVICE_GETPROVIDER NETSERVICE_GETREMOTE_INFO + 1
#define NETSERVICE_CONN_DLG NETSERVICE_GETPROVIDER + 1
#define NETSERVICE_DISCONN_DLG NETSERVICE_CONN_DLG + 1
#define NETSERVICE_MEM_ALLOC NETSERVICE_DISCONN_DLG + 1
#define NETSERVICE_HEAP_ALLOC NETSERVICE_MEM_ALLOC + 1
#define NETSERVICE_NETWORKINFO NETSERVICE_HEAP_ALLOC + 1//////////////////////////////////////////////////////////////////////////////////
//
// Windows OS Types
//
//////////////////////////////////////////////////////////////////////////////////#define WIN_UNKNOWN 0
#define WIN32_S WIN_UNKNOWN + 1
#define WIN32_NT_PRO WIN32_S + 1
#define WIN32_NT_SERVER WIN32_NT_PRO + 1
#define WIN32_2000_PRO WIN32_NT_SERVER + 1
#define WIN32_2000_SERVER WIN32_2000_PRO + 1
#define WIN32_WINDOWS_95 WIN32_2000_SERVER + 1
#define WIN32_WINDOWS_98 WIN32_WINDOWS_95 + 1//////////////////////////////////////////////////////////////////////////////////
//
//
//
//////////////////////////////////////////////////////////////////////////////////
typedef struct _UNCInfo
{
union
{
UNIVERSAL_NAME_INFO UNC_NameInfo;
REMOTE_NAME_INFO UNC_RemoteInfo;
} Info;
} UNCInfo;#define MaxUNCInfo 4000//////////////////////////////////////////////////////////////////////////////////
//
// CNetService Class
//
//////////////////////////////////////////////////////////////////////////////////
class CNetService
{public:
CNetService(DWORD p_MemSize=MAX_ENUM_BUFFER);
virtual ~CNetService(); //////////////////////////////////////////////////////////////////////////////public:
virtual bool OpenEnum(DWORD p_Scope,
DWORD p_Type,
DWORD p_Usage,
NETRESOURCE * p_pNetResource=NULL);
virtual void CloseEnum(void); virtual bool EnumerateResource(DWORD p_Count);
virtual NETRESOURCE * EnumerateNext(void); virtual bool GetUNCInfo(CString p_LocalPath);
CString UniversalName(void); CString UNCRemoteServer(void);
CString UNCRemotePath(void); CString GetProviderName(DWORD p_NetType); virtual bool AddConnection(NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent=0);
virtual bool AddConnection(HWND p_hWnd,
NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent); virtual bool NetConnect(DWORD p_NetType,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent);
virtual bool NetConnect(HWND p_hWnd,
DWORD p_NetType,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent); virtual bool NetDisconnect(CString p_Local,
DWORD p_DisconnectType,
bool p_ForceDisconnect); CString NetUser(CString p_Remote); int OSVersion(void); long Result(void);
bool OpenState(void); DWORD Error(void);
CString ErrorMessage(DWORD p_Error); DWORD ServiceError(void);
CString ServiceErrorMessage(DWORD p_Error); DWORD NetError(void);
CString NetErrorMessage(void);
CString NetErrorProvider(void); DWORD ResourceDisplayType(void); bool NetworkInformation(CString p_ProviderName);
bool NetworkInformation(CString p_ProviderName,
NETINFOSTRUCT * p_pNetInfoStruct); NETINFOSTRUCT * GetNetInfo(void);
DWORD CNetService::GetNetStatus(void);
WORD CNetService::GetNetType(void);
DWORD CNetService::GetNetInfoPrinters(void);
DWORD CNetService::GetNetInfoDrives(void);protected:
bool GetNetError(int p_ServiceError);
private:
LPVOID GetMem(DWORD p_MemSize);
void FreeMem(LPVOID p_pMem); //////////////////////////////////////////////////////////////////////////////
public:protected:
long g_Result;
bool g_Open;
DWORD g_Error; int g_ServiceError;
DWORD g_Count; DWORD g_NetError;
CString g_NetErrorMessage;
CString g_NetErrorProvider; CString g_UserName;
CString g_ProviderName; HANDLE g_hEnum;
HANDLE g_hMem;
DWORD g_MemSize;
DWORD g_EnumMemSize; NETRESOURCE * g_pNetResource;
NETINFOSTRUCT g_NetInfoStruct; CString g_UniversalName;
CString g_RemainingPath;
CString g_ConnectionName; int g_WindowsType; DWORD g_EnumIndex;
DWORD g_EnumBufferSize;
char * g_pEnumBuffer;private:
};//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////#endif
#include "stdafx.h"
#include "NetService.h"//////////////////////////////////////////////////////////////////////////////////#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif//////////////////////////////////////////////////////////////////////////////////
//
// CNetService Class
//
//////////////////////////////////////////////////////////////////////////////////
//
// Written by Jay Wheeler
// EarthWalk Software
//
// Version 1 - October, 2000.
//
//////////////////////////////////////////////////////////////////////////////////
//
// CNetService is copyright ?2000. EarthWalk Software.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.,
// or download a copy of the license from the Free Software Foundation at
// http://www.fsf.org/
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Constructor
//
//////////////////////////////////////////////////////////
CNetService::CNetService(DWORD p_MemSize)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_Open = false; g_MemSize = p_MemSize + 4096;
g_hMem = NULL;
g_ServiceError = NETSERVICE_NOERROR;
g_hMem = HeapCreate(0,
g_MemSize,
0);
if (g_hMem == NULL)
{
g_Error = GetLastError();
g_ServiceError = NETSERVICE_HEAP_ALLOC;
return;
} g_pEnumBuffer = (char *)GetMem(p_MemSize);
g_EnumMemSize = p_MemSize;
g_pNetResource = (NETRESOURCE *)g_pEnumBuffer;}//////////////////////////////////////////////////////////
//
// Destructor
//
//////////////////////////////////////////////////////////
//
// Extend this method with destructor for
// extended class. The code here will execute
// prior to the new class destructor
//
//////////////////////////////////////////////////////////
CNetService::~CNetService()
{
if (g_Open)
CloseEnum();
if (g_hMem != NULL)
{
if (g_pEnumBuffer != NULL)
FreeMem(g_pEnumBuffer);
HeapDestroy(g_hMem);
}
}//////////////////////////////////////////////////////////
//
// FreeMem
//
//////////////////////////////////////////////////////////
//
// Return Value:
// None.
// Parameters:
// p_pMem
// pointer to memory to deallocate
// Res
// Deallocates memory allocated by GetMem.
//
//////////////////////////////////////////////////////////
void CNetService::FreeMem(void * p_pMem)
{
HeapFree(g_hMem,
0,
p_pMem);
p_pMem = NULL;
}//////////////////////////////////////////////////////////
//
// GetMem
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Pointer to allocated memory or NULL if error.
// Parameters:
// p_MemSize
// Size of memory, in bytes, to allocate.
// Res
// GetMem calls HeapAlloc to allocate memory on
// the heap pointed to by g_hMem.
//
//////////////////////////////////////////////////////////
LPVOID CNetService::GetMem(DWORD p_MemSize)
{
LPVOID l_pMem; l_pMem = HeapAlloc(g_hMem,
HEAP_ZERO_MEMORY,
p_MemSize);
if (l_pMem == NULL)
{
g_Error += 1;
g_ServiceError = NETSERVICE_MEM_ALLOC;
return NULL;
}
return l_pMem;
}//////////////////////////////////////////////////////////
//
// OpenEnum
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false.
// Parameters:
// p_Scope
// Specifies the scope of the enumeration:
// RESOURCE_CONNECTED
// RESOURCE_CONTEXT
// RESOURCE_GLOBALNET
// RESOURCE_REMEMBERED
// p_Type
// Specifies the resource types to enumerate:
// RESOURCETYPE_ANY
// RESOURCETYPE_DISK
// RESOURCETYPE_PRINT
// p_Usage
// If 0 = ALL resources
// Specifies the resource usage type to enumerate:
// 0 = ALL resources
// RESOURCEUSAGE_CONNECTABLE
// RESOURCEUSAGE_CONTAINER
// RESOURCEUSAGE_ATTACHED
// RESOURCEUSAGE_ALL
// p_pNetResource
// Pointer to an optional NETRESOURCE structure to
// apply the open to.
// Res
// Refer to WNetOpenEnum for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
bool CNetService::OpenEnum(DWORD p_Scope,
DWORD p_Type,
DWORD p_Usage,
NETRESOURCE * p_pNetResource)
{
g_pNetResource = p_pNetResource;
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_Open = false; g_Result = WNetOpenEnum(p_Scope,
p_Type,
p_Usage,
g_pNetResource,
&g_hEnum);
if (g_Result != NO_ERROR)
return GetNetError(NETSERVICE_OPEN_ENUM);
g_Open = true;
return true;
}//////////////////////////////////////////////////////////
//
// CloseEnum
//
//////////////////////////////////////////////////////////
//
// Return Value:
// None
// Parameters:
// None
// Res
// Closes the current enumeration handle.
//
// Refer to WNetCloseEnum for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
void CNetService::CloseEnum()
{
if (! g_Open)
return;
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_Open = false; g_Result = WNetCloseEnum(g_hEnum);
}//////////////////////////////////////////////////////////
//
// EnumerateResource
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false.
// Parameters:
// p_Count
// The number of enumerations to fetch to
// the default EnumBuffer.
//
// Set to -1 to retrieve as many as will
// fit in EnumBuffer.
// Res
// Fetches another resource enumeration buffer to
// continue an enumeration of network resources
// that was started by a call to OpenEnum.
//
// Refer to WNetEnumResource for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
bool CNetService::EnumerateResource(DWORD p_Count)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0; g_Count = p_Count;
g_EnumBufferSize = MAX_ENUM_BUFFER; g_Result = WNetEnumResource(g_hEnum,
&g_Count,
g_pEnumBuffer,
&g_EnumBufferSize);
if (g_Result != NO_ERROR)
{
if (g_Result == ERROR_NO_MORE_ITEMS)
return false;
return GetNetError(NETSERVICE_ENUM_RESOURCE);
}
g_pNetResource = (NETRESOURCE *)g_pEnumBuffer;
g_EnumIndex = 0; return true;
}//////////////////////////////////////////////////////////
//
// EnumerateNext
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Pointer to the next NETRESOURCE structure
// in the enumeration buffer, or NULL if at
// the end of the buffer (not necessarily
// the end of the enumeration).
// Parameters:
// None
// Res
// Returns a pointer to the next NETRESOURCE
// structure in the resource enumeration buffer.
//
// Refer to WNetEnumResource for discussion of the
// purpose and usage.
//
//////////////////////////////////////////////////////////
NETRESOURCE * CNetService::EnumerateNext()
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0; if (g_EnumIndex >= g_Count)
return NULL;
NETRESOURCE * l_pNetResource = &g_pNetResource[g_EnumIndex];
g_EnumIndex++;
return l_pNetResource;
}//////////////////////////////////////////////////////////
//
// GetUNC
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful
// Parameters:
// p_LocalPath
// Path to a local device to query.
// p_InfoLevel
// Type of information to retrieve:
// UNIVERSAL_NAME_INFO_LEVEL
// REMOTE_NAME_INFO_LEVEL
// Res
// Retrieves UNC (Universal Naming Convention)
// information representing the specified network
// connection.
//
// The following Methods expose the contents
// returned by this Method:
//
// UniversalName
// UNCRemoteServer
// UNCRemotePath
//
//////////////////////////////////////////////////////////
bool CNetService::GetUNCInfo(CString p_LocalPath)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
UNCInfo * l_pUNC = (UNCInfo *)GetMem(MaxUNCInfo);
if (l_pUNC == NULL)
return false; DWORD l_InfoSize = MaxUNCInfo;
if ((g_Result = WNetGetUniversalName(p_LocalPath,
UNIVERSAL_NAME_INFO_LEVEL,
l_pUNC,
&l_InfoSize)) != NO_ERROR)
{
GetNetError(NETSERVICE_GETUNC_INFO);
FreeMem(l_pUNC);
return false;
} g_UniversalName.Format("%s", l_pUNC->Info.UNC_NameInfo.lpUniversalName); l_InfoSize = MaxUNCInfo;
if ((g_Result = WNetGetUniversalName(p_LocalPath,
REMOTE_NAME_INFO_LEVEL,
l_pUNC,
&l_InfoSize)) != NO_ERROR)
{
GetNetError(NETSERVICE_GETREMOTE_INFO);
FreeMem(l_pUNC);
return false;
} g_ConnectionName.Format("%s", l_pUNC->Info.UNC_RemoteInfo.lpConnectionName);
g_UniversalName.Format("%s", l_pUNC->Info.UNC_RemoteInfo.lpUniversalName);
g_RemainingPath.Format("%s", l_pUNC->Info.UNC_RemoteInfo.lpRemainingPath); FreeMem(l_pUNC);
return true;
}//////////////////////////////////////////////////////////
//
// UNCRemoteServer
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Return the connection name from the last
// GetUNC call.
// Parameters:
// None.
// Res
// No test is made to ensure the UNC buffer
// exists or is valid.
//
//////////////////////////////////////////////////////////
CString CNetService::UNCRemoteServer()
{
return g_ConnectionName;
}//////////////////////////////////////////////////////////
//
// UNCRemotePath
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Return the remote share path from the last
// GetUNC call.
// Parameters:
// None.
// Res
// No test is made to ensure the UNC buffer
// exists or is valid.
//
//////////////////////////////////////////////////////////
CString CNetService::UNCRemotePath()
{
return g_RemainingPath;
}//////////////////////////////////////////////////////////
//
// UniversalName
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Return the UNC for the connection.
// Parameters:
// None.
// Res
// No test is made to ensure the UNC buffer
// exists or is valid.
//
//////////////////////////////////////////////////////////
CString CNetService::UniversalName()
{
return g_UniversalName;
}//////////////////////////////////////////////////////////
//
// GetProviderName
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A string containing the provider name associated
// with a specified network type.
// Parameters:
// p_NetType
// Type of network (e.g. Novell, Banyan, etc.)
// to query
// Res
// The network transport provider name is returned.
//
//////////////////////////////////////////////////////////
CString CNetService::GetProviderName(DWORD p_NetType)
{
g_ServiceError = NETSERVICE_NOERROR;
g_Result = ERROR_SUCCESS;
g_Error = 0;
g_ProviderName.Empty(); DWORD l_BufferSize = 4096;
char * l_pProviderName = (char *)GetMem(l_BufferSize); if (l_pProviderName == NULL)
return g_ProviderName;
if ((g_Result = WNetGetProviderName(p_NetType,
l_pProviderName,
&l_BufferSize)) != NO_ERROR)
GetNetError(NETSERVICE_GETPROVIDER);
else
g_ProviderName.Format("%s", l_pProviderName);
FreeMem(l_pProviderName); return g_ProviderName;
}//////////////////////////////////////////////////////////
//
// NetworkInformation
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful
// Parameters:
// p_ProviderName
// Name of a network provider (as returned from
// GetProvider).
// Res
// This method fetches a NETWORKINFOSTRUC structure
// containing information about the requested
// provider. This information may be retrieved
// using one of the NetInfoxxxx methods.
//
//////////////////////////////////////////////////////////
bool CNetService::NetworkInformation(CString p_ProviderName)
{
return NetworkInformation(p_ProviderName,
&g_NetInfoStruct);
}//////////////////////////////////////////////////////////
//
// NetworkInformation
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful
// Parameters:
// p_ProviderName
// Name of a network provider (as returned from
// GetProvider).
// p_pNetInfoStruct
// Pointer to a NETINFOSTRUCT to use in the query.
// Res
// This method fetches a NETWORKINFOSTRUC structure
// containing information about the requested
// provider. A copy of the returned NETINFOSTRUCT
// is not saved locally.
//
//////////////////////////////////////////////////////////
bool CNetService::NetworkInformation(CString p_ProviderName,
NETINFOSTRUCT * p_pNetInfoStruct)
{
ZeroMemory(p_pNetInfoStruct, sizeof NETINFOSTRUCT);
p_pNetInfoStruct->cbStructure = sizeof NETINFOSTRUCT; if (p_ProviderName.IsEmpty())
return false;
if ((g_Result = WNetGetNetworkInformation(p_ProviderName,
p_pNetInfoStruct)) != NO_ERROR)
{
return GetNetError(NETSERVICE_NETWORKINFO);
}
return true;
}//////////////////////////////////////////////////////////
//
// NetConnect
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_NetDevice
// Network device type requested:
// RESOURCETYPE_DISK
// RESOURCETYPE_PRINT
// p_Local
// Specifies the name of a local device
// NULL for no local device
// p_Remote
// Specifies the remote network name for new connection, or
// network name associated with the name pointed to by
// p_LocalName for current/persistent connection.
// p_NetStack
// The name of the provider that owns the resource, or
// NULL to let the system assign.
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// Creates the specified network connection.
//
//////////////////////////////////////////////////////////
bool CNetService::NetConnect(DWORD p_NetDevice,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
return NetConnect(NULL,
p_NetDevice,
p_Local,
p_Remote,
p_NetStack,
p_UserName,
p_Password,
p_Persistent);
}//////////////////////////////////////////////////////////
//
// NetConnect
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_hWnd
// Window handle for network provider prompts,
// NULL if not Window app or not required.
// p_NetDevice
// Network device type requested:
// NET_DISK
// NET_PRINTER
// p_Local
// Specifies the name of a local device
// NULL for no local device
// p_Remote
// Specifies the remote network name for new connection, or
// network name associated with the name pointed to by
// p_LocalName for current/persistent connection.
// p_NetStack
// The name of the provider that owns the resource, or
// NULL to let the system assign.
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// Creates the specified network connection.
//
// Refer to WNetAddConnection3 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::NetConnect(HWND p_hWnd,
DWORD p_NetType,
CString p_Local,
CString p_Remote,
CString p_NetStack,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
DWORD l_BufferSize = 4096;
void * l_pEnumBuffer = GetMem(l_BufferSize);
g_pNetResource = (NETRESOURCE *)l_pEnumBuffer;
ZeroMemory(g_pNetResource, l_BufferSize); char l_Local [MAX_PATH+1];
char l_Remote [MAX_PATH + 1];
char l_Provider [MAX_PATH + 1]; ZeroMemory(l_Local, MAX_PATH + 1);
ZeroMemory(l_Remote, MAX_PATH + 1);
ZeroMemory(l_Provider, MAX_PATH + 1); if (p_Local.IsEmpty())
g_pNetResource->lpLocalName = NULL;
else
{
if (p_Local.GetLength() > MAX_PATH)
p_Local.Delete(MAX_PATH, MAX_PATH);
memcpy(l_Local, p_Local, p_Local.GetLength());
g_pNetResource->lpLocalName = l_Local;
} if (p_Remote.IsEmpty())
g_pNetResource->lpRemoteName = NULL;
else
{
if (p_Remote.GetLength() > MAX_PATH)
p_Remote.Delete(MAX_PATH, MAX_PATH);
memcpy(l_Remote, p_Remote, p_Remote.GetLength());
g_pNetResource->lpRemoteName = l_Remote;
} if (p_NetStack.IsEmpty())
g_pNetResource->lpProvider = NULL;
else
{
if (p_NetStack.GetLength() > MAX_PATH)
p_NetStack.Delete(MAX_PATH, MAX_PATH);
memcpy(l_Provider, p_NetStack, p_NetStack.GetLength());
g_pNetResource->lpProvider = l_Provider;
} g_pNetResource->dwScope = RESOURCE_GLOBALNET;
g_pNetResource->dwType = p_NetType;
g_pNetResource->dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
g_pNetResource->dwUsage = RESOURCEUSAGE_CONNECTABLE; bool l_Result = AddConnection(p_hWnd,
g_pNetResource,
p_UserName,
p_Password,
p_Persistent);
FreeMem(l_pEnumBuffer);
return l_Result;
}//////////////////////////////////////////////////////////
//
// AddConnection
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_pNetResource
// Pointer to a NETRESOURCE structure for
// this request
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// Creates the specified network connection.
//
// refer to WNetAddConnection3 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::AddConnection(NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
return AddConnection(NULL,
p_pNetResource,
p_UserName,
p_Password,
p_Persistent);
}//////////////////////////////////////////////////////////
//
// AddConnectionW
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false
// (g_Result = result code)
// Parameters:
// p_hWnd
// Window handle for network provider prompts,
// NULL if not Window app or not required.
// p_pNetResource
// Pointer to a NETRESOURCE structure for
// this request
// p_UserName
// User name to connect to the network share
// with, or NULL for current name.
// p_Password
// Password to qualify p_UserName with, if
// p_UserName is not NULL.
// p_Persistent
// True to create a persistent connection,
// False for temporary connection.
// Res
// refer to WNetAddConnection3 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::AddConnection(HWND p_hWnd,
NETRESOURCE * p_pNetResource,
CString p_UserName,
CString p_Password,
bool p_Persistent)
{
DWORD l_dwFlags = (p_Persistent) ? 0 : CONNECT_UPDATE_PROFILE; if ((g_Result = WNetAddConnection3(p_hWnd,
p_pNetResource,
p_Password,
p_UserName,
l_dwFlags)) != NO_ERROR)
return GetNetError(NETSERVICE_ADDCONN_ERROR);
return true;
}//////////////////////////////////////////////////////////
//
// NetUser
//
//////////////////////////////////////////////////////////
//
// Return Value:
// User name associated with the network device.
// Parameters:
// p_Remote
// Specifies the network name associated with
// the requested user name.
// Res
// Creates the specified network connection.
// Sets g_UserName to return value.
//
// Refer to WNetGetUser for specifics.
//
//////////////////////////////////////////////////////////
CString CNetService::NetUser(CString p_Remote)
{
DWORD l_AccountUserSize = 256;
char * l_pAccountUser = (char *)GetMem(l_AccountUserSize); g_UserName.Empty();
ZeroMemory(l_pAccountUser, l_AccountUserSize);
if ((g_Result = WNetGetUser(p_Remote,
l_pAccountUser,
&l_AccountUserSize)) != NO_ERROR)
GetNetError(NETSERVICE_GETUSER);
else
g_UserName.Format("%s", l_pAccountUser); FreeMem(l_pAccountUser); return g_UserName;
}//////////////////////////////////////////////////////////
//
// NetDisconnect
//
//////////////////////////////////////////////////////////
//
// Return Value:
// true if successful, else false.
// Parameters:
// p_Local
// specifies the name of either the redirected
// local device or the remote network resource
// to disconnect from
// p_DisconnectType
// describes the connection type
// p_ForceDisconnect
// if true, disconnects even if files
// are open.
// Res
// Refer to WNetCancelConnection2 for specifics.
//
//////////////////////////////////////////////////////////
bool CNetService::NetDisconnect(CString p_Local,
DWORD p_DisconnectType,
bool p_ForceDisconnect)
{
if ((g_Result = WNetCancelConnection2(p_Local,
p_DisconnectType,
p_ForceDisconnect)) != NO_ERROR)
{
return GetNetError(NETSERVICE_CANCELCONNECT);
}
return true;
}//////////////////////////////////////////////////////////
//
// ResourceDisplayType
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Resource Display Type from the most recent
// NETRESOURCE structure.
// Parameters:
// None
// Res
// Refer to NETRESOURCE structure for specifics.
//
//////////////////////////////////////////////////////////
DWORD CNetService::ResourceDisplayType()
{
return g_pNetResource->dwDisplayType;
}//////////////////////////////////////////////////////////
//
// GetNetInfo
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A pointer to the local NETINFOSTRUCT.
// Parameters:
// None.
// Res
// Returns a pointer to the local NETINFOSTRUCT
// used in a NetworkInformation method.
//
//////////////////////////////////////////////////////////
NETINFOSTRUCT * CNetService::GetNetInfo()
{
return &g_NetInfoStruct;
}//////////////////////////////////////////////////////////
//
// GetNetInfoDrives
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A DWORD bit vector indicating the valid local
// disk devices for redirection.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
DWORD CNetService::GetNetInfoDrives()
{
return g_NetInfoStruct.dwDrives;
}//////////////////////////////////////////////////////////
//
// GetNetInfoPrinters
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A DWORD bit vector indicating the valid local
// print devices for redirection.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
DWORD CNetService::GetNetInfoPrinters()
{
return g_NetInfoStruct.dwPrinters;
}//////////////////////////////////////////////////////////
//
// GetNetType
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A WORD value indicating the network type.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
WORD CNetService::GetNetType()
{
return g_NetInfoStruct.wNetType;
}//////////////////////////////////////////////////////////
//
// GetNetStatus
//
//////////////////////////////////////////////////////////
//
// Return Value:
// A DWORD value indicating the network status:
// NO_ERROR Network is running.
// ERROR_NO_NETWORK Network is not running.
// ERROR_BUSY Network is initializing.
// Parameters:
// None.
// Res
// Requires a call to NetworkInformation to setup
// the local NETINFOSTRUCT variable.
//
//////////////////////////////////////////////////////////
DWORD CNetService::GetNetStatus()
{
return g_NetInfoStruct.dwStatus;
}//////////////////////////////////////////////////////////
//
// GetNetError
//
//////////////////////////////////////////////////////////
//
// Return Value:
// false
// Parameters:
// NetService error number to store.
// Res
// Sets g_Error to GetLastError and, if available,
// sets g_NetError and g_NetErrorMessage to the
// last network error supplied from the Net
// Service provider.
//
//////////////////////////////////////////////////////////
bool CNetService::GetNetError(int p_ServiceError)
{
char l_NetErrorMessage[1024];
char l_NetNameBuf[1024]; g_Error = GetLastError();
g_ServiceError = p_ServiceError;
g_NetError = 0; if (g_Error != ERROR_EXTENDED_ERROR)
return false;
if (WNetGetLastError(&g_NetError,
l_NetErrorMessage,
sizeof l_NetErrorMessage,
l_NetNameBuf,
sizeof l_NetNameBuf) != NO_ERROR)
return false;
g_NetErrorMessage.Format("%s", l_NetErrorMessage);
g_NetErrorProvider.Format("%s", l_NetNameBuf); return false;
}//////////////////////////////////////////////////////////
//
// NetError
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_NetError
// Parameters:
// None
// Res
//
//////////////////////////////////////////////////////////
DWORD CNetService::NetError()
{
return g_NetError;
}//////////////////////////////////////////////////////////
//
// NetErrorMessage
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_NetErrorMessage
// Parameters:
// None
// Res
// Returns a CString containing the last
// decoded network level error message.
//
//////////////////////////////////////////////////////////
CString CNetService::NetErrorMessage()
{
return g_NetErrorMessage;
}//////////////////////////////////////////////////////////
//
// NetErrorProvider
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_NetErrorProvider
// Parameters:
// None
// Res
// Returns the name of the network service
// provider reporting an error.
//
//////////////////////////////////////////////////////////
CString CNetService::NetErrorProvider()
{
return g_NetErrorProvider;
}//////////////////////////////////////////////////////////
//
// Result
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_Result, the result of the last
// network operation.
// Parameters:
// None
// Res
//
//////////////////////////////////////////////////////////
long CNetService::Result()
{
return g_Result;
}//////////////////////////////////////////////////////////
//
// OpenState
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_Open
// Parameters:
// None
// Res
// g_Open is true if OpenEnum was successful.
// reset to false by CloseEnum
//
//////////////////////////////////////////////////////////
bool CNetService::OpenState()
{
return g_Open;
}//////////////////////////////////////////////////////////
//
// Error
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_Error
// Parameters:
// None
// Res
// g_Error contains the latest GetLastError value.
//
//////////////////////////////////////////////////////////
DWORD CNetService::Error()
{
return g_Error;
}//////////////////////////////////////////////////////////
//
// ErrorMessage
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns a string representing the value
// in p_Error
// Parameters:
// p_Error
// System error setting to lookup
// Res
//
//////////////////////////////////////////////////////////
CString CNetService::ErrorMessage(DWORD p_Error)
{
char l_ErrorBuffer[4096];
CString l_ErrorString; memset (l_ErrorBuffer, 0, sizeof(l_ErrorBuffer));
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
p_Error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
l_ErrorBuffer,
sizeof(l_ErrorBuffer),
NULL);
l_ErrorString.Format(l_ErrorBuffer);
return l_ErrorString;
}//////////////////////////////////////////////////////////
//
// ServiceError
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns g_ServiceError
// Parameters:
// None
// Res
// g_ServiceErrror is set by NetServices to indicate
// the type of exception detected.
//
//////////////////////////////////////////////////////////
DWORD CNetService::ServiceError()
{
return g_ServiceError;
}//////////////////////////////////////////////////////////
//
// ServiceErrorMessage
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns a string representing the latest
// g_ServiceError
// Parameters:
// None
// Res
// g_ServiceErrror is set by NetServices to indicate
// the type of exception detected.
//
//////////////////////////////////////////////////////////
CString CNetService::ServiceErrorMessage(DWORD p_Error)
{
CString l_ErrorString; switch (p_Error)
{
case NETSERVICE_NOERROR:
l_ErrorString = "No error.";
break;
case NETSERVICE_OPEN_ENUM:
l_ErrorString = "Unable to open network enumeration.";
break;
case NETSERVICE_ENUM_RESOURCE:
l_ErrorString = "Unable to perform network enumeration.";
break;
case NETSERVICE_ADDCONN_ERROR:
l_ErrorString = "Unable to add network connection.";
break;
case NETSERVICE_GET_OSVER:
l_ErrorString = "Unable to determine OS Version.";
break;
case NETSERVICE_GETUSER:
l_ErrorString = "Unable to retrieve device user name.";
break;
case NETSERVICE_CANCELCONNECT:
l_ErrorString = "Unable to cancel connection.";
break;
case NETSERVICE_GETUNC_INFO:
l_ErrorString = "UNC information unavailable.";
break;
case NETSERVICE_GETREMOTE_INFO:
l_ErrorString = "Remote information unavailable.";
break;
case NETSERVICE_GETPROVIDER:
l_ErrorString = "Unknown provider name.";
break;
case NETSERVICE_CONN_DLG:
l_ErrorString = "Connection dialog failure.";
break;
case NETSERVICE_DISCONN_DLG:
l_ErrorString = "Disconnect dialog failure.";
break;
case NETSERVICE_MEM_ALLOC:
l_ErrorString = "Unable to allocate memory block.";
break;
case NETSERVICE_HEAP_ALLOC:
l_ErrorString = "Unable to allocate memory block.";
break;
case NETSERVICE_NETWORKINFO:
l_ErrorString = "Network information error.";
break;
default:
l_ErrorString = "Unknown error.";
}
return l_ErrorString;
}//////////////////////////////////////////////////////////
//
// OSVersion
//
//////////////////////////////////////////////////////////
//
// Return Value:
// Returns a value indicating the operating system
// currently running on the host system:
// WIN_UNKNOWN Unable to determine
// WIN32_S Windows 3.1 (Win32S)
// WIN32_NT_PRO WindowsNT 4 Workstation
// WIN32_NT_SERVER WindowsNT 4 Server
// WIN32_2000_PRO Windows 2000 Workstation
// WIN32_2000_SERVER Windows 2000 Server
// WIN32_WINDOWS_95 Windows 95
// WIN32_WINDOWS_98 Windows 98
// Parameters:
// None
// Res
//
//////////////////////////////////////////////////////////
int CNetService::OSVersion()
{
BOOL l_OsVer2000;
OSVERSTRUCT l_OSVer; g_WindowsType = 0; ZeroMemory(&l_OSVer, sizeof(OSVERSIONINFOEX));
l_OSVer.OSVerX.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if ((l_OsVer2000 = GetVersionEx(&l_OSVer.OSVer)) == 0)
{
l_OSVer.OSVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (! GetVersionEx(&l_OSVer.OSVer) == 0)
{
g_ServiceError = NETSERVICE_GET_OSVER;
g_Result++;
g_Error = GetLastError();
return NULL;
}
}
switch (l_OSVer.OSVer.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
if (l_OSVer.OSVer.dwMajorVersion <= 4)
g_WindowsType = WIN32_NT_PRO;
if (l_OSVer.OSVer.dwMajorVersion == 5)
g_WindowsType = WIN32_2000_PRO;
break;
case VER_PLATFORM_WIN32_WINDOWS:
if ( (l_OSVer.OSVer.dwMajorVersion > 4) ||
( (l_OSVer.OSVer.dwMajorVersion == 4) &&
(l_OSVer.OSVer.dwMinorVersion > 0) ) )
g_WindowsType = WIN32_WINDOWS_98;
else
g_WindowsType = WIN32_WINDOWS_95;
break;
case VER_PLATFORM_WIN32s:
g_WindowsType = WIN32_S;
break;
default:
g_WindowsType = WIN_UNKNOWN;
}
return g_WindowsType;
}
解决方案 »
- 为什么我定义了def文件,DLL里的函数还是被改名呢
- 关于线程同步的问题
- 远程注入呼不出来窗口咋回事的呀??
- 用任何方法连接网络,卡巴都报PDM.DNS Query
- C*DLG dlg;dlg.domodal();为什么不执行oninitdialog()函数呢?
- 請大家說說 .h 文件中使用class C***;而不寫(定義)具體的類內容的用法及作用??? 謝謝!!!
- 下面的错误是怎么回事呢?
- 关于单文档视图
- 会这样吗?
- 关于套接字的建立,大家帮我看看错在哪里
- 谁有类似Acdc那样可以显示bmp图片缩略图的原码?或者告诉我应该怎么做?
- 请问我想捕获键盘的ctrl+alt+q事件应该怎样写?谢谢!!!
//////////////////////////////////////////////////////////////////////////////////
//
// WNetEnumDlg
//
//////////////////////////////////////////////////////////////////////////////////
//
// Written by Jay Wheeler
// EarthWalk Software
//
// Version 1 - October, 2000.
//
//////////////////////////////////////////////////////////////////////////////////
//
// WNetEnum is copyright ?2000. EarthWalk Software.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////////#if !defined(AFX_WNETENUMDLG_H__684C38B8_A6C7_11D4_B4B1_0000E89D178F__INCLUDED_)
#define AFX_WNETENUMDLG_H__684C38B8_A6C7_11D4_B4B1_0000E89D178F__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////#include "WNetEnum.h"
#include "NetService.h"/////////////////////////////////////////////////////////////////////////////enum ImageType
{
Icon_Global,
Icon_Network,
Icon_Server,
Icon_FolderClosed,
Icon_FolderOpen,
Icon_Disk,
Icon_Print
};/////////////////////////////////////////////////////////////////////////////class CWNetEnumDlg : public CDialog
{
public:
CWNetEnumDlg(CWnd* pParent = NULL); // standard constructor void Init(void);
void FillTree(void); bool EnumerateContainer(NETRESOURCE * p_pNetResource);
void ParseName(CString p_Name);
void DisplayNetworkError(CNetService * p_pNetService); void InitImageList(void);// Dialog Data
//{{AFX_DATA(CWNetEnumDlg)
enum { IDD = IDD_WNETENUM_DIALOG };
CTreeCtrl m_treeList;
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CWNetEnumDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon; // Generated message map functions
//{{AFX_MSG(CWNetEnumDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
afx_msg void OnAbout();
afx_msg void OnRefresh();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
TVITEMEX g_tvItem;
TVINSERTSTRUCT g_tvInsert; int g_level;
HTREEITEM g_hRoot[10]; char g_Server[MAX_PATH];
char g_Share[MAX_PATH]; CString g_ShareName; CImageList g_smallImageList; // List of small images for ListCtrl
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_WNETENUMDLG_H__684C38B8_A6C7_11D4_B4B1_0000E89D178F__INCLUDED_)
//////////////////////////////////////////////////////////////////////////////////
//
// WNetEnumDlg
//
//////////////////////////////////////////////////////////////////////////////////
//
// Written by Jay Wheeler
// EarthWalk Software
//
// Version 1 - October, 2000.
//
//////////////////////////////////////////////////////////////////////////////////
//
// WNetEnum is copyright ?2000. EarthWalk Software.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//////////////////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "WNetEnumDlg.h"/////////////////////////////////////////////////////////////////////////////#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
//
// CAboutDlg
//
/////////////////////////////////////////////////////////////////////////////class CAboutDlg : public CDialog
{
public:
CAboutDlg();// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA // ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
//
// CWNetEnumDlg
//
/////////////////////////////////////////////////////////////////////////////CWNetEnumDlg::CWNetEnumDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWNetEnumDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWNetEnumDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CWNetEnumDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWNetEnumDlg)
DDX_Control(pDX, IDC_TREE, m_treeList);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CWNetEnumDlg, CDialog)
//{{AFX_MSG_MAP(CWNetEnumDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_About, OnAbout)
ON_BN_CLICKED(IDC_Refresh, OnRefresh)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
//
// OnInitDialog
//
/////////////////////////////////////////////////////////////////////////////
BOOL CWNetEnumDlg::OnInitDialog()
{
CDialog::OnInitDialog(); ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
} SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
Init(); return TRUE;
}/////////////////////////////////////////////////////////////////////////////
//
// OnSysCommand
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}/////////////////////////////////////////////////////////////////////////////
//
// OnPaint
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}/////////////////////////////////////////////////////////////////////////////
//
// OnQueryDragIcon
//
/////////////////////////////////////////////////////////////////////////////
HCURSOR CWNetEnumDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}/////////////////////////////////////////////////////////////////////////////
//
// OnOK
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::OnOK()
{
CDialog::OnOK();
}/////////////////////////////////////////////////////////////////////////////
//
// OnRefresh
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::OnRefresh()
{
m_treeList.DeleteAllItems();
g_level = 0; EnumerateContainer(NULL);
}/////////////////////////////////////////////////////////////////////////////
//
// OnAbout
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::OnAbout()
{
CAboutDlg * l_pAbout = new CAboutDlg();
l_pAbout->DoModal();
delete l_pAbout;
}/////////////////////////////////////////////////////////////////////////////
//
// Init
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::Init()
{
InitImageList();
m_treeList.DeleteAllItems();
g_level = 0; EnumerateContainer(NULL);
}/////////////////////////////////////////////////////////////////////////////
//
// EnumerateContainer
//
/////////////////////////////////////////////////////////////////////////////
bool CWNetEnumDlg::EnumerateContainer(NETRESOURCE * p_pNetResource)
{
CNetService * l_pNetService = new CNetService();
HTREEITEM l_treeItem;
int l_ShareImage;
int l_ShareImageSel; if (! l_pNetService->OpenEnum(RESOURCE_GLOBALNET, // p_Scope
RESOURCETYPE_ANY, // p_Type
NULL, // p_Usage
p_pNetResource)) // p_pNetResource
{
DisplayNetworkError(l_pNetService);
delete l_pNetService;
return false;
} if (! l_pNetService->EnumerateResource(-1))
{
DisplayNetworkError(l_pNetService);
l_pNetService->CloseEnum();
delete l_pNetService;
return false;
} do
{
while ((p_pNetResource = l_pNetService->EnumerateNext()) != NULL)
{
if ( (p_pNetResource->lpRemoteName != NULL) )
{
ParseName(p_pNetResource->lpRemoteName);
if ((p_pNetResource->dwUsage & RESOURCEUSAGE_CONTAINER) == RESOURCEUSAGE_CONTAINER)
{
if (m_treeList.GetCount() == 0)
{
g_tvInsert.hParent = NULL;
g_tvInsert.hInsertAfter = NULL;
g_tvInsert.item.mask = TVIF_TEXT;
g_tvInsert.item.pszText = g_Server; g_hRoot[0] = m_treeList.InsertItem(&g_tvInsert);
}
else
{
if (g_level > 0)
g_hRoot[g_level] = m_treeList.InsertItem(g_Server,
g_level,
g_level,
g_hRoot[g_level - 1],
TVI_SORT);
else
g_hRoot[g_level] = m_treeList.InsertItem(g_Server,
g_level,
g_level,
g_hRoot[g_level],
TVI_SORT);
}
if (g_hRoot[g_level] == NULL)
{
MessageBox("Unable to insert item in tree",
"InsertItem",
MB_OK);
g_level--;
return false;
}
g_level++;
if (! EnumerateContainer(p_pNetResource))
return false;
g_level--;
}
else
{
l_ShareImage = Icon_FolderClosed;
l_ShareImageSel = Icon_FolderOpen; if (p_pNetResource->dwType == RESOURCETYPE_DISK)
{
l_ShareImage = Icon_Disk;
l_ShareImageSel = Icon_Disk;
}
else
if (p_pNetResource->dwType == RESOURCETYPE_PRINT)
{
l_ShareImage = Icon_Print;
l_ShareImageSel = Icon_Print;
}
l_treeItem = m_treeList.InsertItem (g_Share,
l_ShareImage,
l_ShareImage,
g_hRoot[g_level-1],
TVI_SORT);
}
}
}
}
while (l_pNetService->EnumerateResource(-1)); if ((l_pNetService->Error() != NO_ERROR) &&
(l_pNetService->Error() != ERROR_NO_MORE_ITEMS))
DisplayNetworkError(l_pNetService);
l_pNetService->CloseEnum();
delete l_pNetService;
return true;
}/////////////////////////////////////////////////////////////////////////////
//
// ParseName
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::ParseName(CString p_Name)
{
CString l_RemoteName;
int l_Start; ZeroMemory(g_Server, sizeof g_Server);
memcpy(g_Server, p_Name, p_Name.GetLength()); if ((l_Start = p_Name.Find("\\\\", 0)) >= 0)
{
if (l_Start < p_Name.GetLength() - 1)
p_Name.Delete(l_Start, 2);
}
else
return; memcpy(g_Server, p_Name, p_Name.GetLength());
g_Server[p_Name.GetLength()] = 0; ZeroMemory(g_Share, sizeof g_Share);
memcpy(g_Share, p_Name, p_Name.GetLength());
if ((l_Start = p_Name.Find("\\")) >= 0)
p_Name.Delete (0, l_Start + 1);
memcpy(g_Share, p_Name, p_Name.GetLength());
g_Share[p_Name.GetLength()] = 0;
g_ShareName = p_Name;
}/////////////////////////////////////////////////////////////////////////////
//
// DisplayNetworkError
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::DisplayNetworkError(CNetService * p_pNetService)
{
CString l_ErrorMessage;
CString l_Title = "Network error: " +
p_pNetService->ErrorMessage(p_pNetService->Error()); if (p_pNetService->Error() == ERROR_EXTENDED_ERROR)
l_ErrorMessage = p_pNetService->NetErrorMessage() +
", " +
p_pNetService->NetErrorProvider();
else
l_ErrorMessage = p_pNetService->ServiceErrorMessage(p_pNetService->ServiceError());
MessageBox(l_Title,
l_ErrorMessage,
MB_OK);
}/////////////////////////////////////////////////////////////////////////////
//
// InitImageList
//
/////////////////////////////////////////////////////////////////////////////
void CWNetEnumDlg::InitImageList()
{
g_smallImageList.Create(16,16,FALSE,1,1); HICON l_Icon;
l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_Global));
g_smallImageList.Add (l_Icon); l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_Network));
g_smallImageList.Add (l_Icon); l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_Server));
g_smallImageList.Add (l_Icon); l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_FolderClosed));
g_smallImageList.Add (l_Icon); l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_FolderOpen));
g_smallImageList.Add (l_Icon); l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_Disk));
g_smallImageList.Add (l_Icon); l_Icon = LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_Print));
g_smallImageList.Add (l_Icon); m_treeList.SetImageList (&g_smallImageList, TVSIL_NORMAL);
}/////////////////////////////////////////////////////////////////////////////
谢谢!先送上20分。等通过后在送20分,好吗?我先试试。
找不到给分的地方,太弱至了!找到后在送上。