- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我一直在谷歌上寻找我的问题的一些答案,但不太明白我找到了什么。在使用 System.IO 读取一些文本文件后,我有一些对象被创建并存储在 C# List 中。之后,我想将对这些对象中的每一个的引用(使用 const 指针)发送到 C++ dll 中的内部类,以便它可以使用它们来计算某些算法。
这是我正在做的一些简单示例(不是实际代码):
C#类:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class SimpleClass
{
[MarshalAs(UnmanagedType.LPStr)]
public string Name;
public float Length;
}
与相应的 C 结构:
struct SimpleClass
{
const char* Name;
float Length;
};
存储在
List<SimpleClass> ItemList;
在解析一些文本文件之后。
然后调用下面的dll函数:
C#:
[DllImport("SimpleDLL")]
public static extern void AddSimpleReference(SimpleClass inSimple);
C:
void AddSimpleReference(const SimpleClass* inSimple)
{
g_Vector.push_back(inSimple); // g_Vector is a std::vector<const SimpleClass* > type
}
我试过的是:
for(int i=0; i<ItemList.Count;++i)
{
SimpleClass theSimpleItem = ItemList[i];
AddSimpleReference(theSimpleItem);
}
最初,我认为只需使用赋值运算符就可以很容易地获得实际的引用/地址,因为 C# 中的类是按引用传递的,但事实证明,C++ 类总是推送相同的地址值 (临时引用的地址值)放入容器而不是实际地址。如何获取实际对象地址并将其发送到 C++ DLL,以便它可以对 C# 对象进行只读访问?
更新:对于使用不安全代码发布答案的人深表歉意。我忘了提到 C# 代码实际上用作 Unity 游戏引擎中的脚本,不允许使用不安全的代码。
最佳答案
首先,您需要更改互操作签名以采用指针(因此使其不安全)。
[DllImport("SimpleDLL")]
public unsafe static extern void AddSimpleReference(SimpleClass* inSimple);
然后,因为 GC 可以随意在内存中移动对象,所以您需要在整个时间内将对象固定在内存中,您将需要它在非托管端的地址。为此,您需要 fixed
语句:
SimpleClass theSimpleItem = ItemList[i];
unsafe
{
fixed(SimpleClass* ptr = &theSimpleItem)
{
AddSimpleReference(ptr);
}
}
如果 AddSimpleReference
使用指针然后丢弃它,这将起作用。但是您将指针存储在 std::vector
中以备后用。这是行不通的,因为一旦执行离开 fixed
block ,由于 GC 将原始项目移动到其他地方,指针可能会变得无效。
要解决此问题,您需要固定这些项目,直到您完成它们为止。为此,您可能需要求助于 GCHandle
类型。
// Change the interop signature, use IntPtr instead (no "unsafe" modifier)
[DllImport("SimpleDLL")]
public static extern void AddSimpleReference(IntPtr inSimple);
// ----
for(int i=0; i<ItemList.Count;++i)
{
SimpleClass theSimpleItem = ItemList[i];
// take a pinned handle before passing the item down.
GCHandle handle = GCHandle.Alloc(theSimpleItem, GCHandleType.Pinned);
AddSimpleReference(GCHandle.ToIntPtr(handle));
// probably a good idea save this handle somewhere for later release
}
// ----
// when you're done, don't forget to ensure the handle is freed
// probably in a Dispose method, or a finally block somewhere appropriate
GCHandle.Free(handle);
当做这样的事情时,请记住将对象长时间固定在内存中是一个坏主意,因为它会阻止垃圾收集器有效地完成其工作。
关于c# - 如何将在 C# 列表中创建的对象的地址传递给 C++ dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6341023/
我有一个 Windows Service(一个发布版本),我替换了一个在 Debug模式下构建的 DLL 并尝试启动服务。它引发了错误无法加载文件或程序集“名称”或其依赖项之一。尝试加载格式不正确的程
我使用 Microsoft Visual Basic 6.0 Enterprise Edition SP6 开发了一个 VB6 应用程序。 我已将可执行文件复制到未安装 VB6 的计算机 C。 我已在
我有很多小的 DLL,我想将它们组合成一个大的(更)DLL(如 suggested here)。我可以通过合并我的项目来做到这一点,但我想要一种不那么干扰的方式。 可以将多个 DLL 合并为一个单元吗
我想将非托管 DLL 和图像文件与托管 DLL 合并。我该怎么做?可能吗? 最佳答案 很有可能。这可以通过 bxilmerge 来完成此解决方案可以将非托管 DLL 和图像或其他文件与托管 DLL 合
如何在Windows Server 2003上安装msvcr71.dll,这是我的软件所需的。我真的不想将此dll复制到system32文件夹,因为它可能会破坏此目标系统。 最佳答案 只需将其复制到程
我最近遇到了一个安装在我的系统上的 DLL,Dependancy Walker(以及我尝试过的所有其他实用程序)说按名称或顺序导出为零,但文件大小约为 4mb。我认为 DLL 的唯一目的是导出供其他代
我终于让我的本地主机在本地显示该站点。一切似乎都在朝着这个方向努力。我的下一步是当网站使用 ActiveX.dll 中的函数时,实际上能够从 VB6 IDE 进入代码 更新: 我更新了代码并删除了我在
首先,我指的是Windows环境和VC++编译器。 我想要做的是重建一个Vc++ dll并与已经链接到该lib的exe保持兼容性,而不必重建该exe或使用LoadLibrary动态加载该dll。换句话
我以前这样做过,我不记得我是从网上下载的 DLL 还是其他东西,但我不想感染病毒。我需要访问这个命名空间,以便我可以拥有 Webbrowswer 控件不提供的额外功能。 如何准确添加 Com 引用。还
我正在尝试为依赖于 ghostscript 的库创建 Nuget 包,因此引用 gsdll32.dll - 一个非托管库。我不能只包含一个标准的 dll 引用。我应该把它放在 nuget 目录结构中的
如果我有 Windows 可执行文件,我如何找出它将加载哪些 dll? 我只是谈论哪些将静态加载,而不是那些可能使用 LoadLibrary 等动态加载的内容。 最佳答案 dumpbin是VC++自带
这可能是一个非常菜鸟的问题,但在当今的网络应用程序开发世界中,许多程序员不需要过多处理 dll,因此也懒得去了解它们的用途。 那么什么是dll? 它有什么用? 它是如何工作的? 如何创建一个? 在什么
所以我刚刚开始使用 OpenTK,并将此代码放在一个继承 GameWindow 类的类中: protected override void OnRenderFrame(FrameEventArgs e
如何调试未由 java 应用程序加载的 dll。 场景是这样的:我的 java 应用正在加载正在使用另一个 dll 的 jni.dll,而那个 dll 正在使用另一个 dll。 javajni.dll
我将“更好”放在引号中,因为这是一个定性问题。几年来我一直在编写 COM DLL,直到最近才发现并成功使用了带有 Typelib 概念的标准 DLL。 使用 COM DLL 代替 DLL+Typeli
这种用户机器可能没有msvcp100.dll、msvcp100.dll之类dll的情况怎么处理?我不希望我的软件因为这种错误而无法安装在用户的机器上。我一直在考虑要么找到一个工具并将每个需要的 dll
在 VS2012 中使用 C# .NET 和 COM 互操作,我正在开发一个用于其他几个程序的公共(public)库。为了简化集成,我想将整个库缩减为一个 DLL。该库的特点之一是本地化。它有包含多种
我在 .Net 中使用 FieldTalk Modbus。当我运行应用程序时,我在为 MbusTcpMasterProtocol 创建对象时遇到错误。 MbusTcpMasterProtocol mb
我想指出,我知道如何向/从 GAC 添加/删除程序集。我要问的是,是否有人可以从技术角度向我解释它是如何工作的。将 dll 放在那里有什么意义 - 从节省资源的角度来看。 亲切的问候 最佳答案 将东西
我继承了一个传统的经典 ASP 应用程序,它使用 VB6 ActiveX DLL 来执行业务逻辑。 我想跟踪加载和卸载 DLL 的点。有没有办法在 VB6 DLL 中拦截这些事件? 在相关说明中,Cl
我是一名优秀的程序员,十分优秀!