public static int DDGetNetUserList(string licence_name, out int num_net_users, byte[] nu_info_bytes, int num_info_structs, out int extended_error) { int ret_code = -1; extended_error = num_net_users = 0; if (IntPtr.Size == 4) { try { ret_code = DDGetNetUserList32.DDGetNetUserList(licence_name, out num_net_users, nu_info_bytes, num_info_structs, out extended_error); } catch (DllNotFoundException) { Console.WriteLine("Error! Cannot find dpwin32.dll. This should be in the same folder as DllTest."); } } else { try { ret_code = DDGetNetUserList64.DDGetNetUserList(licence_name, out num_net_users, nu_info_bytes, num_info_structs, out extended_error); } catch (DllNotFoundException) { Console.WriteLine("Error! Cannot find dpwin64.dll. This should be in the same folder as DllTest."); } } return(ret_code); }
public static int DDGetNetUserList(string licence_name, out int num_net_users, out NU_INFO[] nu_info, int num_info_structs, out int extended_error) { int ret_code = -1; int i; extended_error = num_net_users = 0; byte[] nu_info_bytes = new byte[num_info_structs * Marshal.SizeOf(typeof(NU_INFO))]; byte[] nu_info_one_struct = new byte[Marshal.SizeOf(typeof(NU_INFO))]; GCHandle MyGC; nu_info = null; // call the function and get info as a byte array if (IntPtr.Size == 4) { ret_code = DDGetNetUserList32.DDGetNetUserList(licence_name, out num_net_users, nu_info_bytes, num_info_structs, out extended_error); } else { ret_code = DDGetNetUserList64.DDGetNetUserList(licence_name, out num_net_users, nu_info_bytes, num_info_structs, out extended_error); } if (ret_code != 0) { return(ret_code); } // convert byte array to an array of structs nu_info = new NU_INFO[num_net_users]; for (i = 0; i < num_net_users; i++) { Array.Copy(nu_info_bytes, i * Marshal.SizeOf(typeof(NU_INFO)), nu_info_one_struct, 0, Marshal.SizeOf(typeof(NU_INFO))); MyGC = GCHandle.Alloc(nu_info_one_struct, GCHandleType.Pinned); nu_info[i] = (NU_INFO)Marshal.PtrToStructure(MyGC.AddrOfPinnedObject(), typeof(NU_INFO)); MyGC.Free(); } return(0); }