Win32 API CopyFile()无法发送多个文件

时间:2009-08-02 10:12:42

标签: c winapi api file

尝试使用CopyFile()函数时遇到了一个奇怪的错误。它不会将这两个文件都写入我的目的地。

这是代码。我发送文件时的部分已被评论。请记住,此代码是草稿,因此请忽略函数定义。

/*



*/


#include <stdio.h>
#include <windows.h>
#include <dirent.h>

char* getPath();
char* combineStrings(char* profile, char* path);
char** findProfile(char* path);
void copyagain();




int main()
{ 
    int fileIndex;
    char* fileLocation     = getPath();  
    char* whereAmI         = _getcwd(NULL,0);
    char **files           = findProfile(fileLocation);
    char* filesToExport[3] = {"\\formhistory.sqlite","\\cookies.sqlite", "\\downloads.sqlite"};
    char* profileName      = files[2];
    char* partPath         = strncat(fileLocation,"\\",3);
    char* pathWoutFile     = strncat(fileLocation,profileName,strlen(profileName) + 1);
    char* fullPathWithFile;
    char* fullSendPath;

    char* downloads = "\\downloads.sqlite";
    char* cookies   = "\\cookies.sqlite";
    char* history   = "\\formhistory.sqlite";







     char* from1 = strncat(fileLocation,filesToExport[0],100);
     char* send1 = strncat(whereAmI,filesToExport[0],100);


      char* from2 = strncat(fileLocation,filesToExport[1],100);
     char* send2 = strncat(whereAmI,filesToExport[1],100);



// ***** This is where I try to send the files ***** 

CopyFile(from1,send1,TRUE);

//Fails when I add two calls to CopyFile();
CopyFile(from2,send2,TRUE);

    return 0;
}

char* getPath()
{

      char* appPath;
      char* usrPath;
      char* fullPath;
      char* drive     = getenv("SYSTEMDRIVE");
      char* user      = getenv("USERNAME");


      OSVERSIONINFO info;
      info.dwOSVersionInfoSize = sizeof(info);
      GetVersionEx(&info);

      if (info.dwMajorVersion >= 6)
      {
        appPath = "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles";
        usrPath = "\\Users\\";
      }

      else
      {
         appPath = "\\Application Data\\Mozilla\\Firefox\\Profiles";
         usrPath = "\\Documents and Settings\\";
      }

      strncat(drive,usrPath,strlen(usrPath) + 1);
      strncat(drive,user,strlen(user) + 1);
      strncat(drive,appPath,strlen(appPath) + 1);
      fullPath = drive;



      return (fullPath);
}

char** findProfile(char* path)
{

    DIR *dir = opendir (path);
    struct dirent *dp;          
    size_t filecount = 0;       
    size_t i = 0;
    char **files;

    if (dir == NULL) {

        return NULL;           
    }
    while ((dp = readdir (dir)) != NULL) {
        filecount++;
    }

    files = (char **) malloc (filecount * sizeof (*files));
    if (files == NULL) {
        return NULL;            
    }


    rewinddir (dir);
    while ((dp = readdir (dir)) != NULL) {
        files[i] = strdup (dp->d_name);
        if (files[i] == NULL) {

            while (i > 0) {
                free (files[--i]);
            }
            free (files);
            return NULL;
        }

        i++;
    }

    closedir (dir);
    return files;
}

2 个答案:

答案 0 :(得分:3)

你的代码中的

char* whereAmI         = _getcwd(NULL,0);

为路径分配足够的内存

char* send2 = strncat(whereAmI,filesToExport[1],100);
然后

strncat尝试使用不存在的内存添加到该路径。

结果:未定义的行为。

答案 1 :(得分:0)

我认为至少部分问题是from1from2指向相同的字符串(send1send2)。

这些行都将打印机返回fileLocation,因此from1from2指向相同的内容。类似于send1send2

 char* from1 = strncat(fileLocation,filesToExport[0],100);
 char* from2 = strncat(fileLocation,filesToExport[1],100);

另一个问题是你要覆盖缓冲区(和/或写入你不拥有的缓冲区)。

getcwd()返回一个指向你不允许修改的内存的指针,而_getcwd()返回指向malloc的内存的指针(所以你可以修改它),它只有字符串那么大返回(据你所知),你不能连接它。