Monday 15 September 2014

c# - WIN32API returns only current machine name -



c# - WIN32API returns only current machine name -

i want collect list of machines in local network, trying below code. homecoming me current machine name only. how can machines ip/name in local network?.

i have tried sv_101_types, among these returns empty list , remaining returns current machine name.

could guide me, wrong?

public class programme { static void main(string[] args) { arraylist machines = netapi32.getserverlist(netapi32.sv_101_types.sv_type_all); } } /// <summary> /// summary description class1. /// </summary> /// public class netapi32 { // constants public const uint error_success = 0; public const uint error_more_data = 234; public enum sv_101_types : uint { sv_type_workstation = 0x00000001, sv_type_server = 0x00000002, sv_type_sqlserver = 0x00000004, sv_type_domain_ctrl = 0x00000008, sv_type_domain_bakctrl = 0x00000010, sv_type_time_source = 0x00000020, sv_type_afp = 0x00000040, sv_type_novell = 0x00000080, sv_type_domain_member = 0x00000100, sv_type_printq_server = 0x00000200, sv_type_dialin_server = 0x00000400, sv_type_xenix_server = 0x00000800, sv_type_server_unix = 0x00000800, sv_type_nt = 0x00001000, sv_type_wfw = 0x00002000, sv_type_server_mfpn = 0x00004000, sv_type_server_nt = 0x00008000, sv_type_potential_browser = 0x00010000, sv_type_backup_browser = 0x00020000, sv_type_master_browser = 0x00040000, sv_type_domain_master = 0x00080000, sv_type_server_osf = 0x00100000, sv_type_server_vms = 0x00200000, sv_type_windows = 0x00400000, sv_type_dfs = 0x00800000, sv_type_cluster_nt = 0x01000000, sv_type_terminalserver = 0x02000000, sv_type_cluster_vs_nt = 0x04000000, sv_type_dce = 0x10000000, sv_type_alternate_xport = 0x20000000, sv_type_local_list_only = 0x40000000, sv_type_domain_enum = 0x80000000, sv_type_all = 0xffffffff }; [structlayout(layoutkind.sequential)] public struct server_info_101 { [marshalas(system.runtime.interopservices.unmanagedtype.u4)] public uint32 sv101_platform_id; [marshalas(system.runtime.interopservices.unmanagedtype.lpwstr)] public string sv101_name; [marshalas(system.runtime.interopservices.unmanagedtype.u4)] public uint32 sv101_version_major; [marshalas(system.runtime.interopservices.unmanagedtype.u4)] public uint32 sv101_version_minor; [marshalas(system.runtime.interopservices.unmanagedtype.u4)] public uint32 sv101_type; [marshalas(system.runtime.interopservices.unmanagedtype.lpwstr)] public string sv101_comment; }; public enum platform_id { platform_id_dos = 300, platform_id_os2 = 400, platform_id_nt = 500, platform_id_osf = 600, platform_id_vms = 700 } [dllimport("netapi32.dll", entrypoint = "netserverenum")] public static extern int netserverenum([marshalas(unmanagedtype.lpwstr)]string servername, int level, out intptr bufptr, int prefmaxlen, ref int entriesread, ref int totalentries, sv_101_types servertype, [marshalas(unmanagedtype.lpwstr)]string domain, intptr resume_handle); [dllimport("netapi32.dll", entrypoint = "netapibufferfree")] public static extern int netapibufferfree(intptr buffer); [dllimport("netapi32", charset = charset.unicode)] private static extern int netmessagebuffersend( string servername, string msgname, string fromname, string buf, int buflen); public static int netmessagesend(string servername, string messagename, string fromname, string strmsgbuffer, int imsgbufferlen) { homecoming netmessagebuffersend(servername, messagename, fromname, strmsgbuffer, imsgbufferlen * 2); } public static arraylist getserverlist(netapi32.sv_101_types servertype) { int entriesread = 0, totalentries = 0; arraylist alservers = new arraylist(); { // buffer store available servers // filled netserverenum function intptr buf = new intptr(); server_info_101 server; int ret = netserverenum(null, 101, out buf, -1, ref entriesread, ref totalentries, servertype, null, intptr.zero); // if function returned data, fill tree view if (ret == error_success || ret == error_more_data || entriesread > 0) { intptr ptr = buf; (int = 0; < entriesread; i++) { // cast pointer server_info_101 construction server = (server_info_101)marshal.ptrtostructure(ptr, typeof(server_info_101)); //cast pointer ulong add-on work on 32-bit or 64-bit systems. ptr = (intptr)((ulong)ptr + (ulong)marshal.sizeof(server)); // add together machine name , comment arraylist. //you homecoming entire construction here if desired alservers.add(server); } } // free buffer netapibufferfree(buf); } while ( entriesread < totalentries && entriesread != 0 ); homecoming alservers; } }

c# winapi

No comments:

Post a Comment