它的例子也给你:<br>#include <windows.h><br>#include <stdio.h><br>#include <string.h><br>#include <stdlib.h><br>#include <time.h><br>#define INCL_NET <br>#include <lan.h><br><br>// Constant definitions.<br><br>#define LEVEL_01 1<br>#define LEVEL_11 11<br>#define LEVEL_10 10<br>#define LEVEL_00 0<br><br>#define SMALL_BUFFER 1024<br>#define MEDIUM_BUFFER 4*1024<br>#define SERVER_NAME 50<br><br>// Buffer allocation performed globally for simplicity.<br><br>LPSTR bdcNames[SMALL_BUFFER];<br>LPSTR UserData[MEDIUM_BUFFER];<br>LPSTR WrkSta[SMALL_BUFFER];<br>LPSTR pdcName[SERVER_NAME];<br>LPSTR servername[SERVER_NAME]; <br><br>// Create a structure to hold the current server and <br>// the logon time so the values are together.<br><br>typedef struct svr_usr {<br> LPSTR server;<br> long logon_time;<br>} SVR_USR;<br><br>// Create typedefs for the larger structure names to <br>// shorten the number of characters to type.<br><br>typedef struct user_info_11 USER11;<br>typedef struct wksta_info_10 WORK10;<br>typedef struct wksta_info_1 WORK01;<br>typedef struct server_info_100 SERVER100;<br>typedef struct tm TIMER;<br><br>// Declare pointers so the buffers returned from the system<br>// functions can be cast to an appropriate value.<br><br>// Global variable descriptions.<br>// Users: Pointer to a USER_INFO_11 structure that contains user <br>// information about the username derived from the Wksta <br>// array. The element of interest is usri11_last_logon, the <br>// time that the server last validated the user's <br>// password.<br>// Wksta: Pointer to the WKSTA_INFO_10 structure that contains <br>// information about the WFW workstation. This structure is <br>// filled first, and the information placed there is used <br>// to get additional information. The domain name is used <br>// to get a list of BDCs that will be queried for user <br>// data. The username is the element that will qualify the <br>// user data request. The elements of interest are <br>// wki10_username, the current logged-on user, and <br>// wki10_logon_domain, the domain in which the user <br>// has logged on.<br>// Servers: List of BDCs for the user's logon domain. Each <br>// server in the array will be queried for information <br>// about the current logged-on user as described <br>// in the user's variable comments.<br>//<br><br>TIMER *lpTime;<br>USER11 far *Users;<br>WORK10 far *Wksta;<br>SERVER0 far *Servers;<br>WORK01 far *Wksta01;<br><br>char * lpszTime;<br><br>// Declare values for use with the network management functions.<br><br>unsigned short svrEntries;<br>unsigned short svrRead;<br>unsigned short usrEntries;<br>unsigned short wkstaEntries;<br><br>// Create a temporary variable to use as a loop control variable.<br><br>unsigned short i;<br><br>char Message[256];<br><br>// Define a global variable to hold the validating server <br>// and the time the user last logged on.<br><br>SVR_USR validate = { NULL, 0 };<br><br>// Create a global variable for the return value of the function; <br>// this is used in error checking.<br><br>DWORD netRet;<br><br>// Bogus WinMain so you can step through the sample <br>// in a 16-bit debugger.<br><br>int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)<br>HANDLE hInstance; // current instance <br>HANDLE hPrevInstance; // previous instance <br>LPSTR lpCmdLine; // command line <br>int nCmdShow; // show-window type (open/icon) <br>{<br> // First, get the workstation information calling <br> // the NetWkstaGetInfo function at level 10. <br> // This will return the domain in which the user logged on.<br><br> netRet = NetWkstaGetInfo( NULL, // This workstation<br> LEVEL_10, // Information requested<br> (char far *)WrkSta, // Buffer<br> SMALL_BUFFER, <br> &wkstaEntries); // Expecting only 1 entry<br> <br> if( netRet != NERR_Success )<br> {<br> // A network error occurred. Print it to the stdout.<br><br> sprintf(Message,"ERROR: NetWkstaGetInfo API Failed. Error Code: %d/n", netRet);<br> MessageBox( NULL, Message, "NetWkstaGetInfo Error", MB_OK);<br> return(0);<br> }<br><br> // Now we must retrieve the BDCs and the PDC to check <br> // when the last user logon was validated. <br> //<br> // The argument list is set as follows:<br> // Execute on the local machine ( NULL )<br> // Pass the domain name of interest (Wksta[0].wki10_logon_domain)<br> // Pass a buffer to receive the PDC name<br> // Pass the size of the PDC name buffer<br> <br> Wksta = (WORK10 *)WrkSta;<br> netRet = NetGetDCName( NULL, <br> Wksta[0].wki10_logon_domain,<br> (char far *)pdcName,<br> SERVER_NAME);<br> if(netRet != NERR_Success )<br> {<br> // Could not locate a PDC. Something is wrong; end the program.<br><br> sprintf(Message,"ERROR: NetGetDCName API Failed. Error Code: %d/n", netRet);<br> MessageBox( NULL, Message, "NetGetDCName Error", MB_OK);<br> return(0);<br> }<br><br> // We have the PDC. Now, query for the user information, <br> // and store it temporarily for comparison to the BDC's data.<br><br> netRet = NetUserGetInfo( (char far *)pdcName, // Execute on the PDC<br> Wksta[0].wki10_username, // User's name<br> LEVEL_11,<br> (char far *)UserData, // Put structures here<br> SMALL_BUFFER,<br> &usrEntries); // Expecting only one entry<br> if( netRet != NERR_Success )<br> {<br> // No user account on the PDC. End the program.<br><br> sprintf(Message,"ERROR: NetUserGetInfo API Failed on PDC. Error Code: %d/n", netRet);<br> MessageBox( NULL, Message, "NetUserGetInfo Error", MB_OK);<br> return(0);<br> }<br><br> // Set the structure so the PDC is the starting validating server.<br><br> validate.server = (char far *)pdcName;<br> Users = (USER11 * )UserData;<br> validate.logon_time = Users[0].usri11_last_logon;<br><br> // Now retrieve all of the BDCs.<br><br> netRet = NetServerEnum2( NULL,<br> LEVEL_00,<br> (char far *)bdcNames,<br> SMALL_BUFFER,<br> &svrRead,<br> &svrEntries,<br> SV_TYPE_DOMAIN_BAKCTRL,<br> Wksta[0].wki10_logon_domain);<br> <br> if( netRet != NERR_Success )<br> {<br> // No BDCs. This could be an error.<br><br> sprintf(Message,"ERROR: NetServerEnum2 API Failed. Error Code: %d", netRet);<br> MessageBox( NULL, Message, "NetServerEnum2", MB_OK);<br> return( 0 );<br> }<br><br> // We have a list of BDCs. <br> // If svrEntries > 1<br> // loop through the list, checking the last logon against the<br> // current logon stored in validate.logon_time.<br><br> Servers =( SERVER0 * )bdcNames;<br> for( i = 0; i < svrEntries; i++ )<br> {<br> // Must add the // to the names returned from the NetServerEnum function.<br><br> _fstrcpy( (LPSTR)servername,"////"
;<br> _fstrcat( (LPSTR)servername,Servers
.sv0_name);<br> netRet = NetUserGetInfo( (LPSTR)servername, // Execute on a BDC<br> Wksta[0].wki10_username, // User's name<br> LEVEL_11,<br> (char far *)UserData,<br> SMALL_BUFFER,<br> &usrEntries); // Expecting only one entry<br> Users = (USER11 *)UserData;<br> if( netRet == NERR_Success )<br> { <br> // We found a user entry on this BDC. Compare the<br> // last logon time to the stored time. Replace the stored <br> // time if the time is greater.<br> //<br> // Replace the servername so the time and the server match.<br> <br> if( Users[0].usri11_last_logon > validate.logon_time)<br> {<br> validate.server = (char far *)&Servers;<br> validate.logon_time = Users[0].usri11_last_logon;<br> } <br> else if( Users[0].usri11_last_logon == validate.logon_time )<br> { <br> // This could indicate a problem.<br><br> sprintf(Message,"Values are the same. %ls %ls", Servers.sv0_name, validate.server);<br> MessageBox( NULL, Message, "HMMMM...........",MB_OK);<br> }<br> }<br> } <br><br> // Convert the time in seconds to a time structure for display; <br> // build the output string.<br><br> lpTime = gmtime( &validate.logon_time);<br> lpszTime = asctime( lpTime );<br> _fstrcpy((LPSTR)Message, (LPSTR)"Username: "<br> _fstrcat((LPSTR)Message, (LPSTR)Wksta[0].wki10_username);<br> _fstrcat((LPSTR)Message, (LPSTR)"/nLast Logon: "<br> _fstrcat((LPSTR)Message, (LPSTR)lpszTime);<br> _fstrcat((LPSTR)Message, (LPSTR)"Logon Server: "<br> _fstrcat((LPSTR)Message, (LPSTR)validate.server);<br><br> // Display the information.<br><br> MessageBox(NULL,Message,"Logon Validation Information", MB_OK);<br> return(0);<br>}