c# - 我的 C++ 和 C# 互操作在 64 位中崩溃,为什么?指针大小?

标签 c# c++ pointers pinvoke 32bit-64bit

我有一个 native 32 位 dll(无源代码),它在我使用的应用程序中作为插件运行。我自己做了另一个 native dll,它将与该插件通信以创建和更新插件的控件。 我从那个 dll 导出了我需要的功能,以便从我的 c# 应用程序(使用 p/invoke)控制插件。

代码如下:

h 文件:

#pragma once

#include "include\SpoutControls.h"

extern "C" { __declspec(dllexport) void InitializeControls(char *sendername, int *numControls, char** names, int *types, float* floats, float* toggles, float* press, char** text); }
extern "C" { __declspec(dllexport) bool UpdateControls(const char** text, float *floats, float *toggles, float *press, int *numControls); }
extern "C" { __declspec(dllexport) void CloseControls(); }
//
extern "C" __declspec(dllexport) int ReleaseMemory(float *pArray)
{
    delete[] pArray;
    //delete[] Usize;
    return 0;
};

中央人民政府:

#include "SpoutControls4vvvv.h"

//SpoutControls and the functions
//CreateControl, OpenControls, CheckControls, CloseControls
//are declared in SpoutControls.h, which comes with the 32 bit plugin dll
SpoutControls spoutcontrols;

void InitializeControls(char *sendername, int *numControls, char** names, int *types, float* floats, float* toggles, float* press, char** text) {

    int Vcontrols = numControls[0];
    int Tcontrols = numControls[1];
    int Pcontrols = numControls[2];
    int Scontrols = numControls[3];

    int all = Vcontrols + Tcontrols + Pcontrols + Scontrols;
    int v=0, t=0, p=0, s = 0;

    for (int controlID = 0; controlID < all; controlID++) {

        if (types[controlID] == 0) {
            spoutcontrols.CreateControl(names[controlID], "float",0.0,1.0, floats[v]);
            v++;
        }
        if (types[controlID] == 1) {
            spoutcontrols.CreateControl(names[controlID], "bool", toggles[t]);
            t++;
        }
        if (types[controlID] == 2) {
            spoutcontrols.CreateControl(names[controlID], "event", press[p]);
            p++;
        }
        if (types[controlID] == 3) {
            spoutcontrols.CreateControl(names[controlID], "text", text[s]);
            s++;
        }

    }

    spoutcontrols.OpenControls(sendername);
}


bool UpdateControls(const char** text, float *floats, float *toggles, float *press, int *numControls) {
    int Vcontrols = numControls[0];
    int Tcontrols = numControls[1];
    int Pcontrols = numControls[2];
    int Scontrols = numControls[3];

    int all = Vcontrols + Tcontrols + Pcontrols + Scontrols;
    int v = 0, t = 0, p = 0, s = 0;



    if (spoutcontrols.CheckControls(myControls)) {

        for (int controlID = 0; controlID < all; controlID++) {

            if (myControls[controlID].type == 10) {
                floats[v] = myControls[controlID].value;
                v++;
            }
            if (myControls[controlID].type == 0) {
                toggles[t] = myControls[controlID].value;
                t++;
            }
            if (myControls[controlID].type == 1) {
                press[p] = myControls[controlID].value;
                p++;
            }
            if (myControls[controlID].type == 100) {
                text[s] = myControls[controlID].text.data();
                s++;
            }

        }
        return true;
    }
    return false;
}

void CloseControls() {
    spoutcontrols.CloseControls();
}

这是 C# 代码:

public unsafe class SystemSpoutSenderNode: IDisposable
    {

        [System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll")]
        private static extern void InitializeControls(IntPtr sendername, IntPtr numControls,String[] names, IntPtr types, IntPtr floats, IntPtr toggles, IntPtr press, String[] text);
        [System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll")]
        private static extern int CloseControls();
        [System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool UpdateControls([In, Out] String[] text, [In, Out] float[] floats,  [In, Out] float[] toggles, [In, Out] float[] press, IntPtr numControls);
        [System.Runtime.InteropServices.DllImport("SpoutControls4vvvv.dll")]
        private static extern int ReleaseMemory(IntPtr ptr);


    public void Evaluate(int SpreadMax)
            {           
                        //countControls determines number of controls per type (string,float,toggle,click)                                  
                        int[] controls = countControls(FType);
                        //sumControls will just add up all elements in controls
                        int all = sumControls(controls);

                        //in my code these arrays will get filled with values, deleted here for readability         
                        String[] names = new String[all];   
                        int[] types = new int[all]; 
                        float[] floats = new float[controls[0]];
                        float[] toggles = new float[controls[1]];
                        float[] press = new float[controls[2]];

                        String[] text = new String[controls[3]];

                        //initialze return arrays
                        String[] Rtext = new String[controls[3]];
                        float[] Rfloats = new float[controls[0]];
                        float[] Rtoggles = new float[controls[1]];
                        float[] Rpress = new float[controls[2]];

                        //allocate pointers
                        IntPtr SndrNamePtr = NativeUtf8FromString(FSenderName);
                        IntPtr BinPtr = Marshal.AllocHGlobal(4*sizeof(int));                
                        IntPtr TypePtr = Marshal.AllocHGlobal(all*sizeof(int));
                        IntPtr FloatPtr = Marshal.AllocHGlobal(controls[0]*sizeof(float));
                        IntPtr TogglePtr = Marshal.AllocHGlobal(controls[1]*sizeof(float));
                        IntPtr PressPtr = Marshal.AllocHGlobal(controls[2]*sizeof(float));

                        try
                            {           
                            //copy control info + defaults to pointer   
                            Marshal.Copy(controls, 0, BinPtr, 4);
                            Marshal.Copy(types, 0, TypePtr, all);
                            Marshal.Copy(floats, 0, FloatPtr, controls[0]);
                            Marshal.Copy(toggles, 0, TogglePtr, controls[1]);
                            Marshal.Copy(press, 0, PressPtr, controls[2]);

                            //initialize controls   
                            if (FWrite) InitializeControls(SndrNamePtr,BinPtr,names,TypePtr,FloatPtr,TogglePtr,PressPtr,text);

                            //update controls
                            bool changed = UpdateControls(Rtext,Rfloats,Rtoggles,Rpress,BinPtr);


                            //FF, FT, FS and FP are the outputs in my c# host
                            if (changed){

                                for(int j=0; j<controls[0];j++){
                                FF[j]=Rfloats[j];
                                }           
                                for(int j=0; j<controls[1];j++){
                                FT[j]=FloatToBool(Rtoggles[j]);
                                }
                                for(int j=0; j<controls[3];j++){
                                FS[j]=Rtext[j];
                                }
                            }

                            for(int j=0; j<controls[2];j++){
                                FP[j]=FloatToBool(Rpress[j]);
                                }

                            }

                        finally
                        {
                            Marshal.FreeHGlobal(SndrNamePtr);
                            Marshal.FreeHGlobal(BinPtr);
                            Marshal.FreeHGlobal(FloatPtr);
                            Marshal.FreeHGlobal(TogglePtr);
                            Marshal.FreeHGlobal(PressPtr);  
                        }
                    }
                }   
            }

            public void Dispose()
            {
                CleanUp();
                CloseControls();
            }
}

注意:c# 代码无需在基于框架的 c# 主机环境中预编译即可运行以进行图形编程 (vvvv),因此我删除了主机特定的输入(FType、FSenderName)和输出(FF、FS)声明,FP,FT) 以避免混淆。这些将用于将此代码与其他功能“连接”。主机将在每一帧调用 Evaluate。

现在开始实际问题:

到目前为止,它在 32 位中运行良好,但在 64 位中,我的 C# 主机在没有任何消息的情况下崩溃了。经过一些阅读后,我认为这是由于 32/64 位系统中的指针大小不同,但我不确定该怎么做/如果这实际上适用于此。如果可以,我将不胜感激

  • 向我解释如何(以及为什么)让这段代码在 64 位中运行
  • 指出您在此过程中可能发现的任何其他错误 - 我是 c++ 的新手,并且仍然是 c# 的初学者,所以我非常有信心这里有很多需要改进的地方;特别是:内存泄漏以及将值从 C++ 传递到 C#,反之亦然...uiuiui。

我明白我不应该在 64 位中将指针转换为 int,所以我最后尝试的是从

int Vcontrols = numControls[0];
int Tcontrols = numControls[1];
int Pcontrols = numControls[2];
int Scontrols = numControls[3];

int Vcontrols = (INT_PTR)numControls[0];
int Tcontrols = (INT_PTR)numControls[1];
int Pcontrols = (INT_PTR)numControls[2];
int Scontrols = (INT_PTR)numControls[3];

但没有运气,因此我发布了我原来的问题,即使这是一个正确的改进(?)。

编辑:感谢@dkackman 指出了一个不清楚的地方:我的 cpp 代码调用了作为源代码 (SpoutControls.h) 与 native 32 位 dll 一起出现的函数。它不是 32 位 dll 本身的来源,而是声明用于(据我所知)访问与 32 位 dll 相同的共享内存的函数。 如果这可能是问题所在,我也可以在此处复制粘贴代码? 也可以找到here

谢谢。

最佳答案

恐怕你运气不好。如果您的进程是 64 位的,则无论您尝试多少,都无法加载 32 位的 dll。

Can I load a 32 bit DLL into a 64 bit process on Windows?

来自 https://msdn.microsoft.com/en-us/library/windows/desktop/aa384231(v=vs.85).aspx

On 64-bit Windows, a 64-bit process cannot load a 32-bit dynamic-link library (DLL).

如果无法访问其源代码,您唯一的选择是将您的主机转换为 32 位,或者找出如何在 32 位进程中托管 32 位插件并使用某种 IPC 从 64 位主机进程与其通信。

所以我猜测这与您的包装器、数组传递或互操作代码无关。

关于c# - 我的 C++ 和 C# 互操作在 64 位中崩溃,为什么?指针大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192120/

相关文章:

c# - 如何手动设置 HttpContext.User.Identity.IsAuthenticated 的值

c++ - 为git存储库制作一个c文件

c - 警告 : assignment from incompatible pointer type

c++ - 在访问说明符之前放置一个常量限定内联成员函数是错误的吗?

c++ - 避免复制重复使用的特征 block

c++ - 为什么我在这里得到一个 EXC_BAD_ACCESS 以及如何让它工作?

c - 在C中,如何对每个指针都指向可变长度int数组的指针数组进行排序?

c# - ScriptSharp 项目中的单元测试 C# 代码

JavaScript 链接函数/使用 IIFE 表达式扩展函数

C# ref 混淆