internal IntPtr ToUniStr(bool allocateFromHeap) { IntPtr ptr; EnsureNotDisposed(); // Demand Permission //new CryptographicPermission( CryptographicPermissionFlags.Decrypt ).Demand(); if (allocateFromHeap) { ptr = MarshalEx.AllocHGlobal((m_length + 1) * 2); } else { ptr = MarshalEx.AllocHLocal((m_length + 1) * 2); //ptr = MarshalEx.AllocCoTaskMem( (m_length+1) * 2 ); } try { this.UnProtectMemory(); Win32Native.memset(ptr, 0, (uint)((m_length + 1) * 2)); //Win32Native.ZeroMemory( ptr, (uint)((m_length+1) * 2) ); Marshal.Copy(m_buffer, 0, ptr, m_length * 2); return(ptr); } finally { // Assert Permission //new CryptographicPermission( CryptographicPermissionFlags.Encrypt ).Assert(); ProtectMemory(); } }
/// <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); }
/// <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))); }
/// <summary> /// Writes the ConnectionInfo data to unmanaged memory. /// </summary> /// <returns>A pointer to the unmanaged memory block storing the ConnectionInfo data</returns> public IntPtr ToPtr() { IntPtr p = MarshalEx.AllocHLocal((uint)Marshal.SizeOf(typeof(ConnectionInfo))); MarshalEx.WriteInt32(p, 0, this.cbSize); MarshalEx.WriteInt32(p, 4, this.dwParams); MarshalEx.WriteInt32(p, 8, this.dwFlags); MarshalEx.WriteInt32(p, 12, this.dwPriority); MarshalEx.WriteBool(p, 16, this.bExclusive); MarshalEx.WriteBool(p, 20, this.bDisabled); MarshalEx.WriteByteArray(p, 24, this.guidDestNet.ToByteArray()); MarshalEx.WriteIntPtr(p, 40, this.hWnd); MarshalEx.WriteInt32(p, 44, this.uMsg); MarshalEx.WriteInt32(p, 48, this.lParam); MarshalEx.WriteInt32(p, 52, this.ulMaxCost); MarshalEx.WriteInt32(p, 56, this.ulMinRcvBw); MarshalEx.WriteInt32(p, 60, this.ulMaxConnLatency); handle = p; return(p); }
private PingReply InternalSend(IPAddress address, byte [] buffer, int timeout, PingOptions options) { if (_handlePingV4 == IntPtr.Zero) { _handlePingV4 = IcmpCreateFile(); } if (_replyBuffer == IntPtr.Zero) { _replyBuffer = MarshalEx.AllocHLocal(0xffff); } IPOptions ipo = new IPOptions(options); InitStructure(buffer); IcmpSendEcho2(_handlePingV4, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, (uint)address.Address, _requestBuffer, (ushort)buffer.Length, ref ipo, _replyBuffer, 0xffff, (uint)timeout); FreeStructure(); IcmpEchoReply reply = (IcmpEchoReply)Marshal.PtrToStructure(this._replyBuffer, typeof(IcmpEchoReply)); return(new PingReply(reply)); }
private void InitStructure(byte[] buffer) { _requestBuffer = MarshalEx.AllocHLocal(buffer.Length); Marshal.Copy(buffer, 0, _requestBuffer, buffer.Length); }
/// <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))); }