0

SERVER_INFO_101 の公式の例のコードを実行しようとしていますが、一部の LAN PC で常に「アクセスが拒否されました」というエラーが発生します (それらの約 50%)。

https://docs.microsoft.com/en-us/windows/win32/api/lmserver/nf-lmserver-netservergetinfo

#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "netapi32.lib")

#include <stdio.h>
#include <windows.h> 
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwLevel = 101;
   LPSERVER_INFO_101 pBuf = NULL;
   NET_API_STATUS nStatus;
   LPTSTR pszServerName = NULL;

   if (argc > 2)
   {
      fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);
      exit(1);
   }
   // The server is not the default local computer.
   //
   if (argc == 2)
      pszServerName = (LPTSTR) argv[1];
   //
   // Call the NetServerGetInfo function, specifying level 101.
   //
   nStatus = NetServerGetInfo(pszServerName,
                              dwLevel,
                              (LPBYTE *)&pBuf);
   //
   // If the call succeeds,
   //
   if (nStatus == NERR_Success)
   {
      //
      // Check for the type of server.
      //
      if ((pBuf->sv101_type & SV_TYPE_DOMAIN_CTRL) ||
         (pBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL) ||
         (pBuf->sv101_type & SV_TYPE_SERVER_NT))
         printf("This is a server\n");
      else
         printf("This is a workstation\n");
   }
   //
   // Otherwise, print the system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      NetApiBufferFree(pBuf);

   return 0;
}

しかし、C# を使用して EQUIVALENT コードを実行すると、プログラムはすべての LAN PC で問題なく実行されます。

public class NetServerGetInfoLib
    {
        #region Win32 API Interfaces
        [DllImport("Netapi32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int NetServerGetInfo(
            string serverName,
            int level,
            out IntPtr pSERVER_INFO_XXX);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        private struct SERVER_INFO_101
        {
            public int PlatformId;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string Name;
            public int VersionMajor;
            public int VersionMinor;
            public int Type;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string Comment;
        }

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


        public static void GetServerInfo(Boolean debugMode, string serverName, StringBuilder sb)
        {
            try
            {
                IntPtr pSI = IntPtr.Zero;
                SERVER_INFO_101 serverInfo;
                try
                {
                    if (NetServerGetInfo(serverName, 101, out pSI) == 0)
                    {
                        serverInfo = (SERVER_INFO_101)Marshal.PtrToStructure(pSI, typeof(SERVER_INFO_101));
                        sb.AppendFormat("Platform = {0} {1}.{2}", (PlataformID)serverInfo.PlatformId, serverInfo.VersionMajor, serverInfo.VersionMinor).AppendLine();
                        sb.AppendFormat("ServerType = {0} ", (ServerType)serverInfo.Type).AppendLine();
                        if (serverInfo.Comment != null)
                        {
                            string comment = serverInfo.Comment.Trim();
                            if (comment.Length > 0)
                            {
                                sb.AppendFormat("Comment = {0} ", comment).AppendLine();
                            }
                        }
                    }
                }
                finally
                {
                    NetApiBufferFree(pSI);
                }

            }
            catch (Exception ex)
            {
                if (debugMode)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }

    }

C++ ではパーミッション エラーが発生するのに、C# ではエラーが発生しないのはなぜですか?

4

0 に答える 0