Thread: Code runs on Windows 7 but not Vista?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Code runs on Windows 7 but not Vista?

    The program below works fine on Windows 7 Home Edition. It just copies a couple of files from one location to another, renaming them in the process. As you can see, messages are printed to the screen to tell the user what is happening.

    Originally, the program used the CopyFile lines that are now commented out. On W7, the program worked just fine, copying 2 mdb files from the mmtp folder to c:\ with new names. I was happy. (I'm a newbie at C++...for me this was a significant achievement.)

    I wanted to make sure the program was portable so I copied the program to my Vista. It printed the messages just fine. But it did not copy the files. So I created the program from scratch on the Vista. (I have Visual C++ Express 2010 on both machines. The builds were sucessful on both machines with each trial.) I made sure the folder and files existed. Once more, it printed the messages, but didn't copy the files.

    Stumped, I decided to replace the copyfile lines with the system copy lines you see in use below. Once again, the messages printed, but instead of copying files, the program printed this: The system cannot find the file specified. Not what I expected but it's a clue, right? Still, I don't understand why the program couldn't see the files.

    Finally, I copied the mdb file to c:\ and tried again. Still no copying, but I got a new message "Access denied". Wow, another clue. The program can see the files now but can't copy them. Aghh! I'm not asking for much...just to copy a couple of files.

    It seems it's not the code but something about Vista or the environment. Now you're where I am now. Actually, I hope you're not where I am because I don't have any idea what to do next and would appreciate your help.

    Thanks so much for any ideas.

    gk

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <io.h>
    #include <Windows.h>
    
    using std::cout;
    using std::endl;
    int main()
    
    {
        cout << "Preparing to copy..." << endl;
    	  cout << endl;
        cout << "Copying main database" << endl;
    	system("copy C:\\Consumer.mdb c:\\Austin.mdb");
      //CopyFile("c:/mmtp/consumer.mdb","c:/Austin.mdb",FALSE);
    
         cout << "Copying license file" << endl;
     	system("copy C:\\Consumer.mdb c:\\Austin.mdb");
     //CopyFile("c:/mmtp/db8.mdb","c:/license.mdb",FALSE);
      cout << endl;
      cout << "Files copied" << endl;
    		 system("pause");
    return 0;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Vista's UAC sucked.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try copying files in your own user area, say
    C:/Users/me/Documents

    CopyFile("C:/Users/me/Documents/db8.mdb","C:/Users/me/Documents/license.mdb",FALSE);

    It's also worth reading the manual -> CopyFile Function (Windows)
    Mostly to see what status it returns, and whether getLastError will give you more information than "it doesn't work".

    Most programs are no longer allowed to write files at will anywhere on the file system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2

    It was so simple

    Salem, as it turned out all I had to do was run the program as administrator. Wokred just fine. Thanks for replying..

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gken05 View Post
    The program below works fine on Windows 7 Home Edition. It just copies a couple of files from one location to another, renaming them in the process. As you can see, messages are printed to the screen to tell the user what is happening.
    You may want to fix the "wrong way" slashes in your CopyFile() calls and try that again... set them up as \\ just like in your system command

    Also if you're writing to file systems outside your own documents folder you may need to include a manifest file in your program's resources using a resource editor (if your version of VC doesn't have it).

    The manifest file should look like this... I've bold faced the parts you need to change for your particular application...
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <assemblyIdentity type="win32"
                        name="ProgramNameWithoutExtension"
                        version="1.0.0.0"
                        processorArchitecture="X86 or X64" />
      <description>
        Short text description of program  
      </description>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity type="win32"
                            name="Microsoft.Windows.Common-Controls"
                            version="6.0.0.0"
                            processorArchitecture="X86"
                            publicKeyToken="6595b64144ccf1df"
                            language="*" />
        </dependentAssembly>
      </dependency>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel  level="asInvoker" 
                                      uiAccess="false" /> 
          </requestedPrivileges>
        </security>
      </trustInfo>
    </assembly>

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CommonTater View Post
    You may want to fix the "wrong way" slashes in your CopyFile() calls and try that again... set them up as \\ just like in your system command
    There is no need. For APIs, / is the same as \.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Elysia View Post
    There is no need. For APIs, / is the same as \.
    Naming Files, Paths, and Namespaces (Windows)

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your point?
    Quote Originally Posted by MSDN
    Note: File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix as detailed in the following sections.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-18-2010, 08:45 PM
  2. Runs in Windows but not un Linux (Ubuntu)
    By Joelito in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2009, 06:49 PM
  3. Differences between Windows XP and Windows Vista programming
    By nathan3011 in forum Windows Programming
    Replies: 3
    Last Post: 01-15-2009, 10:05 AM
  4. Open an excel file from a windows service under Windows Vista
    By AdrianaLuby in forum C# Programming
    Replies: 1
    Last Post: 06-05-2007, 03:55 AM
  5. windows installer for psp runs for vs6
    By iain in forum Tech Board
    Replies: 1
    Last Post: 06-27-2004, 09:21 AM