2010-10-10 45 views
1

我试图在C#应用程序中使用Microsoft的文本服务框架。到目前为止,这一切都在顺风顺水,但我遇到了一些让我陷入困境的东西。根据MSDN文档中,ITfFnReconversion接口发布此方法:COM互操作和编组指针指向一个接口的指针在C#

HRESULT GetReconversion(
    [in] ITfRange *pRange, 
    [out] ITfCandidateList **ppCandList 
); 

我在C#中声明为:

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
void GetReconversion([In, MarshalAs(UnmanagedType.Interface)] ITfRange pRange, [Out, MarshalAs(UnmanagedType.Interface)] out ITfCandidateList ppCandList); 

而且我打电话,像这样:

ITfCandidateList candidateList;  
reconversionInstance.GetReconversion(range, out candidateList); 

值的恢复实例和范围设置较早,我相信它们是有效的。 每次执行此行时,都会收到访问冲突错误,指示尝试读取或写入受保护内存的内容。我假设这是由于候选人列表参数的不当编组,因此我开放给其他可能性。

鉴于该参数是声明为指针的指针,我也试过把它当作一个IntPtr,就像这样:

[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
void GetReconversion([In, MarshalAs(UnmanagedType.Interface)] ITfRange pRange, [Out, MarshalAs(UnmanagedType.SysInt)] out IntPtr ppCandList); 

    IntPtr candidateList;  
    reconversionInstance.GetReconversion(range, out candidateList); 

但留下了同样的错误。

我怎样才能正确编组,这样我就可以获得ITfCandidateList的实例?

为了清楚起见,这里是因为我已经导入他们,但正如我所说,我已经尝试了一些不同的签名GetReconversion接口:

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("4CEA93C0-0A58-11D3-8DF0-00105A2799B5")] 
    public interface ITfFnReconversion : ITfFunction 
    { 
       [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
     void GetDisplayName([Out, MarshalAs(UnmanagedType.BStr)] out string pbstrName); 
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
     void QueryRange([In, MarshalAs(UnmanagedType.Interface)] ITfRange pRange, [In, Out, MarshalAs(UnmanagedType.Interface)] ref ITfRange ppNewRange, [Out, MarshalAs(UnmanagedType.Bool)] out bool pfConvertable); 
     [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
     void GetReconversion([In, MarshalAs(UnmanagedType.Interface)] ref ITfRange pRange, [Out, MarshalAs(UnmanagedType.SysInt)] out IntPtr ppCandList); 
     [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
     void Reconvert([In, MarshalAs(UnmanagedType.Interface)] ITfRange pRange); 
    } 
[ComImport, Guid("A3AD50FB-9BDB-49E3-A843-6C76520FBF5D"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
public interface ITfCandidateList 
{ 
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
    void EnumCandidates([Out, MarshalAs(UnmanagedType.Interface)] out IEnumTfCandidates ppEnum); 
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
    void GetCandidate([In] uint nIndex, [Out, MarshalAs(UnmanagedType.Interface)] out ITfCandidateString ppCand); 
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
    void GetCandidateNum([Out] out uint pnCnt); 
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)] 
    void SetResult([In] uint nIndex, [In, ComAliasName("TSF.TfCandidateResult")] TfCandidateResult imcr); 
} 

回答

0

有一些明显错在这里。 ITfCandidateList **ppCandList不能翻译成简单的输出:这是指向ITfCandidateList的指针。

在我看来,你需要定义为IntPtr,然后尝试读取使用Marshal.PtrToStructure.

+0

感谢输入的那段回忆。我试过这个,并且它一直抛出与尝试读取或写入受保护的内存相同的错误。 – Damion 2010-10-11 00:26:36