c++ - Windows Netapi32 -
i accessing windows netapi32
using netapi32.lib
, using c++
access api. having trouble retrieving computer name, 1 netfileenum
here on file_info_3
structure here. in documentation says,
fi3_username
pointer string specifies user (on servers have user-level security) or computer (on servers have share-level security) opened resource. note windows not support share-level security.this string unicode if _win32_winnt or force_unicode defined.
now, network running script in has indeed share-level security, not sure how list computer name.
relevant code
library includes:
#pragma comment(lib, "netapi32.lib") #include <stdio.h> #include <assert.h> #include <windows.h> #include <lm.h>
initiate structure:
fstatus
defined in code as, net_api_status fstatus
, i/o structure. documentation here
if fstatus
successful return value nerr_success
.
if function fails, return value can 1 of following error codes.
error_access_denied
user not have access requested information.error_invalid_level
value specified level parameter not valid.error_invalid_parameter
specified parameter not valid.error_more_data more
entries available. specify large enough buffer receive entries.- ....
more here
to handle use if ((fstatus == nerr_success) || (fstatus == error_more_data))
the user name not found.
fstatus = netfileenum( flservername, //pointer string specifies dns or netbios name of remote server on function execute. if parameter null, local computer used. flbasepath, //pointer string specifies qualifier returned information. if parameter null, open resources enumerated. flusername, //pointer string specifies name of user or name of connection. dflevel, //pointer string specifies name of user or name of connection. can either 2 or 3, using 3 (lpbyte*)&pfile, //pointer address of buffer receives information. format of data depends on value of level parameter. fwprefmaxlen, //pecifies preferred maximum length of returned data, in bytes. &fwentriesread, //pointer value receives count of elements enumerated. &fwtotalentries, //pointer value receives total number of entries have been enumerated current resume position. &fwresumehandle); //pointer value contains resume handle used continue existing file search. net_api_status netfileenum( _in_ lmstr servername, _in_ lmstr basepath, _in_ lmstr username, _in_ dword level, _out_ lpbyte *bufptr, _in_ dword prefmaxlen, _out_ lpdword entriesread, _out_ lpdword totalentries, _inout_ pdword_ptr resume_handle );
raw values above:
net_api_status fstatus; lpfile_info_3 pfile = null; lpfile_info_3 ptmpfile; dword dflevel = 3; lptstr flservername = null; lptstr flusername = null; lptstr flbasepath = null; dword fwprefmaxlen = max_preferred_length; dword fwentriesread = 0; dword fwtotalentries = 0; dword fwresumehandle = 0;
ptmpfile
level 3 (documentation here) buffer object,
bufptr [out]
pointer address of buffer receives information. format of data depends on value of levelparameter.
this buffer returns data in format,
typedef struct _file_info_3 { dword fi3_id; dword fi3_permissions; dword fi3_num_locks; lmstr fi3_pathname; lmstr fi3_username; } file_info_3, *pfile_info_3, *lpfile_info_3;
retrieve data:
printf("\n\tcomputer: %s\n", ptmpfile->fi3_username); //how retrieve computer name??? printf("\n\tid: %d\n", ptmpfile->fi3_id); printf("\n\tpath: %s\n", ptmpfile->fi3_pathname);
**it important note have tried using vbnet , works, somehow cant figure how on c++.
complete test program:
#ifndef unicode #define unicode #endif //initialize netapi library #pragma comment(lib, "netapi32.lib") #include <stdio.h> #include <assert.h> #include <windows.h> #include <lm.h> int wmain(int argc, wchar_t *argv[]) { //netfile enum, using 3 level. net_api_status fstatus; lpfile_info_3 pfile = null; lpfile_info_3 ptmpfile; dword dflevel = 3; lptstr flservername = null; lptstr flusername = null; lptstr flbasepath = null; dword fwprefmaxlen = max_preferred_length; dword fwentriesread = 0; dword fwtotalentries = 0; dword fwresumehandle = 0; dword fi; // // check command line arguments. // dont need currently. // { fstatus = netfileenum(flservername, flbasepath, flusername, dflevel, (lpbyte*)&pfile, fwprefmaxlen, &fwentriesread, &fwtotalentries, &fwresumehandle); if ((fstatus == nerr_success) || (fstatus == error_more_data)) { if ((ptmpfile = pfile) != null) { (fi=0; fi < fwentriesread; fi++) { assert(ptmpfile != null); if (ptmpfile == null) { fprintf(stderr, "an access violation has occurred\n"); break; } printf("\n\tcomputer: %s", ptmpfile->fi3_username); printf("\n\tid: %d", ptmpfile->fi3_id); printf("\n\tpath: %s", ptmpfile->fi3_pathname); printf("\n\tlocks: %d\n", ptmpfile->fi3_num_locks); ptmpfile++; fwtotalentries++; } } } else fprintf(stderr, "a system error has occurred: %d\n", fstatus); // // free allocated memory. // if (pfile != null) { netapibufferfree(pfile); pfile = null; } } // // continue call netfilenum while // there more entries. // while (fstatus == error_more_data); if (pfile != null) netapibufferfree(pfile); return 0; }
output:
from build:
1>------ build started: project: perfmon, configuration: release win32 ------ 1>compiling... 1>file_enumerate.cpp 1>linking... 1>generating code 1>finished generating code 1>embedding manifest... 1>build log saved @ "file://...." 1>perfmon - 0 error(s), 0 warning(s) ========== build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
run:
computer: user1 //prints username, not computername (in our system, each user has same username) id: 1005687 path: c:\fips\library computer: user2 //prints username, not computername (in our system, each user has same username) id: 1005689 path: c:\fips\library\util
if else wondering solution, figured out. query number of files assoicated computer
, not user
, netfileenum
function has used, documentation here. netfileenum
syntax shown below,
net_api_status netfileenum( _in_ lmstr servername, _in_ lmstr basepath, _in_ lmstr username, _in_ dword level, _out_ lpbyte *bufptr, _in_ dword prefmaxlen, _out_ lpdword entriesread, _out_ lpdword totalentries, _inout_ pdword_ptr resume_handle );
where have pass computer name
lmstr username
(you can retrieve computer name quering netsessionenum(502)
return computer names in network, documentation here ) , query returns file details based on dword level
, either file_info_3
documentation here , file_info_2
documentation here.
Comments
Post a Comment