Solved

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

Posted on 2000-03-29
5
254 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now