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

时间:2016-08-28 14:06:09

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

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

这里是代码:

h file:

#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),因此我删除了主机特定的输入decalarations(FType,FSenderName)和输出(FF,FS,FP,FT)避免混淆。这些将用于&#34;连接&#34;此代码具有其他功能。每个帧都会由主持人调用评估。

现在回答实际问题:

到目前为止,它在32位工作正常,但在64位我的c#主机崩溃没有任何消息。经过一些阅读后我相信这是由于32 / 64bit系统中的指针大小不同,但我不确定该做什么/如果这实际上适用于此。如果你能

,我会非常感激
  • 解释我如何(以及为什么)让这段代码以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)和本机32位dll。它不是32位dll本身的来源,但声明用于(据我所知)访问与32位dll相同的共享内存的函数。 如果这可能是问题,我也可以复制粘贴代码吗? 也可以找到here

谢谢。

1 个答案:

答案 0 :(得分:6)

恐怕你运气不好。如果您的流程是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

  

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

如果不访问其源代码,您唯一的选择就是将主机转换为32位或以其他方式弄清楚如何在32位进程中托管32位插件,并使用某种IPC与64位主机进程进行通信。

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