从dll访问.csv文件

从dll访问.csv文件,dll,dllexport,Dll,Dllexport,我试图访问DLL中的文件,但fopen始终返回NULL。我不是很熟悉DLL的,我不知道如何调试这个问题。我试着用可执行文件运行我的程序,它运行时没有错误。我将我的.c和.h文件张贴在下面,感谢您的帮助。 ReadFile1.c: #include "ReadFile1.h" #define MAX_STR_LEN 257 #define MAX_Color_Maps 17 int __declspec(dllexport) __stdcall Read

我试图访问DLL中的文件,但fopen始终返回NULL。我不是很熟悉DLL的,我不知道如何调试这个问题。我试着用可执行文件运行我的程序,它运行时没有错误。我将我的.c和.h文件张贴在下面,感谢您的帮助。 ReadFile1.c:

#include "ReadFile1.h"
    #define MAX_STR_LEN 257
    #define MAX_Color_Maps 17
    int __declspec(dllexport) __stdcall ReadF(char* ColorMap)
    {
        FILE* bookFile;
        /* allocation of the buffer for every line in the File */
        char* buf = malloc(MAX_STR_LEN);
        char* tmp;
        /* if the space could not be allocated, return an error */
        if (buf == NULL)
        {
            printf("No memory\n");
            return 1;
        }
        if ((bookFile = fopen("Colormaps2.csv", "r")) == NULL) //Reading a file
        {
            printf("File could not be opened.\n");
            printf("%s",strerror(errno));
            return 1;
        }
        struct Color_decider Colors[MAX_Color_Maps];
        int i = 1;
        fgets(buf, 256, bookFile);
        tmp = strtok(buf, ";");
        char* temp1;
        temp1= strdup(tmp);
        strncpy(Colors[0].Color_maps, temp1, strlen(temp1));
        while (i<17)
        {
            tmp = strtok(NULL, ";");
            temp1 = strdup(tmp);
            strncpy(Colors[i].Color_maps, temp1, strlen(temp1));
            i++;
        }
        for (int k = 1; k < 17; k++)
{
    if (!strcmp(Colors[k].Color_maps, ColorMap))  // Find the input column.
    {
        for (int j = 0; j < 256; j++)
        {
            fgets(buf, 256, bookFile);
            tmp = strtok(buf, ";");
            for (int l = 1; l < k * 3; l++)
                tmp = strtok(NULL, ";");

            tmp = strtok(NULL, ";");
            temp1 = strdup(tmp);
            Colors->C[j][0] = atoi(temp1);
            tmp = strtok(NULL, ";");
            temp1 = strdup(tmp);
            Colors->C[j][1] = atoi(temp1);
            tmp = strtok(NULL, ";");
            temp1 = strdup(tmp);
            Colors->C[j][2] = atoi(temp1);
            printf("%d\t", Colors->C[j][0]);
            printf("%d\t", Colors->C[j][1]);
            printf("%d\n", Colors->C[j][2]);

        }
        break;
    }
}
free(buf);
fclose(bookFile);
return 0;
    }
#包括“ReadFile1.h”
#定义最大长度257
#定义最大颜色映射17
整数declspec(dllexport)\标准调用ReadF(char*ColorMap)
{
档案*书籍档案;
/*为文件中的每一行分配缓冲区*/
char*buf=malloc(最大长度);
char*tmp;
/*如果无法分配空间,则返回一个错误*/
如果(buf==NULL)
{
printf(“无内存”);
返回1;
}
if((bookFile=fopen(“Colormaps2.csv”,“r”))==NULL)//读取文件
{
printf(“无法打开文件。\n”);
printf(“%s”,strerror(errno));
返回1;
}
结构颜色决策器颜色[最大颜色映射];
int i=1;
fgets(buf,256,图书档案);
tmp=strtok(buf,“;”);
字符*temp1;
temp1=标准偏差(tmp);
strncpy(Colors[0]。颜色映射,temp1,strlen(temp1));
而(iC[j][0]=atoi(temp1);
tmp=strtok(空,“;”);
temp1=标准偏差(tmp);
颜色->C[j][1]=atoi(temp1);
tmp=strtok(空,“;”);
temp1=标准偏差(tmp);
颜色->C[j][2]=atoi(temp1);
printf(“%d\t”,颜色->C[j][0]);
printf(“%d\t”,颜色->C[j][1]);
printf(“%d\n”,颜色->C[j][2]);
}
打破
}
}
免费(buf);
fclose(图书档案);
返回0;
}
ReadFile1.h

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
struct Color_decider {
    char Color_maps[10];
    int C[256][3];
};
char *strdup(char *org)
{
    int org_size;
    static char *dup;
    char *dup_offset;
    /* Allocate memory for duplicate */
    org_size = strlen(org);
    dup = (char *)malloc(sizeof(char)*org_size+1);
    if( dup == NULL)
        return( (char *)NULL);

    /* Copy string */
    dup_offset = dup;
    while(*org)
    {
        *dup_offset = *org;
        dup_offset++;
        org++;
    }
    *dup_offset = '\0';

    return(dup);
}
int __declspec(dllexport) __stdcall ReadF(char* ColorMap);
#包括
#包括
#包括
#包括
结构颜色判定器{
字符颜色_映射[10];
INTC[256][3];
};
char*strdup(char*org)
{
国际组织规模;
静态字符*dup;
char*dup_偏移量;
/*为复制分配内存*/
组织大小=斯特伦(组织);
dup=(char*)malloc(sizeof(char)*组织大小+1);
如果(dup==NULL)
返回((char*)NULL);
/*复制字符串*/
dup_offset=dup;
while(*org)
{
*dup_offset=*组织;
dup_offset++;
org++;
}
*dup_偏移量='\0';
返回(dup);
}
int-declspec(dllexport)标准调用ReadF(字符*颜色映射);

错误与.dll无关。请尝试指定文件的完整路径(更好的是:将其作为函数的参数)。您好,谢谢您的回答,但我仍然遇到同样的问题,尽管我硬编码了路径。您还有什么建议吗?