?
Solved

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

Posted on 2000-03-29
5
Medium Priority
?
271 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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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