【问题标题】:Win32 API CopyFile() fails to send multiple filesWin32 API CopyFile() 发送多个文件失败
【发布时间】:2010-11-16 03:46:57
【问题描述】:

在尝试使用 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;
}

【问题讨论】:

标签: c winapi api file


【解决方案1】:

我认为您的问题至少有一部分是 from1from2 指向同一个字符串(send1send2 也是如此)。

这两行都将打印机返回到fileLocation,所以from1from2 指向同一个东西。 send1send2 类似。

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

另一个问题是您正在覆盖缓冲区(和/或写入不属于您的缓冲区)。

getcwd() 返回一个指向您不允许修改的内存的指针,而 _getcwd() 返回一个指向 malloc 内存的指针(因此您可以修改它),它仅与返回的字符串一样大(如据你所知)所以你不能连接到它。

【讨论】:

    【解决方案2】:

    在您的代码中:

    char* whereAmI         = _getcwd(NULL,0);
    

    为路径分配足够的内存

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

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

    结果:未定义的行为。

    【讨论】:

    • 那么,如果我进行了第二次 CopyFile 调用,为什么它会起作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2020-04-19
    相关资源
    最近更新 更多