NetFileEnum返回ERROR_MORE_DATA

时间:2017-01-04 15:55:50

标签: c# pinvoke netapi32

尝试从文件服务器检索大量打开文件会返回ERROR_MORE_DATA值(错误号234),但在处理一小部分文件时似乎工作正常(似乎返回大约84个条目)。此代码基于以下示例:http://pinvoke.net/default.aspx/netapi32/NetFileEnum.html

我发现的大多数示例都没有涉及如何处理大量文件。根据我的理解,这与resume_handle有关,但是我不确定需要做什么。我是否需要以某种方式在循环中调用此方法?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace OpenFiles
{
    class Program
    {
        public static string computername = "computername";
        static void Main(string[] args)
        {

            List<string> theFileList = NativeMethods.GetFiles(computername);
            foreach (string file in theFileList)
            {
                Console.WriteLine(file);
            }
        }
    }

    static class NativeMethods
    {

        [DllImport("netapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern int NetFileEnum(
            string servername,
            string basepath,
            string username,
            int level,
            ref IntPtr bufptr,
            int prefmaxlen,
            out int entriesread,
            out int totalentries,
            IntPtr resume_handle
        );

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct FILE_INFO_3
        {
            public int fi3_id;
            public int fi3_permission;
            public int fi3_num_locks;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string fi3_pathname;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string fi3_username;
        }

        [DllImport("Netapi32.dll", SetLastError = true)]
        static extern int NetApiBufferFree(IntPtr Buffer);

        public static List<string> GetFiles(string Computername)
        {
            const int MAX_PREFERRED_LENGTH = -1;

            int dwReadEntries;
            int dwTotalEntries;
            IntPtr pBuffer = IntPtr.Zero;
            FILE_INFO_3 pCurrent = new FILE_INFO_3();
            List<string> fileList = new List<string>();

            int dwStatus = NetFileEnum(Computername, null, null, 3, ref pBuffer, MAX_PREFERRED_LENGTH, out dwReadEntries, out dwTotalEntries, IntPtr.Zero);

            if (dwStatus == 0)
            {

                for (int dwIndex = 0; dwIndex < dwReadEntries; dwIndex++)
                {

                    IntPtr iPtr = new IntPtr(pBuffer.ToInt32() + (dwIndex * Marshal.SizeOf(pCurrent)));
                    pCurrent = (FILE_INFO_3)Marshal.PtrToStructure(iPtr, typeof(FILE_INFO_3));

                    string fileInfo = pCurrent.fi3_id + "," + 
                        pCurrent.fi3_num_locks + "," + 
                        pCurrent.fi3_pathname + "," + 
                        pCurrent.fi3_permission + "," + 
                        pCurrent.fi3_username;

                    fileList.Add(fileInfo);

                }

                NetApiBufferFree(pBuffer);

            }
            else
            {
                Console.WriteLine("error: " + dwStatus);
            }
            return fileList;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在我有限的经验中,大量的结果可能比最大缓冲区大。在这种情况下,会给出更多的数据响应,并指示我们使用提供的简历句柄再次调用。在您的示例中,不会更改resume句柄,因为DllImport签名未将其定义为out参数。使用第一次调用的恢复句柄结果(传递零意味着第一次调用)允许您接收下一批次。循环直到收到成功回复或其他错误。

请务必解决定义NetFileEnum签名的问题。 恢复句柄未定义为out,因此无法通过被调用函数进行更改。

请尝试使用以下DLL导入签名:

[DllImport("netapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern int NetFileEnum(
    string servername,
    string basepath,
    string username,
    int level,
    ref IntPtr bufptr,
    int prefmaxlen,
    out int entriesread,
    out int totalentries,
    out IntPtr resume_handle
);

当您获得更多数据响应时,您应该能够多次使用生成的简历句柄重新调用NetFileEnum。