Link to home
Start Free TrialLog in
Avatar of jhattingh
jhattingh

asked on

What's wrong with this code....?

The following code does the following:

1: Enumerates all the protected files and while enumerating, double checks their "protected" state by calling the SfcIsFileProtected() function

2: It then calls the SfcIsFileProtected() function, passing it the path of a file that does exist AND was listed in the earlier enumeration.

THE PROBLEM: SfcIsFileProtected() says the file IS protected during enumeration, but not in step 2... WHY???


THE CODE:


CString strData, strLine;

PROTECTED_FILE_DATA data;
data.FileNumber = 0;
int nCount=0;
      
while ( SfcGetNextProtectedFile(NULL, &data) != 0 )
{
      LPCTSTR lpszFilename = LPCTSTR(data.FileName);
      if( _access(lpszFilename, 00) != -1 )
      {
            int nRet = SfcIsFileProtected( NULL, data.FileName );

            strLine.Format("[%4d] \"%s\" - %s\r\n", nCount++, CString(data.FileName), nRet !=0?"Protected":"Unprotected");

            strData += strLine;
      }
}

LPCWSTR lpwszFileName = (LPCWSTR)"c:\\winnt\\system32\\xenroll.dll";
if  ( SfcIsFileProtected( NULL, lpwszFileName ) != 0 )
      strLine.Format("=== %s is a protected file === \r\n", lpwszFileName);
else
      strLine.Format("*** %s is NOT a protected file *** \r\n", lpwszFileName);

TRACE("# Last Error: %d\n", GetLastError());

strData += strLine;
GetDlgItem(IDC_EDIT_FILES)->SetWindowText(strData);
Avatar of RONSLOW
RONSLOW

O think your problem is simply casting between LPCTSTR and LPCWSTR.  casting will not convert a string from 8-bit ASCII to wide characters.  Mabe use CString instead of LPCTSTR and LPCWSTR as it knows how to do (some) conversions.
Avatar of jhattingh

ASKER

Updated Code:




      CString strData, strLine;

      PROTECTED_FILE_DATA data;
      data.FileNumber = 0;
      int nCount=0;
      
      while ( SfcGetNextProtectedFile(NULL, &data) != 0 )
      {
            CString strFileName(data.FileName);
            LPCTSTR lpszFilename = (LPCTSTR)strFileName;
            if( _access(lpszFilename, 00) != -1 )
            {
                  int nRet = SfcIsFileProtected( NULL, data.FileName );

                  strLine.Format("%4d__%s - %s\r\n", nCount++, CString(data.FileName), nRet !=0?"Protected":"Unprotected");
                  strData += strLine;

                  //if (nCount>10) break;
            }
      }

      LPCWSTR lpwszFileName = (LPCWSTR)"c:\\winnt\\system32\\xenroll.dll";

      if  ( SfcIsFileProtected( NULL, lpwszFileName ) != 0 )
            strLine.Format("=== %s is a protected file === \r\n", lpwszFileName);
      else
            strLine.Format("*** %s is NOT a protected file *** \r\n", lpwszFileName);

      strData += strLine;

      GetDlgItem(IDC_EDIT_FILES)->SetWindowText(strData);
ASKER CERTIFIED SOLUTION
Avatar of yarond
yarond

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you RONSLOW for your reply. I hope you're not upset that i accepted yarond's answer over yours. My reason was because the second half of my code was failing because wasn't doing the conversion the routine that yarond recommended. CString still was a good thing to use in the first conversion... but that wasn't where the problem was.

I *am* very grateful for all your time spent on answering my problem.

Jason
That is exactly what I said was the problem, that you weren't doing the conversion.

The code you changed didn't address that.

Oh well, guess yarond gets the points.  He came in with the routine you need to use to do the conversion that I said you needed to do.