• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1730
  • Last Modified:

C++ CopyFile UNC Failure

I have been trying to copy a file fom my local drive to a UNC path but it doesnt copy, it works if i try to copy from local to local but not to s server

ive tried doubling up the back slashes etc like the below

\\\\?\\UNC\\

does anyone have any further information on this?
0
9772885
Asked:
9772885
  • 5
  • 5
1 Solution
 
AltwiesCommented:
You must make sure that you share the directory and set the path/file access security on the remote directory to allow you read write and create access. This usually means that you have to add a user group or "Anyone" to the access privledge list and set the appropriate check boxes corresponding to the privledge level you want to assign.
0
 
jkrCommented:
What does 'GetLastError()' return?
0
 
9772885Author Commented:
Im very new to C++ so havnt learnt much yet and im not sure how to apply the function you have suggested. I am able to create new files in the destination folder if i open in explorer so shouldnt be a permissions problem


/*Refresh Control*/

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <cstdlib>
#include <istream>
#include <iterator>
#include <fstream>
using namespace std;



int main ()
{

// declare a variable
int i;
int j;

for(i= 0; i < 1; i=0)

    {
      int i;
      printf ("Checking if processor is available...");
      if (system(NULL)) puts ("Ok");
      else exit (1);
      printf ("Executing Refresh...\n");
      i = system ("c:/folder/Refresh.acsauto");                                              
      printf ("RC: %d.\n",i); //Display Return Code


    for(int i=0; i<300000000;i++); //Wait For 5 Seconds
            printf ("Wait Finished");
            
            
            //Copy File
            CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",1);
         

         

    }
return 0;
MessageBox(NULL, "An Error Has Occured", "An Error Has Occured", MB_OK);
      
      
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jkrCommented:
Instead of


            CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",1);

try


            CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);

If the last parameter is '1' and the target file exists, the function will fail.
0
 
9772885Author Commented:

ive made that change and set up a test script to try and get the copy to work but its still not copying across, there wasnt a file to overwrite so the old code should still have worked. Do i have this set correctly is there a way to check if there is an error it seems to run through with no problems, should there be a return code i can check?


#include <windows.h>

int main ()
{
            //Copy File
            CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);
}
0
 
jkrCommented:
To check for errors, you could do the following:
#include <windows.h>
#include <stdio.h>

int main ()
{
            //Copy File
       BOOL bRC = CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);

        if (!bRC) printf("Error: %d\n", GetLastError());
}

Open in new window

0
 
9772885Author Commented:
I have now tried this, a blank cmd box briefly pops up for a few secinds and then disapears with no error msge and nor is the file copied accross
0
 
jkrCommented:
In that case, try the following, so the error should be readable:
#include <windows.h>
#include <stdio.h>

int main ()
{
            //Copy File
       BOOL bRC = CopyFile("C:\\Plasma\\ACD_Primary.txt", "\\\\?\\UNC\\Server\\Folder\\Output.txt",0);

        if (!bRC) printf("Error: %d\n", GetLastError());

        Sleep(20000);
}

Open in new window

0
 
9772885Author Commented:
Hi there was no error message just a blank screen
0
 
jkrCommented:
If there was no error message, the file should have been copied successfully...
0
 
9772885Author Commented:
It is now working thank you all for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now