开发者

Function get_tables() is failing for Windows 7, but it is working for Windows XP

When I debug the code (listed below) in Windows XP, get_tables(&_result) assigns a list of tables to _result and the value of hr becomes S_OK.

If I try the same code with Windows 7 (32-bit), the get_tables function is assigning NULL (0X00000) to _result (that is not excepted), and the value of hr becomes "the application called an interfac开发者_如何学编程e that was marshalled for different thread". My application then crashes.

Why it is this happening for Windows 7 (32-bit)?

Should I go for marshalling thread?

Or I need to change some settings for Windows 7?


// Append the new table
m_pCatalog->Tables->Append(_variant_t((IDispatch *) pTableNew)); 

While debugging I go to this point (see below).

inline TablesPtr _Catalog::GetTables()
{
    struct Tables * _result;
    HRESULT _hr = get_Tables(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
        return TablesPtr(_result, false);
} 


pTableNew was obtained/created on a thread other than the one on which it is being used now - this is your bug.

COM Interfaces (including IDispatch) need to be marshalled between threads unless both threads are within the MTA (COINIT_MULTITHREADED). There are several ways to marshal, but an easy way to have interfaces that are shared across threads is to use the global interface table and store a GIT cookie instead of an interface pointer. This article

http://msdn.microsoft.com/en-us/library/ms678517(VS.85).aspx

has a good basic explanation of the GIT.

If by chance the COM object is one of yours implemented in C++ (rather than one provided by third-party code) another advanced solution would be switch to having your object aggregate the free-threaded marshaller (allowing it to be used directly from any thread). Doing so requires other changes in your code (such as consistent use of GIT cookies for external objects), and more care. Don's article is pretty good:

http://www.microsoft.com/msj/0997/activex0997.aspx

Martyn

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜