相关疑难解决方法(0)

PInvoke:在 C++ 中分配内存并在 C# 中释放它

我们正在使用 PInvoke 在 C# 和 C++ 之间进行互操作。

我有一个互操作结构,如下所示,另一侧具有相同的布局 C++ 结构。

[StructLayout(LayoutKind.Sequential)]
public struct MeshDataStruct : IDisposable
{
    public MeshDataStruct(double[] vertices, int[] triangles , int[] surfaces)
    {
        _vertex_count = vertices.Length / 3;
        _vertices = Marshal.AllocHGlobal(_vertex_count*3*sizeof (double));
        Marshal.Copy(vertices, 0, _vertices, _vertex_count);
    }

    // .. extract data methods to double[] etc.

    private IntPtr _vertices;
    private int _vertex_count;

    public void Dispose()
    {
        if (_vertices != IntPtr.Zero)
        {
            Marshal.FreeHGlobal(_vertices);
            _vertices = IntPtr.Zero;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想添加第二个 ctor

    public MeshDataStruct(bool filled_in_by_native_codee)
    {
        _vertex_count = 0;
        _vertices …
Run Code Online (Sandbox Code Playgroud)

c# c++ pinvoke interop marshalling

6
推荐指数
2
解决办法
2377
查看次数

将字符串封送到非托管内存,将它们传递给c ++并再次返回c#

我在c#中创建非托管内存块,并用结构中的数据填充它.

我遍历结构列表并执行以下操作:

Marshal.StructureToPtr(structTemp, currentMemoryPosition, false);
currentMemPosition = new IntPtr(currentMemPosition.ToInt64() + structSize);      
Run Code Online (Sandbox Code Playgroud)

该结构包含引用类型:"string".我已经研究了BOL的StructureToPtr方法,它说:

"All other reference types (for example, strings and arrays) are marshaled to copies"
Run Code Online (Sandbox Code Playgroud)

究竟是什么意思?

这是否意味着对该字符串的引用仍将在内存中,尽管结构的实例将超出范围?

上面的非托管内存块,我传递给使用它的c ++方法.当作业在c ++部分完成时,我再次遍历内存中的结构(在c#中)和:

Marshal.DestroyStructure(currentMemPosition, typeof(struct));
Run Code Online (Sandbox Code Playgroud)

对我来说最重要的问题是:

Whether I can:

1) Create structs with strings inside
2) Marshal them to unmanaged mamory
3) Make use of them on c++ side
4) **Return them from c++**
5) Read them again in c#
6) Deallocate them in c# by using Marshal.DestroyStructure (EDITED)
Run Code Online (Sandbox Code Playgroud)

带字符串引用类型的struct布局是:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack …
Run Code Online (Sandbox Code Playgroud)

c# marshalling

5
推荐指数
1
解决办法
2310
查看次数

C#,DLL导入API在VS2012 .NET Framework 4.5中无法正常工作

我的WinForms项目存在问题,该项目是在VS2005 .NET Framework 2.0中创建的,我刚刚升级到VS2012 .NET Framework 4.5.在我的项目中,我使用了第三方DLL DllImport并使用了它的功能,因为我有他们的所有文档.

问题是导入的DLL中的一个函数在VS2005中工作正常.NET Framework 2.0在VS2012 .NET 4.5中不起作用.

以下是我项目的代码片段:

[DllImport("W5EditLD.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "K5GetClassName")]
public static extern string GetClassName();//Dll import definition

public string _GetClassName()
{
    return GetClassName();//wrapper function to DLL import function 
}

string sClassName = _GetClassName();//where i call API via wrapper method,**
Run Code Online (Sandbox Code Playgroud)

上面的代码片段在VS2005 .NET Framework 2.0中运行正常但是当我将项目升级到VS2012 .NET Framework 4.5时,我必须按以下方式执行此操作:

[DllImport("W5EditLD.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "K5GetClassName")]

public static extern IntPtr GetClassName();//Dll import definition

public IntPtr _GetClassName()
{
    return GetClassName();//wrapper function to …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke visual-studio-2012

3
推荐指数
1
解决办法
2470
查看次数

标签 统计

c# ×3

marshalling ×2

pinvoke ×2

c++ ×1

interop ×1

visual-studio-2012 ×1