从C#将布尔数组传递给C ++代码 [英] Passing Array of Bool to C++ Code from C#

查看:60
本文介绍了从C#将布尔数组传递给C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与其他几个人一起从事一个项目,可惜他们不再可用于参考资料。该项目的GUI使用C#,而C ++用于更新我们的旧数据库。目前,我在C#中具有以下功能:

I'm currently working on a project with a few other people, who are sadly no longer available for reference material. This project uses C# for the GUI, and C++ for updating our archaic database. At the moment, I have this function in C#:

[DllImport("Synch.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    //Synch bool Synch(void * voidPtr, const TCHAR * databaseDir, const TCHAR * truckDir)
    static extern Int32 Synch(IntPtr pSync, string databaseDir, string destDir, ref bool[] wholeFiles);

wholeFiles是一个布尔数组,用于根据用户确定是否应同步某些文件输入。

wholeFiles is an array of booleans used to determine if certain files should be synced or not, based on user input.

C ++应该采用此数组,并且能够在满足某些条件时将某些布尔值翻转为true或false。

The C++ is supposed to take in this array, and be able to flip some of the bools to true or false as certain conditions are met.

if (numSyncRecords > 0){
        Log::WriteVerbose(_T("   %d recs, %d differences found"), (int) numRecsUsed, (int) numSynRecords);
        if(wholeFiles[sourceFileID]){
            CString tempPath = _T("");
            tempPath += destDir;
            tempPath += fileName;
            DeleteFile(tempPath);
        }
        else
            wholeFiles[sourceFileID] = false;
    }
    else
        wholeFiles[sourceFileID] = false;

在上述情况下,我传入True,如果文件没有更改,请设置

In the case above, I pass in Trues, and if there are no changes to the file, set it to false to avoid processing a file that doesn't need it.

现在,我的问题是,当C ++代码完成时,当它们仍然存在True标记时应该设置为false。如果我通过ref发送它,那么我会得到堆栈溢出。没有引用,数组似乎不会改变。

Now, my issue is, when the C++ code is finished, there are still True marks when they should be set to false. If I send it in by ref, then I get a stack overflow. Without ref, the array doesn't seem to change.

编辑:已请求。

SYNC_API Int32 Synch(void * voidPtr, const TCHAR * databaseDir, const TCHAR * truckDir, bool wholeFiles[])

Edit2:在C ++代码中,我有一个log语句,该语句循环遍历数组,并在wholeFiles数组中的每个位置每行输出一次true / false,最后得到适当的日志输出,但是当我在C#代码上查看Array之后,它保持不变。

I have, in the C++ code, a log statement that loops over the array and outputs true/false once per line per position in the wholeFiles array, and I end up with the appropriate log output, but when I look at the Array on the C# code after that is done, it's unchanged.

推荐答案

如果下面的注释您的 DllImport 是C ++方面的确切函数声明,那么您的 wholeFiles 数组在C ++方面将永远看不到因为它不期望有第五个参数。确保首先解决该问题。

If the comment below your DllImport is the exact function declaration on the C++ side, then your wholeFiles array will never be seen on the C++ side because it's not expecting a 5th argument. Make sure to fix that issue first.

在C#中,只有一个大小的 bool ,即1个字节。在C ++中,有两种类型,C样式的1字节 bool 和Win32的4字节 BOOL 默认情况下,P / Invoke将把布尔值编组为 BOOL

In C# there's only one size of bool, and that is 1 byte. In C++ there are two types, the C-style 1-byte bool and the Win32 4-byte BOOL. By default P/Invoke will marshal bools as BOOL.

因此,如果第一位不是整个问题都无法解决,那么您正在使用C风格的 bool ,并且需要使用 [MarshalAs] 像这样:

So if that first bit isn't your entire issue and it's not working, then you are using the C-style bool and need to tell C# that with [MarshalAs] like this:

[DllImport("Synch.DLL", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern Int32 Synch(
    IntPtr pSync,
    string databaseDir,
    string destDir,
    [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.I1)]
    bool[] wholeFiles);

您也不需要 ref 因为数组本身就是引用类型。 ref 数组仅在您调用的函数希望将数组与另一个数组互换时才有用。

You also don't need the ref because an array is a reference type in itself. A ref array would only be useful if the function you are calling wants to swap the array with another one.

另外请注意,您不必使其成为多行,如果函数全部在一行中,则该属性将起作用,我在这里只是为了便于阅读。

Also note that you don't have to make it multiline, the attribute will work if the function is all in one line, I just did that for readability here.

这篇关于从C#将布尔数组传递给C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆