/// <summary> /// Returns a collection of destinations specified within the system. /// </summary> /// <returns>A DestinationInfo collection with details of all the destinations in the system.</returns> public DestinationInfoCollection EnumDestinations() { DestinationInfo destInfo = new DestinationInfo(); IntPtr hDestInfo = IntPtr.Zero; DestinationInfoCollection dests = new DestinationInfoCollection(); bool loop = true; int i = 0; int ret = 0; lock (syncRoot) { do { hDestInfo = MarshalEx.AllocHLocal((uint)DestinationInfo.NativeSize); ret = ConnMgrEnumDestinations(i++, hDestInfo); if (ret == -2147467259) { loop = false; break; } DestinationInfo cm = new DestinationInfo(hDestInfo); dests.Add(cm); MarshalEx.FreeHLocal(hDestInfo); }while(loop); } return(dests); }
private void FreeStructure() { if (_requestBuffer != IntPtr.Zero) { MarshalEx.FreeHLocal(_requestBuffer); _requestBuffer = IntPtr.Zero; } }
private void CleanFileName() { //dispose native string if (_vrd.lpszRecordFileName != IntPtr.Zero) { MarshalEx.FreeHLocal(_vrd.lpszRecordFileName); } }
/// <summary> /// Disposes of the ConnectionInfo object. /// </summary> public void Dispose() { this.hWnd = IntPtr.Zero; if (handle != IntPtr.Zero) { MarshalEx.FreeHLocal(handle); } }
/// <summary> /// Enumerates and returns all connected network resources. /// </summary> /// <returns>Array of NetworkResource class</returns> public static NetworkResource[] GetConnectedResources() { NETRESOURCE netRes = new NETRESOURCE(); IntPtr hEnum = IntPtr.Zero; int ret = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, 0, IntPtr.Zero, ref hEnum); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //Allocate memory for NETRESOURCE array int bufferSize = 16384; IntPtr buffer = MarshalEx.AllocHLocal(bufferSize); if (buffer == IntPtr.Zero) { throw new OutOfMemoryException("There's not enough native memory."); } uint c = 0xFFFFFFFF; int count = (int)c; int size = Marshal.SizeOf(typeof(NETRESOURCE)); ArrayList arrList = new ArrayList(); ret = WNetEnumResource(hEnum, ref count, buffer, ref bufferSize); if (ret == 0) { IntPtr currPtr = buffer; for (int i = 0; i < count; i++) { netRes = (NETRESOURCE)Marshal.PtrToStructure(currPtr, typeof(NETRESOURCE)); NetworkResource res = new NetworkResource(Marshal.PtrToStringUni(netRes.lpLocalName), Marshal.PtrToStringUni(netRes.lpRemoteName)); //res.RemoteName = Marshal.PtrToStringUni(netRes.lpRemoteName); //res.ShareName = Marshal.PtrToStringUni(netRes.lpLocalName); arrList.Add(res); currPtr = new IntPtr((int)currPtr + size); } } else { //clean up MarshalEx.FreeHLocal(buffer); } //clean up MarshalEx.FreeHLocal(buffer); return((NetworkResource[])arrList.ToArray(typeof(NetworkResource))); }
protected override void Dispose(bool disposing) { if (this._disposed) { return; } this._disposed = true; if (this._replyBuffer != IntPtr.Zero) { MarshalEx.FreeHLocal(_replyBuffer); _replyBuffer = IntPtr.Zero; } if (this._handlePingV4 != IntPtr.Zero) { IcmpCloseHandle(_handlePingV4); _handlePingV4 = IntPtr.Zero; } }
/// <summary> /// Enumerates network resources. /// </summary> /// <param name="remoteName">The name of the server</param> /// <returns>Array of NetworkResource class</returns> public static NetworkResource[] GetNetworkResources(string remoteName) { NETRESOURCE netRes = new NETRESOURCE(); netRes.dwScope = RESOURCE_GLOBALNET; netRes.dwType = RESOURCETYPE_DISK; netRes.dwUsage = RESOURCEUSAGE_CONTAINER; netRes.lpRemoteName = MarshalEx.StringToHGlobalUni(remoteName); netRes.lpLocalName = MarshalEx.StringToHGlobalUni(""); netRes.lpComment = IntPtr.Zero; netRes.lpProvider = IntPtr.Zero; IntPtr hEnum = IntPtr.Zero; int ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, netRes, ref hEnum); if (ret != 0) { throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //Allocate memory for NETRESOURCE array int bufferSize = 16384; IntPtr buffer = MarshalEx.AllocHLocal(bufferSize); if (buffer == IntPtr.Zero) { throw new OutOfMemoryException("There's not enough native memory."); } uint c = 0xFFFFFFFF; int count = (int)c; int size = Marshal.SizeOf(typeof(NETRESOURCE)); ArrayList arrList = new ArrayList(); ret = WNetEnumResource(hEnum, ref count, buffer, ref bufferSize); if (ret == 0) { IntPtr currPtr = buffer; for (int i = 0; i < count; i++) { netRes = (NETRESOURCE)Marshal.PtrToStructure(currPtr, typeof(NETRESOURCE)); NetworkResource res = new NetworkResource("", Marshal.PtrToStringUni(netRes.lpRemoteName)); //res.RemoteName = Marshal.PtrToStringUni(netRes.lpRemoteName); arrList.Add(res); currPtr = new IntPtr((int)currPtr + size); } } else { //clean up MarshalEx.FreeHLocal(buffer); throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString()); } //clean up MarshalEx.FreeHLocal(buffer); return((NetworkResource[])arrList.ToArray(typeof(NetworkResource))); }