请教NetFileEnum函数的用法,主要是前三个参数怎么使用,别给我用NULL的例子,谢谢!还有就是在Windows2003下面,获得的路径只有盘符。

liuguangzhou 2008-09-03 05:36:04
请教NetFileEnum函数的用法,主要是前三个参数怎么使用,别给我用NULL的例子,谢谢!
还有就是在Windows2003下面,获得的路径只有盘符。而不是在XP下面能够获得具体的路径。
...全文
209 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuguangzhou 2008-09-04
  • 打赏
  • 举报
回复
自己解决了第二个问题,是UNICODE编码的问题。继续请问第一个问题。
另外第二个问题的编码也请各位给点思路吧。多谢!
liuguangzhou 2008-09-04
  • 打赏
  • 举报
回复
第一个问题也解决了,同样是UNICODE编码的问题。
求人还不如求己,多思考还是有出路的。
liuguangzhou 2008-09-03
  • 打赏
  • 举报
回复
多谢jixingzhong,但是我不需要MSDN上的说明,我已经看过很多遍了,也不需要98下的例子代码,那对我没有用。
请先看一下我的问题,再作回复吧,谢谢。
jixingzhong 2008-09-03
  • 打赏
  • 举报
回复
sample:


#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <svrapi.h>

const short MAX_ENTRIES = 20;

int main(int argc, char FAR * argv[])
{
char FAR * pszServerName = NULL;
short nLevel = 50;
struct file_info_50* pBuf = NULL;
struct file_info_50* pTmpBuf = NULL;
short cbBuffer;
short nEntriesRead = 0;
short nTotalEntries = 0;
short nTotalCount = 0;
int i;
NET_API_STATUS nStatus;
//
// ServerName can be NULL to indicate the local computer.
//
if (argc > 2)
{
printf("Usage: %s [\\\\ServerName]\n", argv[0]);
exit(1);
}

if (argc == 2)
pszServerName = argv[1];
//
// Allocate the memory required to receive a maximum of
// 20 file_info_50 structures.
//
cbBuffer = MAX_ENTRIES * sizeof(struct file_info_50);

pBuf = malloc(cbBuffer);

if (pBuf == NULL)
printf("No memory\n");

// Call the NetFileEnum function to list the
// open files, specifying information level 50.
//
nStatus = NetFileEnum(pszServerName,
NULL,
nLevel,
(char FAR *)pBuf,
cbBuffer,
&nEntriesRead,
&nTotalEntries);
//
// Loop through the entries; process errors.
//
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
for (i = 0; (i < nEntriesRead); i++)
{
assert(pTmpBuf != NULL);

if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}
//
// Display the information for each entry retrieved.
//
printf("\tShare: %s\n", pTmpBuf->fi50_sharename);
printf("\n\tPath: %s\n", pTmpBuf->fi50_pathname);
printf("\tUser: %s\n", pTmpBuf->fi50_username);
printf("\tID: %d\n", pTmpBuf->fi50_id);

pTmpBuf++;
nTotalCount++;
}
}
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Display a warning if the buffer was not large enough
// to contain all available entries.
//
if ((nEntriesRead < nTotalEntries) || (nStatus == ERROR_MORE_DATA))
fprintf(stderr, "Not all entries have been enumerated\n");
//
// Free the allocated memory.
//
if (pBuf != NULL)
free(pBuf);

fprintf(stderr, "\nTotal of %d entries enumerated\n", nTotalCount);

return 0;
}
jixingzhong 2008-09-03
  • 打赏
  • 举报
回复
NetFileEnum Function

The NetFileEnum function returns information about some or all open files on a server, depending on the parameters specified.
Syntax

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
);

Parameters

servername [in]

Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. If this parameter is NULL, the local computer is used.

This string is Unicode if _WIN32_WINNT or FORCE_UNICODE is defined.
basepath [in]

Pointer to a string that specifies a qualifier for the returned information. If this parameter is NULL, all open resources are enumerated. If this parameter is not NULL, the function enumerates only resources that have the value of the basepath parameter as a prefix. (A prefix is the path component up to a backslash.)

This string is Unicode if _WIN32_WINNT or FORCE_UNICODE is defined.
username [in]

Pointer to a string that specifies the name of the user. If this parameter is specified, its value serves as a qualifier for the enumeration. The files returned are limited to those that have user names matching the qualifier. If this parameter is NULL, no user-name qualifier is used.

This string is Unicode if _WIN32_WINNT or FORCE_UNICODE is defined.
level [in]

Specifies the information level of the data. This parameter can be one of the following values.
Value Meaning

2


Return the file identification number. The bufptr parameter points to an array of FILE_INFO_2 structures.

3


Return information about the file. The bufptr parameter points to an array of FILE_INFO_3 structures.
bufptr [out]

Pointer to the address of the buffer that receives the information. The format of this data depends on the value of the level parameter.

This buffer is allocated by the system and must be freed using the NetApiBufferFree function. Note that you must free the buffer even if the function fails with ERROR_MORE_DATA.
prefmaxlen [in]

Specifies the preferred maximum length of returned data, in bytes. If you specify MAX_PREFERRED_LENGTH, the function allocates the amount of memory required for the data. If you specify another value in this parameter, it can restrict the number of bytes that the function returns. If the buffer size is insufficient to hold all entries, the function returns ERROR_MORE_DATA. For more information, see Network Management Function Buffers and Network Management Function Buffer Lengths.
entriesread [out]

Pointer to a value that receives the count of elements actually enumerated.
totalentries [out]

Pointer to a value that receives the total number of entries that could have been enumerated from the current resume position. Note that applications should consider this value only as a hint.
resume_handle [in, out]

Pointer to a value that contains a resume handle which is used to continue an existing file search. The handle should be zero on the first call and left unchanged for subsequent calls. If this parameter is NULL, no resume handle is stored.

Return Value

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.
Return code Description

ERROR_ACCESS_DENIED


The user does not have access to the requested information.

ERROR_INVALID_LEVEL


The value specified for the level parameter is not valid.

ERROR_MORE_DATA


More entries are available. Specify a large enough buffer to receive all entries.

ERROR_NOT_ENOUGH_MEMORY


Insufficient memory is available.

NERR_BufTooSmall


The supplied buffer is too small.
Remarks

Only members of the Administrators or Server Operators local group can successfully execute the NetFileEnum function.

You can call the NetFileGetInfo function to retrieve information about a particular opening of a server resource.

If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling NetFileEnum. For more information, see IADsResource and IADsFileServiceOperations.
Requirements
Client Requires Windows Vista, Windows XP, or Windows 2000 Professional.
Server Requires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header

Declared in Lmshare.h; include Lm.h.
Library

Use Netapi32.lib.
DLL

Requires Netapi32.dll.

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧