Solved

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

Posted on 2000-03-29
5
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

  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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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