Solved

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

Posted on 2000-03-29
5
255 Views
Last Modified: 2012-06-27
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);
0
Comment
Question by:jhattingh
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2667336
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.
0
 
LVL 1

Author Comment

by:jhattingh
ID: 2668624
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);
0
 
LVL 1

Accepted Solution

by:
yarond earned 50 total points
ID: 2677919
You still didn't solve the problem RONSLOW commented about. You can't write things like (LPCWSTR)"c:\=...", It won't make your normal string into a wide one.
try using mbstowcs to turn your file path to wide format, and then pass the wide format to SfcIsFileProtected.
Again, it's not a simple type conversion. In a nutshell, while in the regular strings the text characters are stored one directly after the other, in the wide character string two bytes are reserved for each string, and for normal text it means every second byte will contain 0 (that is, be NULL). So when you just typecase and call the function, it actually recieves junk and not a file name.
0
 
LVL 1

Author Comment

by:jhattingh
ID: 2677995
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
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2678491
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.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now