把以下C++写的代码改写为C#语言
BOOL CSinstar3Tsf::_InitThreadCompartment()
{
    ITfCompartmentMgr *pCompartmentMgr;
    ITfCompartment *pCompartment;
    BOOL fRet = FALSE;    

	if ( _pThreadMgr->QueryInterface( IID_ITfCompartmentMgr, (void **)&pCompartmentMgr) == S_OK)
	{
		if ( pCompartmentMgr->GetCompartment( GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION, &pCompartment) == S_OK)
		{
			fRet = AdviseSink( pCompartment, (ITfCompartmentEventSink *)this,
				IID_ITfCompartmentEventSink, &_dwThreadInputModeSinkCookie);			

			pCompartment->Release();

			if ( !fRet)
			{
				_dwThreadInputModeSinkCookie = TF_INVALID_COOKIE;
			}
		}
		if ( pCompartmentMgr->GetCompartment( GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, &pCompartment) == S_OK)
		{
			fRet = AdviseSink( pCompartment, (ITfCompartmentEventSink *)this,
				IID_ITfCompartmentEventSink, &_dwThreadKeyboardSinkCookie);

			VARIANT var;
			var.vt = VT_I4;
			var.lVal = TRUE;
			pCompartment->SetValue(_tfClientId, &var);

			pCompartment->Release();

			if ( !fRet)
			{
				_dwThreadKeyboardSinkCookie = TF_INVALID_COOKIE;
			}
		}

		pCompartmentMgr->Release();
	}   
    
    return fRet;
}

void CSinstar3Tsf::_UninitThreadCompartment()
{
    ITfCompartmentMgr *pCompartmentMgr;
    ITfCompartment *pCompartment;

	if ( _pThreadMgr->QueryInterface( IID_ITfCompartmentMgr, (void **)&pCompartmentMgr) == S_OK)
	{
		if ( pCompartmentMgr->GetCompartment( GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION, &pCompartment) == S_OK)
		{
			UnadviseSink( pCompartment, &_dwThreadInputModeSinkCookie);

			pCompartment->Release();
		}
		if ( pCompartmentMgr->GetCompartment( GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, &pCompartment) == S_OK)
		{
			UnadviseSink( pCompartment, &_dwThreadKeyboardSinkCookie);

			pCompartment->Release();
		}
		pCompartmentMgr->Release();
	} 
}

BOOL CSinstar3Tsf::_IsKeyboardOpen() const
{
    ITfCompartmentMgr *pCompMgr = NULL;
    BOOL fOpen = FALSE;

    if (_pThreadMgr->QueryInterface(IID_ITfCompartmentMgr, (void **)&pCompMgr) == S_OK)
    {
        ITfCompartment *pCompartment;
		HRESULT hr = pCompMgr->GetCompartment(GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, &pCompartment);
        if (hr == S_OK)
        {
            VARIANT var;
			hr = pCompartment->GetValue(&var);
            if (S_OK == hr && var.vt == VT_I4)
            {
				fOpen = (BOOL)var.lVal;
            }
			pCompartment->Release();
        }
        pCompMgr->Release();
    }

    return fOpen;
}


void CSinstar3Tsf::_UninitThreadCompartment()
{
    ITfCompartmentMgr *pCompartmentMgr;
    ITfCompartment *pCompartment;

	if ( _pThreadMgr->QueryInterface( IID_ITfCompartmentMgr, (void **)&pCompartmentMgr) == S_OK)
	{
		if ( pCompartmentMgr->GetCompartment( GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION, &pCompartment) == S_OK)
		{
			UnadviseSink( pCompartment, &_dwThreadInputModeSinkCookie);

			pCompartment->Release();
		}
		if ( pCompartmentMgr->GetCompartment( GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, &pCompartment) == S_OK)
		{
			UnadviseSink( pCompartment, &_dwThreadKeyboardSinkCookie);

			pCompartment->Release();
		}
		pCompartmentMgr->Release();
	} 
}

STDAPI CSinstar3Tsf::OnChange(REFGUID rguidCompartment)
{	
	    if(!_pThreadMgr) return S_OK;
	    //响应键盘开启状态变化
	    if (IsEqualGUID(rguidCompartment,GUID_COMPARTMENT_KEYBOARD_OPENCLOSE))
	    {//update input mode flags
		    BOOL bOpen=_IsKeyboardOpen();
		    if(!bOpen)
		    {
			    if(_IsCompositing())
			    {
				    SASSERT(_pThreadMgr);
				    ITfContext * pCtx = GetImeContext();
				    if(pCtx) 
				    {	
					    _EndComposition(pCtx);
					    ReleaseImeContext(pCtx);
				    }
			    }
		    }
		    if(m_pSinstar3)
		    {
			    m_pSinstar3->OnOpenStatusChanged(bOpen);
		    }
	    }
	    //响应输入模式变化
	    if ( IsEqualGUID( rguidCompartment, GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION))
	    {
		    EInputMethod mode=GetConversionMode();
		    if(!_IsKeyboardOpen()) mode=HalfAlphanumeric;
		    if(m_pSinstar3) m_pSinstar3->OnConversionModeChanged(mode);
	    }

        return S_OK;
}