P/Invoke in ASP.NET (从 dll 读取/写入文本文件)
P/Invoke in ASP.NET (Reading/writing text file from dll)
我有一个C++ Win32
程序,我正在其中编写和读取文本文件。这个C++程序生成一个 dll,我在我的 ASP.NET Web 应用程序中引用了这个 dll。
使用 P/Invoke,我正在调用方法来从这个 dll 读取和写入文件。
当我在 WPF 应用程序中使用 P/调用对此进行测试时,dll 工作正常。引用 dll 位于此 WPF 应用的 bin/Debug
文件夹中,调用 dll 中的 write 方法会在同一文件夹中生成一个文本文件。此外,from the same folder
,我可以使用dll的read方法来读取文本文件。
但是,当我从我的 ASP.NET Web 应用程序调用 Dll 方法时,生成的文件会转到其他目录(很可能是因为 dll 被加载到其他地方执行),我无法找到这个生成的文件的位置(没有任何错误)
与桌面应用程序类似,有没有办法将 fie 写入 bin 文件夹本身,以便我可以从 bin 文件夹本身读取?
示例代码:
.cpp文件
extern "C" D_API int Write1()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.n";
myfile.close();
return 1;
}
extern "C" D_API char* Read1()
{
ifstream myReadFile;
myReadFile.open("test.txt");
char output[100];
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> output;
}
}
return output;
}
C# .aspx.cs
[DllImport("Testing1.dll", EntryPoint = "fnTest", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int Write1();
[DllImport("Testing1.dll", EntryPoint = "ReadTest", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern StringBuilder Read1();
由于您使用的是相对路径,因此该文件将相对于您调用本机代码时进程的工作目录。正如您所发现的那样,这是一种相当脆弱的安排。
我将通过在本机代码中添加一个额外的字符串参数来解决此问题,该参数指定要使用的文件的完整路径。我敢肯定,您可以从托管代码中轻松生成它。
原生代码
extern "C" D_API int WriteTest(char *filename)
{
....
myfile.open(filename);
....
}
托管代码
[DllImport("Testing1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WriteTest();
要说明的另一点是,读取数据的函数不正确。它尝试返回堆栈分配的缓冲区。您需要在托管代码中分配缓冲区,然后将其传递给本机代码。也许是这样的:
extern "C" D_API int ReadTest(char *filename, char* buffer, int len)
{
//read no more than len characters from filename into buffer
}
在托管端:
[DllImport("Testing1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ReadTest(string filename, StringBuilder buffer, int len);
....
StringBuilder buffer = new StringBuilder(100);
int retval = ReadTest(FullySpecifiedFileName, buffer, buffer.Capacity);
相关文章:
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 读取文件的最后一行并输入到链接列表时出错
- 为什么在读取文件大小时文件IO速度会发生变化
- 读取文件时运行时的未知行为
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C++ 读取文件读取文件不正确
- 读取文件并将其存储在unordered_map中时出现问题
- 读取文件时无法使用 OpenMP 获得加速
- 使用istringstearm和get行缓慢读取文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取文件时引发异常
- 从标准输入读取文件后读取用户输入
- 在读取文件后重置句柄
- 如何在C++编译时读取文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- 读取文件在第二次调用时返回INVALID_HANDLE
- 通过指针读取文件
- 逐行读取文件,并将数据插入变量和数组中