0

我正在扩展 GeckoFx (http://geckofx.org) 并且在从 XPCom 提供的非托管接口返回数组值时遇到一些问题。

我使用最新的 XulRunner 1.9.2.13 版本为 GeckoFx 添加了大量新功能支持,但是在尝试从接口方法返回数组时出现访问冲突异常。例如:

[Guid("43987F7B-0FAA-4019-811E-42BECAC73FC5"), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface mozISpellCheckingEngine
{
    //...
    void GetDictionaryList([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)]ref string[] dictionaries, out uint count);
    //...
}

public static string[] GetAvailableDictionaries()
{
    string[] _dictionaries = null;
    uint count = 0;

    //GetSpellChecker() returns a valid mozISpellCheckingEngine object
    GetSpellChecker().GetDictionaryList(ref _dictionaries, out count);
    if (count > 0)
    {
        if (_dictionaries != null)
        {
            return _dictionaries;
        }
    }
    return null;
}

问题是当 GetDictionaryList 有时返回时,它会返回一个带有单个索引并包含一个字典名称的列表(我在此方法搜索的位置有 2 个字典),并且 count 返回正确的值 2;其他时候,该方法将失败并引发访问冲突,并且 _dictionaries 的值为 string[0] 而 count 保持正确,值为 2。

我想这个问题的最大部分必须是“我在接口声明中正确地编组方法及其参数吗?”。

这个示例代码就是这样 - 一个例子。我想在 GeckoFx 中实现其他几个 XulRunner 功能,但是它们也返回数组并遇到同样的问题。在我能解决这个问题之前,我的工作有点停滞不前。

感谢您提供的所有帮助。

-斯科特

4

1 回答 1

0

改变:

void GetDictionaryList([MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)]ref string[] dictionaries, out uint count);

至:

void GetDictionaryList(ref IntPtr dictionaries, out uint count); 

并像这样使用它:

IntPtr dictionaries = IntPtr.Zero;
int count;

GetDictionaryList(ref dictionaries, count);

 // check dictionaries != IntPtr.Zero; and count > 0

 // dictionaries will be a IntPtr to array IntPtr (of size count)

 string vals = new string[count];

 for(int i = 0; i < count; ++i)
  vals[i] = Marshal.PtrToStringUni(Marshal.SizeOf(typeof(IntPtr)) * i);

(我没有编译这段代码,所以可能有错别字。)

于 2010-12-14T08:18:05.697 回答