HRESULT:来自STA线程的0x80004002(E_NOINTERFACE)

时间:2014-03-04 20:14:28

标签: c# com quickbooks

我正在使用Quickbooks SDK,我正在尝试将sessionManager.getResponse(0)转换为IResponse。但是我遇到了:

Unable to cast COM object of type 'System.__ComObject' to interface type 
'Interop.QBFC7.IResponse'. This operation failed because the QueryInterface call on the 
COM component for the interface with IID '{2EA0ED7C-01D5-4CCC-B545-AC3D68D3CA81}' failed 
due to the following error: No such interface supported (Exception from HRESULT: 
0x80004002 (E_NOINTERFACE)).

从环顾四周来看,我发现这个错误通常是通过创建一个STA线程并将代码置于其中来解决的。另外,我是从Windows窗体这样做的。我试图这样做,但我不能让它工作。以下是一些代码段:

//Creating the thread and starting it
backgroundThread = new Thread(new ThreadStart(Method));
backgroundThread.SetApartmentState(ApartmentState.STA);
backgroundThread.Start();

//inside Method()
SessionManager sessionMgr = SessionManager.getInstance();

IMsgSetRequest msgset = sessionMgr.getMsgSetRequest();
msgset.ClearRequests();
msgset.Attributes.OnError = ENRqOnError.roeContinue;

ICustomerQuery query = msgset.AppendCustomerQueryRq();
query.metaData.SetValue(ENmetaData.mdMetaDataOnly);
if (sessionMgr.doRequests(ref msgset))
    return;
//This is the offending line
IResponse resp = (IResponse)sessionMgr.getResponse(0);
int count = resp.retCount;

将sessionMgr.getResponse(0)强制转换为IResponse的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您是否正在使用SDK的包装器或帮助器类?我以前从未见过SessionManager.getInstance()。这是我将使用的一些示例代码。我只用一个按钮设置了一个WinForm。

using QBFC12Lib;

private void button1_Click(object sender, EventArgs e)
{
    // Create a new thread and start it
    Thread backgroundThread = new Thread(new ThreadStart(Method));
    backgroundThread.SetApartmentState(ApartmentState.STA);
    backgroundThread.Start();
}

private void Method()
{
    QBSessionManager sessionMgr = new QBSessionManager();
    sessionMgr.OpenConnection2("AppID", "AppName", ENConnectionType.ctLocalQBD);
    sessionMgr.BeginSession("", ENOpenMode.omDontCare);

    IMsgSetRequest MsgRequest = sessionMgr.CreateMsgSetRequest("US", 12, 0);
    MsgRequest.Attributes.OnError = ENRqOnError.roeContinue;

    ICustomerQuery query = MsgRequest.AppendCustomerQueryRq();
    query.metaData.SetValue(ENmetaData.mdMetaDataOnly);

    IMsgSetResponse MsgResponse = sessionMgr.DoRequests(MsgRequest);

    if (MsgResponse != null && MsgResponse.ResponseList != null)
    {
        IResponse response = MsgResponse.ResponseList.GetAt(0);

        MessageBox.Show(response.retCount.ToString());
    }
}

相关问题