Simulating Users with the RunAs Command

One of the problems with writing applications, administering any network, or understanding system issues is to ensure that you see things from the user’s perspective. It doesn’t matter what your forte might be (programmer, administrator, DBA, manager, or the like), getting the user view of things is essential or your efforts are doomed to failure. Of course, this means seeing what the user sees. Anyone can run an application at the administrator level with good success, but the user level is another story because the user might not have access to resources or rights to perform tasks correctly.

Most knowledgeable users know that you can simulate an administrator by right clicking the application and choosing Run As Administrator from the context menu. In fact, if you Shift+Right Click the application, you’ll see an entry for Run As A Different User on the context menu that allows you to start the application as any user on the system. However, the GUI has limitations, including an inability to use this approach for batch testing of an application. In addition, this approach uses the RunAs command defaults, such as loading the user’s profile, which could cause the application to react differently than it does on the user’s system because it can’t find the resources it needs on your system.

A more practical approach is to use the RunAs command directly to get the job done. You can see some basic coverage of this command on page 480 of Windows Command-Line Administration Instant Reference. To gain a basic appreciation of how the user views things, simply type RunAs /User:UserName Command and press Enter (where UserName is the user’s fully qualified logon name including domain and Command is the command you wish to test). For example, if you want to see how Notepad works for user John, you’d type RunAs /User:John Notepad and press Enter. At this point, the RunAs command will ask for the user’s password. You’ll need to ask the user to enter it for you, but at that point, you can work with the application precisely as the user works with it.

Of course, many commands require that you provide command line arguments. In order to use command line arguments, you must enclose the entire command in double quotes. For example, if you want to open a file named Output.TXT located in the C:\MyDocs folder using Notepad and see it in precisely the same way that the user sees it, you’d type RunAs /User:John “Notepad C:\MyDocs\Output.TXT” and press Enter.

In some cases, you need to test the application using the users credentials, but find that the user’s profile gets in the way. The user’s system probably isn’t set up the same as your system, so you need your profile so that the system can find things on your machine and not on the user’s machine. In this case, you add the /NoProfile command line switch to use your profile. It’s a good idea to try the command with the user’s profile first, just to get things as close as you can to what the user sees. The default is to load the user’s profile, so you don’t have to do anything special to obtain this effect.

An entire group of users might experience a problem with an application. In this case, you don’t necessarily want to test with a particular user’s account, but with a specific trust level. You can see the trust levels setup on your system by typing RunAs /ShowTrustLevels and pressing Enter. To run an application using a trust level, use the /TrustLevel command line switch. For example, to open Output.TXT as a basic user, you’d type RunAs /TrustLevel:0x20000 “Notepad C:\MyDocs\Output.TXT” and press Enter. The basic trust levels are:

 

  • 0x40000 – System
  • 0x30000 – Administrator
  • 0x20000 – Basic User
  • 0x10000 – Untrusted User

Many people are experiencing problems using the /ShowTrustLevels and /TrustLevel command line switches with newer versions of Windows such as Vista and Windows 7. The consensus seems to be that Microsoft has changed things with the introduction of UAC and that you’ll need to work with the new Elevation Power Toys to get the job done. I’d be interested in hearing about people’s experiences. Contact me at John@JohnMuellerBooks.com.


Inheriting Someone Else’s Code

In my book, “C# Design and Development,” I propose a lot of management techniques that could apply to any language. In fact, I’ve used the techniques in this book successfully for years with Visual Basic, C#, IronPython, C++, and PHP, among many others. The idea behind the concepts in this book is to create code that performs well from the outset and is easy to maintain when changes are required. In today’s environment, you can count on changes at some point. There are many reasons to expect changes:

 

  • The business is bought by someone else and the code must be integrated into a new system.
  • A change in some other component necessitates a change in the application (such as the changes in Vista and Windows 7 security).
  • The business has grown and now needs new features to support its customers.
  • Modifications in the law mean changes to the way the application interacts with data.
  • Technology changes in such a way that the application no longer works properly.

In fact, the reasons for change are endless. I could probably continue writing reasons for change for hours and not cover even a small number of them. The point is that applications will change at some pointit’s simply a matter of time.

If you’ve written code for any time at all, you know that feeling in the pit of your stomach when you discover the perfectly lucid comments you wrote at the time you created the application are no long lucid now. Worse still, the documentation you put together doesn’t tell you what you did and why. Still, after a few hours of looking at your comments and documentation, you can usually begin to fit the pieces together of an application you created because you can recreate the thought patterns you had when you designed and wrote it in the first place.

However, what happens when you’re part of a team or you’re working on an application that someone else designed? Now there isn’t any way to “get back into the groove” of what you did in the past. The thought processes are foreign, so the comments and documentation have to be as comprehensive as possible. Even with great comments and documentation, it’s going to take a while to get up and running with the code.

The longer it takes you to recreate the ideas that went into creating the code, the longer the project update languishes and the more extreme management’s position becomes. The only thing that management knows is that your application (the one you inherited from someone else) is costing the business money. Not only is there a cost associated with the update, but the delay costs the organization money too. Users who have to fight with an outdated application are always unhappy and aren’t productive. So, what techniques can you use to become productive as quickly as possible? That’s what I’d like to know as well. Obviously, reading the documentation and comments are two pieces to the puzzle, but what other techniques do you use?

I’m currently putting an article together that discusses this topic in detail and I’d like to hear about your techniques. If you’ve ever been frustrated by someone else’s code, write me at John@JohnMuellerBooks.com. I’ll let you know when the article is published on Software Quality Connection

 

Checking Your Compiler in CodeBlocks

Compilers are important because they turn your human-readable source code into executable code that the computer understands. Selecting the right compiler is essential if you want to obtain the best results from your application. Some readers have asked, “Just how do you select a compiler when working with C++ All-In-One Desk Reference For Dummies?” The book assumes that you’re using the GNU GCC Compiler setting and there isn’t any guarantee another compiler will work with the book’s source code. Use these steps to check your compiler setting.

  1. Open the CodeBlocks application.
  2. Choose Settings | Compiler and Debugger. You’ll see the Compiler and Debugger Settings dialog box shown here:
    CompilerSettings
  3. Choose GNU GCC Compiler in the Selected Compiler field as shown in the figure.
  4. Click OK.

You should be ready to work with the book’s code at this point. Let me know if you have any problems choosing the right compiler at John@JohnMuellerBooks.com.

CodeBlocks on a Macintosh

You probably read the post entitled, “Getting CodeBlocks to Work” regarding my book, “C++ All-In-One Desk Reference For Dummies“, and immediately thought it didn’t apply because you have a Macintosh. Rather than write a single confusing post, I decided to write a second post just for Macintosh developers.  Here are the instructions for the Macintosh:

  1. Go to this page: http://www.codeblocks.org/downloads/5 .
  2. Download the Macintosh version of the compiler, codeblocks-8.02-p2-mac.zip. That contains the compiler.
  3. Double click the file once the download is complete and follow the instructions to install the compiler.
  4. At this point, start the CodeBlocks compiler.  Once it has started up, select Settings -> Compiler and Debugger. In the Compiler and Debugger Settings dialog box, click the “Selected Compiler” drop down and choose the GNU GCC Compiler option.  You should be good to go at this point.


I wrote these instructions with the help of a friend with a Macintosh. While the examples in the book work just fine on a Macintosh, I don’t have a lot of Mac experience. If this fix doesn’t work, we may have to work together a bit to come up with a solution.  This solution did work for two other Macintosh readers, so I’m hoping it also works for you, but I’m more than happy to work with you to make sure you get a working setup. Feel free to write me at John@JohnMuellerBooks.com with your ideas and suggestions for a better procedure. You must have a copy of Mac OS X 10.4 or later to use CodeBlocks!

Getting CodeBlocks to Work

One of the most common e-mails I receive about C++ All-In-One Desk Reference For Dummies is that people are receiving an error message about the compiler when they try to compile the examples. A common error message is:

SayHello2 – Debug uses an invalid compiler. Probably the toolchain path within the compiler options is not set up correctly?! Skipping … Nothing to be done.

The reason you’re having trouble is due to an error on the CD.  It turns out that our production folks made a mistake in putting the book’s CD together. The product that we have on the CD is the IDE only and does not include the compiler.  There is a quick solution to the problem should you wish to use it:

  1. Go to this page: http://www.codeblocks.org/downloads/5.
  2. Download the second item on the list, codeblocks-8.02mingw-setup.exe. That contains the compiler.
  3. Double click the file once the download is complete and follow the instructions to install the compiler.


If you’re using Windows Vista or Windows 7, the version of the MinGW compiler that comes with CodeBlocks might not work.  (It does work on my copy of 64-bit Windows 7 and many other people have used it successfully, but a few people do run into problems.)  In this case, you’ll need to go to http://www.mingw.org/
to download the latest version of the MinGW compiler as they suggest on the CodeBlocks Web site.  You can also get the latest version of the compiler from http://sourceforge.net/projects/mingw/files/Automated MinGW Installer/.  My writing partner, Jeff, suggests that you install:

  • MinGWbaseTools
  • g++ compiler
  • MinGW Make


into
C:\MinGW to make the compiler easier for CodeBlocks to find.  At this point, start the CodeBlocks compiler.  Once it has started up, select Settings -> Compiler and Debugger. In the Compiler and Debugger Settings dialog box, click the “Selected Compiler” dropdown and choose the GNU GCC Compiler option.  You should be good to go at this point.  Please let me know if you experience any other problems at John@JohnMuellerBooks.com.

Using the Book’s Source Code

At least some readers try to hand type the source code found in C++ All-In-One Desk Reference For Dummies. Working with source code when you’re learning a new programming language is hard enough; typing it in by hand and getting a good result is nearly impossible in many cases. I recommend that you always use the code from the book’s CD so that you can avoid potential typos. If you experience problems using the code from the book’s CD, please query me at John@JohnMuellerBooks.com; there may be something that isn’t set up correctly on your system.  The examples on the CD should compile and work, but I always emphasize the word “should”
because each system and setup is slightly different (making things exciting for all of us).

To use the code on the CD, follow these steps:

  1. Copy the Author folder from the CD to your hard drive.
  2. Right click the Author folder and choose Properties from the
    context menu.  You’ll see the Author Properties dialog box.
  3. Clear the Read-only check box. Click OK. You’ll see a Confirm
    Attribute Changes dialog box.
  4. Click OK. Windows will remove the read-only attribute from all
    of the files. You’re ready to begin using the source code supplied with the
    book.

To use a particular example, navigate to that examples folder under the Author folder.  Say you want to work with the AddInteger example in Book I Chapter 2.  You’d navigate to the \Author\BookI\Chapter02\AddInteger.  To open the project, simply double click the associated .CBP file.  In this case, you’d double click the AddInteger.cbp file and CodeBlocks will open with the example loaded and ready for you to work with.