Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Getting a return value of 50(The request is not supported.). The MSDN documentation says this should only happen if the computer is not > 2000. These are XP machines that I'm querying.
Any ideas?

http://msdn.microsoft.com/en-us/library/dd877207%28v=vs.85%29.aspx[^]

C#
var computer_name = "";
    int entry_count;
    string[] computer_names;
    var ret = PInvoke.NetEnumerateComputerNames(host, NET_COMPUTER_NAME_TYPE.NET_ALL_COMPUTER_NAMES, 0, out entry_count, out computer_names);
    if (PInvoke.ERROR_SUCCESS == ret && entry_count > 0)
    {
        computer_name = computer_names[0];
    }
    else
    {
        if (Config.Debug)
            Utilities.ReportError("Getting computer name for " + host + " failed: " + new Win32Exception(ret).Message, false, true);
    }
    return computer_name;
}
catch (Exception e)
{
    if (Config.Debug)
        Utilities.ReportError("Getting computer name for " + host + " failed: " + e.Message, false, true);
    return "";
}
Posted
Comments
Dave Kreskowiak 10-Jun-11 13:12pm    
Is the account you're running this code under an admin on the target machine?
[no name] 10-Jun-11 14:16pm    
Yes. He is a domain admin. We are connecting ipc$ share. This command should work with null sessions, though.
Sergey Alexandrovich Kryukov 10-Jun-11 14:08pm    
Where is your declaration of PInvoke?
Where is your exception dump? Don't forget about Exceptions.Stack and InnerExceptions (all of them, recursively).
--SA
[no name] 10-Jun-11 14:17pm    
No exceptions are being thrown. ret == 50. 0 results, invalid pointer for the last arg.
This does work on some systems. I think these systems are non-XP, but it could just be the configuration of one XP system which we cloned several times for VMs.
[DllImport("Netapi32.dll", SetLastError = true)]
public static extern int NetEnumerateComputerNames(
[MarshalAs(UnmanagedType.LPWStr)]
string server_name,
NET_COMPUTER_NAME_TYPE name_type,
int reserved,
out int entry_count,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)]
out string[] computer_names
);
Kim Togo 12-Jun-11 12:19pm    
In the MSDN doc for NetEnumerateComputerNames it says:

Remarks
The NetEnumerateComputerNames function is supported on Windows Vista and later.

So no Windows XP support on that.

Two answers. Okay. So this is a straight up documentation fail on MSDN's part ... 3 times. I'm going to write them to tell them the should self-fornicate.

It says it will not work in Windows 2000 or earlier. It says that it will not work in anything before Vista. It says that the earliest supported version is Server 2003. Idiots.

The solution, other than sterilization, is in the next answer.
 
Share this answer
 
Here's the proper way to do this.

http://msdn.microsoft.com/en-us/library/aa370663%28v=vs.85%29.aspx[^]

This says it will work in Server 2000 and above; but we all know, now, how much we can really trust Microsoft.

C#
IntPtr buffer;
var ret = PInvoke.NetWkstaGetInfo(host, 100, out buffer);
var strut_size = Marshal.SizeOf(typeof (WKSTA_INFO_100));
WKSTA_INFO_100 wksta_info;
if (PInvoke.ERROR_SUCCESS == ret)
{
    wksta_info = (WKSTA_INFO_100) Marshal.PtrToStructure(buffer, typeof (WKSTA_INFO_100));
    if (!string.IsNullOrEmpty(wksta_info.computer_name))
        return wksta_info.computer_name;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900