P/Invoke in ASP.NET (从 dll 读取/写入文本文件)

P/Invoke in ASP.NET (Reading/writing text file from dll)

本文关键字:读取 文件 dll 文本 in Invoke ASP NET      更新时间:2023-10-16

我有一个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);