source: branches/eraser6/6.0/Eraser.Util/NetApi.cs @ 1830

Revision 1830, 4.7 KB checked in by lowjoel, 4 years ago (diff)

Wrong parameter order for Marshal.Copy, resulting in the manager buffer being copied to the unmanaged buffer.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Runtime.InteropServices;
26
27namespace Eraser.Util
28{
29    public static class NetApi
30    {
31        /// <summary>
32        /// The NetStatisticsGet function retrieves operating statistics for a service.
33        /// Currently, only the workstation and server services are supported.
34        /// </summary>
35        /// <param name="server">Pointer to a string that specifies the DNS or
36        /// NetBIOS name of the server on which the function is to execute. If
37        /// this parameter is NULL, the local computer is used.</param>
38        /// <param name="service">Pointer to a string that specifies the name of
39        /// the service about which to get the statistics. Only the values
40        /// SERVICE_SERVER and SERVICE_WORKSTATION are currently allowed.</param>
41        /// <param name="level">Specifies the information level of the data.
42        /// This parameter must be 0.</param>
43        /// <param name="options">This parameter must be zero.</param>
44        /// <param name="bufptr">Pointer to the buffer that receives the data. The
45        /// format of this data depends on the value of the level parameter.
46        /// This buffer is allocated by the system and must be freed using the
47        /// NetApiBufferFree function. For more information, see Network Management
48        /// Function Buffers and Network Management Function Buffer Lengths.</param>
49        /// <returns>If the function succeeds, the return value is NERR_Success.
50        ///
51        /// If the function fails, the return value is a system error code. For
52        /// a list of error codes, see System Error Codes.</returns>
53        public static byte[] NetStatisticsGet(string server, NetApiService service,
54            uint level, uint options)
55        {
56            IntPtr netApiStats = IntPtr.Zero;
57            string serviceName = "Lanman" + service.ToString();
58            if (NativeMethods.NetStatisticsGet(server, serviceName, level, options, out netApiStats) == 0)
59            {
60                try
61                {
62                    //Get the size of the buffer
63                    uint size = 0;
64                    NativeMethods.NetApiBufferSize(netApiStats, out size);
65                    byte[] result = new byte[size];
66
67                    //Copy the buffer
68                    Marshal.Copy(netApiStats, result, 0, result.Length);
69
70                    //Return the result
71                    return result;
72                }
73                finally
74                {
75                    //Free the statistics buffer
76                    NativeMethods.NetApiBufferFree(netApiStats);
77                }
78            }
79
80            return null;
81        }
82
83        internal static class NativeMethods
84        {
85            [DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
86            public static extern uint NetStatisticsGet(string server, string service,
87                uint level, uint options, out IntPtr bufptr);
88
89            /// <summary>
90            /// The NetApiBufferSize function returns the size, in bytes, of a buffer
91            /// allocated by a call to the NetApiBufferAllocate function.
92            /// </summary>
93            /// <param name="Buffer">Pointer to a buffer returned by the NetApiBufferAllocate
94            /// function.</param>
95            /// <param name="ByteCount">Receives the size of the buffer, in bytes.</param>
96            /// <returns>If the function succeeds, the return value is NERR_Success.
97            ///
98            /// If the function fails, the return value is a system error code. For
99            /// a list of error codes, see System Error Codes.</returns>
100            [DllImport("Netapi32.dll")]
101            public static extern uint NetApiBufferSize(IntPtr Buffer, out uint ByteCount);
102
103            /// <summary>
104            /// The NetApiBufferFree function frees the memory that the NetApiBufferAllocate
105            /// function allocates. Call NetApiBufferFree to free the memory that other
106            /// network management functions return.
107            /// </summary>
108            /// <param name="Buffer">Pointer to a buffer returned previously by another
109            /// network management function.</param>
110            /// <returns>If the function succeeds, the return value is NERR_Success.
111            ///
112            /// If the function fails, the return value is a system error code. For
113            /// a list of error codes, see System Error Codes.</returns>
114            [DllImport("Netapi32.dll")]
115            public static extern uint NetApiBufferFree(IntPtr Buffer);
116
117            private const uint NERR_Success = 0;
118        }
119    }
120
121    public enum NetApiService
122    {
123        Workstation,
124        Server
125    }
126}
Note: See TracBrowser for help on using the repository browser.