Search code examples
visual-c++unc

GetFreeDiskSpaceEx on UNC path


I have the following code to find out free disk space on a UNC path. When I run in Debug mode, I am getting the proper values, but when I run in release mode I am getting a 1326 logon failure on GetFreeDiskSpaceEx function. I have even used NetUseAdd above calling GetFreeDiskSpaceEx. Can someone tell how to get the disk space here from UNC? Following is the function I am using.

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

Solution

  • Got this fixed by using WNetConnection2 instead of NetUseAdd and mapping the network drive. Using GetDiskFreeSpaceEx on the connected drive return proper value.