Win32 API CopyFile()无法发送多个文件
在尝试使用CopyFile()函数时,我遇到了一个奇怪的错误。它不会将这两个文件写入我的目标 这是代码。我发送文件时的部分已被注释。请记住,这段代码是一个粗略的草稿,所以忽略函数定义Win32 API CopyFile()无法发送多个文件,c,winapi,api,file,C,Winapi,Api,File,在尝试使用CopyFile()函数时,我遇到了一个奇怪的错误。它不会将这两个文件写入我的目标 这是代码。我发送文件时的部分已被注释。请记住,这段代码是一个粗略的草稿,所以忽略函数定义 /* */ #include <stdio.h> #include <windows.h> #include <dirent.h> char* getPath(); char* combineStrings(char* profile, char* path); ch
/*
*/
#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;
}
/*
*/
#包括
#包括
#包括
char*getPath();
字符*组合字符串(字符*配置文件,字符*路径);
字符**findProfile(字符*路径);
再次无效();
int main()
{
int文件索引;
char*fileLocation=getPath();
char*wheremi=_getcwd(NULL,0);
char**files=findProfile(文件位置);
char*filesToExport[3]={“\\formhistory.sqlite”、“\\cookies.sqlite”、“\\downloads.sqlite”};
char*profileName=文件[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(其中mi,filesToExport[0],100);
char*from2=strncat(fileLocation,filesToExport[1],100);
char*send2=strncat(其中mi,filesToExport[1],100);
//******这是我试图发送文件的地方******
CopyFile(from1,send1,TRUE);
//向CopyFile()添加两个调用时失败;
CopyFile(from2,send2,TRUE);
返回0;
}
char*getPath()
{
char*appPath;
char*usrPath;
字符*完整路径;
char*drive=getenv(“SYSTEMDRIVE”);
char*user=getenv(“用户名”);
OSVERSIONINFO信息;
info.dwosVersionInfo=sizeof(info);
GetVersionEx(&info);
如果(info.dwMajorVersion>=6)
{
appPath=“\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles”;
usrPath=“\\Users\\”;
}
其他的
{
appPath=“\\Application Data\\Mozilla\\Firefox\\Profiles”;
usrPath=“\\Documents and Settings\\”;
}
strncat(驱动器、usrPath、strlen(usrPath)+1);
strncat(驱动器、用户、strlen(用户)+1);
strncat(驱动器、应用路径、strlen(应用路径)+1);
全路径=驱动;
返回(完整路径);
}
字符**findProfile(字符*路径)
{
DIR*DIR=opendir(路径);
结构方向*dp;
大小\u t文件计数=0;
尺寸i=0;
字符**文件;
if(dir==NULL){
返回NULL;
}
而((dp=readdir(dir))!=NULL){
filecount++;
}
files=(char**)malloc(filecount*sizeof(*files));
if(files==NULL){
返回NULL;
}
倒带目录(目录);
而((dp=readdir(dir))!=NULL){
文件[i]=strdup(dp->d_名称);
if(文件[i]==NULL){
而(i>0){
免费(文件[--i]);
}
免费(文件);
返回NULL;
}
i++;
}
closedir(dir);
归还文件;
}
在您的代码中:
char* whereAmI = _getcwd(NULL,0);
为路径分配足够的内存
char* send2 = strncat(whereAmI,filesToExport[1],100);
然后,strncat尝试使用不存在的内存添加到该路径
结果:未定义的行为。在代码中:
char* whereAmI = _getcwd(NULL,0);
为路径分配足够的内存
char* send2 = strncat(whereAmI,filesToExport[1],100);
然后,strncat尝试使用不存在的内存添加到该路径
结果:未定义的行为。我认为您的问题至少有一部分是
from1
和from2
指向同一字符串(就像send1
和send2
一样)
这些行都将打印机返回到fileLocation
,因此from1
和from2
指向相同的对象。类似于send1
和send2
char* from1 = strncat(fileLocation,filesToExport[0],100);
char* from2 = strncat(fileLocation,filesToExport[1],100);
另一个问题是您正在覆盖缓冲区(和/或写入您不拥有的缓冲区)
getcwd()
返回一个指向不允许修改的内存的指针,而\u getcwd()
返回一个指向malloc'ed内存的指针(因此您可以修改它),它只与返回的字符串一样大(据您所知)所以你不能连接到它。我认为你的问题至少有一部分是from1
和from2
指向同一个字符串(就像send1
和send2
一样)
这些行都将打印机返回到fileLocation
,因此from1
和from2
指向相同的对象。类似于send1
和send2
char* from1 = strncat(fileLocation,filesToExport[0],100);
char* from2 = strncat(fileLocation,filesToExport[1],100);
另一个问题是您正在覆盖缓冲区(和/或写入您不拥有的缓冲区)
getcwd()
返回一个指向不允许修改的内存的指针,而\u getcwd()
返回一个指向malloc'ed内存的指针(因此您可以修改它),它只与返回的字符串一样大(据您所知)因此,您无法连接到它。那么,如果我执行第二个CopyFile调用,为什么会起作用?如果我执行第二个CopyFile调用,为什么会起作用?请使用函数检索用户配置文件目录(和appdata)的路径,而不是硬编码用户配置文件目录(和appdata)的路径,请使用函数检索它们: