各位大侠,能否给个有关使用NetWkstaUserEnum的实例(100分)

  • 主题发起人 主题发起人 yeeler
  • 开始时间 开始时间
#ifndef UNICODE<br>#define UNICODE<br>#endif<br><br>#include &lt;stdio.h&gt;<br>#include &lt;assert.h&gt;<br>#include &lt;windows.h&gt; <br>#include &lt;lm.h&gt;<br><br>int wmain(int argc, wchar_t *argv[])<br>{<br>&nbsp; &nbsp;LPWKSTA_USER_INFO_0 pBuf = NULL;<br>&nbsp; &nbsp;LPWKSTA_USER_INFO_0 pTmpBuf;<br>&nbsp; &nbsp;DWORD dwLevel = 0;<br>&nbsp; &nbsp;DWORD dwPrefMaxLen = -1;<br>&nbsp; &nbsp;DWORD dwEntriesRead = 0;<br>&nbsp; &nbsp;DWORD dwTotalEntries = 0;<br>&nbsp; &nbsp;DWORD dwResumeHandle = 0;<br>&nbsp; &nbsp;DWORD i;<br>&nbsp; &nbsp;DWORD dwTotalCount = 0;<br>&nbsp; &nbsp;NET_API_STATUS nStatus;<br>&nbsp; &nbsp;LPTSTR pszServerName = NULL;<br><br>&nbsp; &nbsp;if (argc &gt; 2)<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; fwprintf(stderr, L"Usage: %s [////ServerName]/n", argv[0]);<br>&nbsp; &nbsp; &nbsp; exit(1);<br>&nbsp; &nbsp;}<br>&nbsp; &nbsp;// The server is not the default local computer.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if (argc == 2)<br>&nbsp; &nbsp; &nbsp; pszServerName = argv[1];<br>&nbsp; &nbsp;fwprintf(stderr, L"/nUsers currently logged on %s:/n", pszServerName);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Call the NetWkstaUserEnum function, specifying level 0.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;do // begin do<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; nStatus = NetWkstaUserEnum(pszServerName,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dwLevel,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(LPBYTE*)&amp;pBuf,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dwPrefMaxLen,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;dwEntriesRead,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;dwTotalEntries,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;dwResumeHandle);<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // If the call succeeds,<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((pTmpBuf = pBuf) != NULL)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through the entries.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; (i &lt; dwEntriesRead); i++)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;assert(pTmpBuf != NULL);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (pTmpBuf == NULL)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Only members of the Administrators local group<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;can successfully execute NetWkstaUserEnum<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;locally and on a remote server.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fprintf(stderr, "An access violation has occurred/n");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Print the user logged on to the workstation. <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wprintf(L"/t-- %s/n", pTmpBuf-&gt;wkui0_username);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pTmpBuf++;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dwTotalCount++;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // Otherwise, indicate a system error.<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fprintf(stderr, "A system error has occurred: %d/n", nStatus);<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // Free the allocated memory.<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; if (pBuf != NULL)<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NetApiBufferFree(pBuf);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pBuf = NULL;<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp;}<br>&nbsp; &nbsp;// <br>&nbsp; &nbsp;// Continue to call NetWkstaUserEnum while <br>&nbsp; &nbsp;// &nbsp;there are more entries. <br>&nbsp; &nbsp;// <br>&nbsp; &nbsp;while (nStatus == ERROR_MORE_DATA); // end do<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Check again for allocated memory.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if (pBuf != NULL)<br>&nbsp; &nbsp; &nbsp; NetApiBufferFree(pBuf);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Print the final count of workstation users.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;fprintf(stderr, "/nTotal of %d entries enumerated/n", dwTotalCount);<br><br>&nbsp; &nbsp;return 0;<br>}<br>----<br>上述代码编译通过。<br><br>
 
jyniu有没有用delphi写的,多谢了。:-)
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp; WKSTA_USER_INFO_0 = record<br>&nbsp; &nbsp; wkui0_username:PWideChar;<br>&nbsp; end;<br><br>&nbsp; PWKSTA_USER_INFO_0 = ^WKSTA_USER_INFO_0;<br><br>&nbsp; WKSTA_USER_INFO_1 = record<br>&nbsp; &nbsp; wkui1_username:PWideChar;<br>&nbsp; &nbsp; wkui1_logon_domain:PWideChar;<br>&nbsp; &nbsp; wkui1_oth_domains:PWideChar;<br>&nbsp; &nbsp; wkui1_logon_server:PWideChar;<br>&nbsp; end;<br><br>&nbsp; PWKSTA_USER_INFO_1 = ^WKSTA_USER_INFO_1;<br><br>&nbsp; function NetWkstaUserEnum( servername:PWideChar; level:DWORD; var bufptr:Pointer;<br>&nbsp; &nbsp; &nbsp; &nbsp; prefmaxlen: DWORD; var entriesread, totalentries, resumehandle:DWORD ): DWORD; cdecl;<br>&nbsp; &nbsp; &nbsp; &nbsp; external 'netapi32.dll' name 'NetWkstaUserEnum';<br><br>&nbsp; function NetApiBufferFree( bufptr:Pointer ): DWORD; cdecl;<br>&nbsp; &nbsp; &nbsp; &nbsp; external 'netapi32.dll' name 'NetApiBufferFree';<br><br>var<br>&nbsp; Form1: TForm1;<br><br><br><br>implementation<br><br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; wui:PWKSTA_USER_INFO_0;<br>&nbsp; dwPrefMaxLen:DWORD;<br>&nbsp; dwEntriesRead:DWORD;<br>&nbsp; dwTotalEntries:DWORD;<br>&nbsp; dwResumeHandle:DWORD;<br>&nbsp; rc: DWORD;<br>begin<br>&nbsp; dwPrefMaxLen := $FFFFFFFF;<br>&nbsp; dwEntriesRead := 0;<br>&nbsp; dwTotalEntries := 0;<br>&nbsp; dwResumeHandle := 0;<br><br>&nbsp; repeat<br>&nbsp; &nbsp; rc := NetWkstaUserEnum( nil, 0, pointer(wui), dwPrefMaxLen, dwEntriesRead,<br>&nbsp; &nbsp; &nbsp; &nbsp; dwTotalEntries, dwResumeHandle );<br><br>&nbsp; &nbsp; if (rc = 0) or (rc = ERROR_MORE_DATA) then begin<br><br>&nbsp; &nbsp; &nbsp; &nbsp; ShowMessage( wui.wkui0_username );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; NetApiBufferFree( wui );<br>&nbsp; &nbsp; end;<br>&nbsp; until (rc &lt;&gt; 0) and (rc &lt;&gt; ERROR_MORE_DATA);<br><br>end;<br><br>end.<br>--<br>细节自己再修正, OK!
 
非常感谢您的帮助。以后有问题可以直接向您请教吗?(我在nt下编译上述C代码时,出错)<br>我的E_Mail:Yeeler@21cn.com
 
接受答案了.
 
后退
顶部