我有以下代码来找出 UNC 路径上的可用磁盘空间。当我在调试模式下运行时,我得到了正确的值,但是当我在发布模式下运行时,我在 GetFreeDiskSpaceEx 函数上得到 1326 登录失败。我什至在上面调用 GetFreeDiskSpaceEx 时使用过 NetUseAdd。有人可以告诉如何从 UNC 获取磁盘空间吗?以下是我正在使用的功能。
void FreeSpaceOnPath(LPCWSTR Path, unsigned __int64* freeBytesAvailable, unsigned __int64* totalNumberOfBytes)
{
// set the structure (use_info_2)
USE_INFO_2 u;
// set netuseadd and error checking variables
DWORD rc, error=0;
memset( &u, '\0', sizeof u);
//fill in the USER_INFO_2 for connection infor
u.ui2_local = NULL;
u.ui2_remote = (LPWSTR) L"\\\\10.0.0.113\\e";;
u.ui2_password = (LPWSTR) L"password#123"; //specify no password
u.ui2_username = (LPWSTR) L"10.0.0.113\\user"; // specify no username
u.ui2_asg_type = USE_WILDCARD;
rc = NetUseAdd( NULL, 1, (byte *) &u, &error);
DWORD lasterror = GetLastError();
printf("GetLastError:%d\n", lasterror);
ULARGE_INTEGER freeBytesAvailable2, totalNumberOfFreeBytes, totalNumberOfBytes2;
BOOL ok = GetDiskFreeSpaceEx(Path, &freeBytesAvailable2, &totalNumberOfBytes2, &totalNumberOfFreeBytes);
DWORD err = GetLastError();
printf("Value returned for GetDiskFreeSpaceEx: %d GetLastError:%d\n", ok, err);
wprintf(L"Dir: %s\n", dir);
printf("Value returned for GetDiskFreeSpaceEx: %d\n", ok);
if (ok)
{
*freeBytesAvailable = freeBytesAvailable2.QuadPart;
*totalNumberOfBytes = totalNumberOfBytes2.QuadPart;
}