I'm trying to use various APIs with C#, in particular using APIs that return
structs and make use of pointers such as LPBYTE, LPDWORD etc. In particular
I'm trying to use the API NET_API_STATUS NetWkstaUserEnum(LPWSTR servername,
DWORD level, LPBYTE *bufptr, DWORD prefmaxlen, LPDWORD entriesread,
LPDWORD totalentries, LPDWORD resumehandle );, but having great difficulty
trying to return the data. Below is some of the code written so far.
private void Form1_Load(object sender, System.EventArgs e)
{
listView1.Columns.Add("Username", -2,
HorizontalAlignment.Left);
listView1.Columns.Add("Domain", -2,
HorizontalAlignment.Left);
listView1.Columns.Add("Machine", -2,
HorizontalAlignment.Left);
listView1.View = View.Details;
}
[DllImport("netapi32.dll", CharSet=CharSet.Auto)]
static extern int NetWkstaUserEnum(StringBuilder servername,
int level,
ref int bufptr,
int prefmaxlen,
ref int entriesread,
ref int totalentries,
ref int resume_handle);
[DllImport("netapi32.dll")]
static extern int NetApiBufferFree(int Buffer);
// for use on Win NT/2000 only
[StructLayout(LayoutKind.Sequential)]
public struct WKSTA_USER_INFO_0
{
public int wkui0_username;
}
[StructLayout(LayoutKind.Sequential)]
public struct WKSTA_USER_INFO_1
{
public int wkui1_username;
public int wkui1_logon_domain;
public int wkui1_oth_domains;
public int wkui1_logon_server;
}
private const int MAX_PREFERRED_LENGTH = -1;
private const int NERR_SUCCESS = 0x0;
private const int ERROR_MORE_DATA = 0x234;
private void button1_Click(object sender, System.EventArgs
e)
{
StringBuilder strServer = new
StringBuilder(textBox1.Text);
int bufptr = 0;
int dwEntriesread = 0;
int dwTotalentries = 0;
int dwResumehandle = 0;
int nStatus;
int nStructSize;
WKSTA_USER_INFO_1 wui1 = new WKSTA_USER_INFO_1();
this.listView1.Items.Clear();
do
{
nStatus = NetWkstaUserEnum(
strServer,
1,
ref bufptr,
MAX_PREFERRED_LENGTH,
ref dwEntriesread,
ref dwTotalentries,
ref dwResumehandle
);
//
// If the call succeeds,
//
if ((nStatus == NERR_SUCCESS) | (nStatus ==
ERROR_MORE_DATA))
{
if (dwEntriesread > 0)
{
//
// Loop through the entries.
//
for (int i = 0; (i <
dwEntriesread); i++)
{
nStructSize =
Marshal.SizeOf(wui1);
Marshal.PtrToStructure((System.IntPtr) bufptr, wui1);
MessageBox.Show(wui1.wkui1_username);
}
}
else
{
MessageBox.Show("A system
error has occurred : " + nStatus);
}
}
}
while(nStatus == ERROR_MORE_DATA);
NetApiBufferFree(bufptr);
}
}
I tried to use the platform documentation as template, but failed miserably.
If anyone can help or point me in the direction of a good book it would be
much appreciated.
Simon
----------------------------------------------------------------------
If you have received this e-mail in error or wish to read our e-mail
disclaimer statement and monitoring policy, please refer to
http://www.drkw.com/disc/email/ or contact the sender.
----------------------------------------------------------------------
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.