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

本文关键字:为什么 指针 崩溃 c++ 互操作 64位 我的 | 更新日期: 2023-09-27 18:04:36

我得到了一个本地32位DLL(没有源代码),它在我使用的应用程序中作为插件运行。我自己做了另一个本机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;
};
cpp

:

#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代码调用的函数是原生32位dll的源代码(SpoutControls.h)。它不是32位dll本身的源,但声明了用于(据我所知)访问与32位dll相同的共享内存的函数。我也可以复制粘贴这里的代码,如果这可能是问题?也可以在这里找到

谢谢。

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

恐怕你运气不好。如果你的进程是64位的,你将无法加载那个32位的dll,无论你怎么尝试。

我可以在Windows上将32位DLL加载到64位进程中吗?

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

在64位Windows上,64位进程不能加载32位动态链接库(DLL)。

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

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