2010-10-10 101 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