C++ DLL 在链接到 C# 应用程序时无法写入文件 asp.net
C++ dll fails to write to file when linked to asp.net c# application
我有一个C++dll。 我想让它写入服务器上的日志文件。 当我将其链接到控制台应用程序时,它可以很好地写入文件。 当我将其链接到 asp.net 应用程序时,文件 I/O 失败。 不太确定我错过了什么。
这是与控制台应用链接时工作正常的C++ dll 代码。
char* LogHelper()
{
char* buff = new char[500];
std::ofstream myfile;
myfile.open("testlog.txt",ios::out);
if(myfile.is_open())
strcpy(buff,"we made it");
else
strcpy(buff,"fail, fail, fail..........");
return buff;
}
下面是调用它的 C# 代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[DllImport("C:\Dev\ChatLib\Debug\ChatLib.dll")]
public static extern string dllStartMain();
protected void b1_Click(object sender, EventArgs e)
{
t1.Text = dllStartMain();
}
}
似乎应该很简单。 为什么 dll 在链接到控制台应用而不是 Web 应用时有效? 我知道它执行 dll,因为它返回"失败,失败,失败......"字符串。
任何关于该主题的文章的帮助或建议将不胜感激。 提前谢谢。
extern "C" __declspec char* LogMain();
{
//Put your code here..
//return the char*.
}
[DllImport("ChatLib.dll", CallingConvention = CallingConvention.Cdecl)]
static extern StringBuilder LogMain();
或
public static class Importer
{
[DllImport("ChatLib.dll", EntryPoint = "LogMain", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string LogMain();
}
public static void main(String args[])
{
string s = Importer.LogMain();
}
或者只使用不安全的关键字:
//Import function just like above
unsafe
{
char* t = Importer.LogMain();
string s = new string(t);
}
相关文章:
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 如何使用Winforms C#或C++.Net在不更改文件中其他内容的情况下修改jpg文件中的Orientation e
- Caffe在net_.reset上打印整个protext文件(new Net<float>(model_file,TEST))
- 合并 c 可执行文件、托管 COM dll 和 .Net GUI
- 如何从协议缓冲区 .NET 代码生成 .proto 文件
- 将 .NET C# 程序集成到已编译(非托管)C++程序中(无需执行单独的.exe文件)
- 在vb.net中调用C DLL文件
- 从C#.NET到Objective-C或C 的bin文件
- 如何在Visual Studio/C++/.NET中快速查找给定声明所需的头文件
- 如何解决"原生DLL libmbusmaster.dll的问题不见了!请将 DLL 文件部署到与 mbusmaster.net.dll 相同的目录中。
- 如何查明exe文件是.Net exe还是常规exe
- 将文件上传到 box.net
- 由触发器调用的 MS SQL XP 更改 C++ 文件日期戳,以允许刷新表单应用程序 vb.net
- DllImport 未在 asp.net 代码隐藏文件中定义,即使包含 System.Runtime.InteropSe
- C++ DLL 在链接到 C# 应用程序时无法写入文件 asp.net
- 通过 p/调用读取/写入文件时出现问题 ASP.NET bin 文件夹中
- P/Invoke in ASP.NET (从 dll 读取/写入文本文件)
- 为什么是.NET 的文件。使用 UNC 路径打开 进行过多的 SMB 调用?
- 如何在二进制文件(.net或非托管文件)中注入/更新Version和其他详细信息
- 使用程序集链接器将资源文件链接到现有.NET程序集