From: Jos Backus Date: 2004-07-13T13:14:11+09:00 Subject: win32api: how to access NetFileEnum results? I'm trying to programmatically determine any network opened files on a Windows 2003 Server system using the NETAPI32.NetFileEnum API. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/netfileenum.asp describes this API as NET_API_STATUS NetFileEnum( LMSTR servername, LMSTR basepath, LMSTR username, DWORD level, LPBYTE* bufptr, DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries, PDWORD_PTR resume_handle ); My code looks like this: require "Win32API" MAX_PREFERRED_LENGTH = -1 level = 3 entriesread = "\0" * 4 totalentries = "\0" * 4 resume_handle = "\0" * 4 NetFileEnum = Win32API.new("netapi32", "NetFileEnum", 'PPPNPNPPP', 'I') ret = NetFileEnum.call(0,0,0,level,bufptr,MAX_PREFERRED_LENGTH,entriesread,totalentries,resume_handle) if 0 != ret puts "Failed:#{ret}" exit end rbuf = "\0" * 10240 # testing, should be *entriesread * sizeof(FILE_INFO_3) memcpy = Win32API.new('msvcrt','memcpy','PPL','P') memcpy.call(rbuf,bufptr,rbuf.size) p rbuf When level == 3, rbuf will contain an array of FILE_INFO_3 structures as described in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/file_info_3_str.asp which looks like this: typedef struct _FILE_INFO_3 { DWORD fi3_id; DWORD fi3_permissions; DWORD fi3_num_locks; LMSTR fi3_pathname; LMSTR fi3_username; } FILE_INFO_3; My problem is that although the NetFileEnum.call succeeds, *bufstr aka rbuf doesn't appear to have the expected contents, i.e. the fi3_id value(s) seen don't correspond with those returned by the `net file' command. The result is consistent though, leading me to believe I'm either using the wrong parameter template or misinterpreting rbuf. What could I be doing wrong? -- Jos Backus _/ _/_/_/ Sunnyvale, CA _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ jos at catnook.com _/_/ _/_/_/ require 'std/disclaimer'