P / Invoke in ASP.NET(从dll读取/写入文本文件)
我有一个C++ Win32
程序,我正在编写和阅读文本文件。 这个C ++程序生成一个DLL,我在ASP.NET Web应用程序中引用了这个dll。
使用P / Invoke,我正在调用从这个dll读取和写入文件的方法。
当我在WPF应用程序中使用P / invoke测试时,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 <> 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();
另一个要点是您读取数据的function不正确。 它尝试返回堆栈分配的缓冲区。 您需要在托管代码中分配缓冲区,然后将其传递给本机代码。 也许是这样的:
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);
- 使用C#在Exchange 2010上创建邮箱
- entity framework中是否不支持通用类作为模型?
- 无法将类型’System.Data.EntityState’隐式转换为’System.Data.Entity.EntityState’。 存在显式转换(您是否错过了演员?)
- 无法加载文件或程序集“app_code”或其依赖项之一
- SignalR跨域和IIS 7.5到集线器的路径只能在服务器上本地工作,如何从外部访问它?
- 有没有办法将.dll文件转换为.cs文件
- 如何获取HttpRuntime.Cache对象的到期日期时间?
- 如何在实例化字体时设置多个FontStyles?
- Google通讯录API – 获取访问令牌(oauth)后