21. FAQ o Win API |
Q> Who on a network or locally is rummaged on mine (not by mine) the machine? A> //------------------------------------------------------ //Who.exe (c) 1999 Serge Andyk asvzzz@chat.ru //Usage "who [server]". Show all local and network users //on specified server and some additional information. //Freeware. You can use and modify this source code //as you wish. //------------------------------------------------------- #include <windows.h> #include <lm.h> #include <stdio.h> #pragma hdrstop #pragma comment (lib, "netapi32.lib") #define MAXLEN 256 SESSION_INFO_502 *buf, *cur; WKSTA_USER_INFO_1 *bufw, *curw; FILE_INFO_3 *buff, *curf; DWORD read, total, resumeh, rc, i; //----------------------------------------------------------- void PrintError (DWORD err) { char msgbuf [4096]; FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), msgbuf, sizeof (msgbuf), NULL); printf ("Error %d: %s\n", err, msgbuf); } //------------------------------------------------------- void PrintLocalUsers (LPWSTR server) { resumeh = 0; do { bufw = NULL; rc = NetWkstaUserEnum ( (LPTSTR) server, 1, (LPBYTE *) &bufw, 2048, &read, &total, &resumeh); if (rc! = ERROR_MORE_DATA && rc! = ERROR_SUCCESS) break; { for (i = 0, curw = bufw; i <read; ++ i, ++ curw) { printf ("%-12S %-15S %-12S \n", curw-> wkui1_username, curw-> wkui1_logon_domain, curw-> wkui1_logon_server ); } } if (bufw! = NULL) NetApiBufferFree (bufw); } while (rc == ERROR_MORE_DATA); if (rc! = ERROR_SUCCESS) PrintError (rc); } //------------------------------------------------------- void PrintNetUsers (LPWSTR server) { resumeh = 0; do { buf = NULL; rc = NetSessionEnum ( (LPTSTR) server, NULL, NULL, 502, (LPBYTE *) &buf, 2048, &read, &total, &resumeh); if (rc! = ERROR_MORE_DATA && rc! = ERROR_SUCCESS) break; { for (i = 0, cur = buf; i <read; ++ i, ++ cur) { printf ("%-12S %-12S %-27S %-22S\n", cur-> sesi502_username, cur-> sesi502_cname, cur-> sesi502_transport, cur-> sesi502_cltype_name ); } } if (buf! = NULL) NetApiBufferFree (buf); } while (rc == ERROR_MORE_DATA); if (rc! = ERROR_SUCCESS) PrintError (rc); } //------------------------------------------------------- void PrintNetFiles (LPWSTR server) { resumeh = 0; do { buff = NULL; rc=NetFileEnum ( (char *) server, NULL, NULL, 3, (BYTE **) &buff, 2048, &read, &total, &resumeh); if (rc! = ERROR_MORE_DATA && rc! = ERROR_SUCCESS) break; for (i = 0, curf = buff; i <read; ++ i, ++ curf) { printf ("%-12S %-15S \n", curf-> fi3_username, curf-> fi3_pathname ); } if (buff! = NULL) NetApiBufferFree (buff); } while (rc == ERROR_MORE_DATA); if (rc! = ERROR_SUCCESS) PrintError (rc); } //------------------------------------------------------- int main (int argc, char *argv []) { WCHAR server [MAXLEN]; if (argc == 1) { wcscpy (server, L ""); } else if (argc == 2) { mbstowcs (server, argv [1], MAXLEN); } else { printf ("Usage - who [server]"); exit (1); } printf ("\nLocal users:\n"); PrintLocalUsers (server); printf ("\nNet users:\n"); PrintNetUsers (server); printf ("\nFiles (Pipes) \n"); PrintNetFiles (server); return 0; } |
2000 (c) DM