DLL里的线程中CreateDispatch失败

6057 2016-05-26 10:35:12

//AfxOleInit();
CoInitialize(NULL);

_Application wordApp;
if(!wordApp.CreateDispatch(_T("Word.Application")))
{
MessageBox(NULL,_T("启动服务失败"),NULL,NULL);
return 0;
}


上面的代码如果放在DLL开辟的一个线程中就会失败,不用线程就能成功,而且如果放在exe开辟的线程中又能成功,请问这是为什么?我在线程的开始已经初始化MOM组件库了啊,这个问题困扰我两天了一点进展都没,项目都落后了好多,请大神帮我分析一下呀!!!!!
...全文
298 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
6057 2016-05-27
  • 打赏
  • 举报
回复
感谢大家,感谢赵老师和schlafenhamster,问题解决了!
6057 2016-05-27
  • 打赏
  • 举报
回复
引用 11 楼 schlafenhamster 的回复:
worker 线程 中 没有 消息 队列, 可能 要 UI 线程
晕,用户界面线程确实可以,但跟消息队列没关系,应该是UI线程的InitInstance()函数自动初始化了COM组件
赵4老师 2016-05-27
  • 打赏
  • 举报
回复
引用 14 楼 qq_26611129 的回复:
[quote=引用 12 楼 zhao4zhong1 的回复:] FRESERVE Lesson 19 COM Components: Free-threaded Server FRECLIEN Lesson 20 COM Components: Client of Free-threaded Server
MSDN98_1和MSDN98_2下一个就可以了吗?[/quote] 两个都要下载。
6057 2016-05-27
  • 打赏
  • 举报
回复
引用 12 楼 zhao4zhong1 的回复:
FRESERVE Lesson 19 COM Components: Free-threaded Server FRECLIEN Lesson 20 COM Components: Client of Free-threaded Server
MSDN98_1和MSDN98_2下一个就可以了吗?
6057 2016-05-27
  • 打赏
  • 举报
回复
引用 11 楼 schlafenhamster 的回复:
worker 线程 中 没有 消息 队列, 可能 要 UI 线程
在.exe中创建的也是工作线程,为什么又能成功呢,所以不是工作线程和界面线程的区别
6057 2016-05-26
  • 打赏
  • 举报
回复
引用 4 楼 zgl7903 的回复:
线程里建议使用 CoInitialize
COleException e;
if(!wordApp.CreateDispatch(_T("Word.Application"), &e))
查看 COleException 错误信息


请问怎么查看 错误信息,if里面的代码也没执行,直接弹出这个对话框
6057 2016-05-26
  • 打赏
  • 举报
回复
引用 3 楼 jason_wentzel 的回复:
你系统安装了Office吗?
必须装了撒,不是说了除了在DLL线程中不会成功,其它都能成功吗
zgl7903 2016-05-26
  • 打赏
  • 举报
回复
线程里建议使用 CoInitialize COleException e; if(!wordApp.CreateDispatch(_T("Word.Application"), &e)) 查看 COleException 错误信息
孤客天涯 2016-05-26
  • 打赏
  • 举报
回复
你系统安装了Office吗?
6057 2016-05-26
  • 打赏
  • 举报
回复

这是创建线程代码
6057 2016-05-26
  • 打赏
  • 举报
回复
自己先顶一下
赵4老师 2016-05-26
  • 打赏
  • 举报
回复
FRESERVE Lesson 19 COM Components: Free-threaded Server FRECLIEN Lesson 20 COM Components: Client of Free-threaded Server
schlafenhamster 2016-05-26
  • 打赏
  • 举报
回复
worker 线程 中 没有 消息 队列, 可能 要 UI 线程
6057 2016-05-26
  • 打赏
  • 举报
回复
搞不定啊,就没人遇到过这个情况?
6057 2016-05-26
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
COM Tutorial Samples Tutorial Home Using Samples First Lesson List of Lessons Click a lesson link below to jump to the tutorial narrative for the associated code sample. Directory/Sample Lesson Topic APPUTIL Lesson 0 Win32 Basics: Win32 Application Utility Library READTUT Lesson 1 Win32 Basics: Tutorial Reader and Linking to APPUTIL EXESKEL Lesson 2 Win32 Basics: Win32 EXE Skeleton Application DLLSKEL Lesson 3 Win32 Basics: Win32 DLL Skeleton DLLUSER Lesson 4 Win32 Basics: EXE User of a DLL COMOBJ Lesson 5 COM Objects: Containment and Aggregation in a DLL COMUSER Lesson 6 COM Objects: Nested Aggregation in an EXE User REGISTER Lesson 7 COM Components: Component Object Registration DLLSERVE Lesson 8 COM Components: Component Object DLL Server DLLCLIEN Lesson 9 COM Components: Client Application of DLL Server LICSERVE Lesson 10 COM Components: DLL Licensed Server LICCLIEN Lesson 11 COM Components: Client Application of Licensed Server MARSHAL Lesson 12 COM Components: Custom Interface Standard Marshaling MARSHAL2 Lesson 13 COM Components: Marshaling DLL Self-Registration LOCSERVE Lesson 14 COM Components: Local Server LOCCLIEN Lesson 15 COM Components: Client Application of Local Server APTSERVE Lesson 16 COM Components: Local Server with Multiple Apartments APTCLIEN Lesson 17 COM Components: Client of Multiple Apartment Server REMCLIEN Lesson 18 COM Components: Distributed COM (DCOM) Remote Client FRESERVE Lesson 19 COM Components: Free-threaded Server FRECLIEN Lesson 20 COM Components: Client of Free-threaded Server CONSERVE Lesson 21 COM Components: Connectable Object Server CONCLIEN Lesson 22 COM Components: Client of Connectable Object Server STOSERVE Lesson 23 COM Components: Structured Storage Server STOCLIEN Lesson 24 COM Components: Client of Structured Storage Server PERSERVE Lesson 25 COM Components: IPersistStream Persistent Object Server PERTEXT Lesson 26 COM Components: IPersistStreamInit Persistent Object Server PERDRAW Lesson 27 COM Components: IPersistStorage Persistent Object Server PERCLIEN Lesson 28 COM Components: Client of Persistent Object Servers DCDMARSH Lesson 29 COM Components: Standard Marshaling for DCOM DCDSERVE Lesson 30 COM Components: DCOM Server Using Security DCOMDRAW Lesson 31 COM Components: DCOM Client Using Security INC -- Common include directory used by the code samples LIB -- Common library directory used by the code samples TUTSAMP -- Main branch directory with TUTORIAL.EXE, MAKEALL.BAT, etc. Throughout the sample sequence a clear differentiation is maintained between client and server, with a separate lesson sample for each. Usually, each client/server pair covers an area of COM technology. Here is an overview of the technologies covered by the lessons. Basic Win32 application programming is covered in the APPUTIL, READTUT, EXESKEL, DLLSKEL, and DLLUSER lessons. APPUTIL provides a utility framework for building Win32 applications. It also contains some tools needed for tutorial purposes. READTUT is a very simple EXE application that shows how to link to the APPUTIL static library and call utility functions in it. READTUT also shows how to invoke the COM tutorial Web page reader that is used throughout the sample series. EXESKEL shows a basic Win32 skeleton EXE application built using APPUTIL. DLLSKEL and DLLUSER simularly show a basic Win32 DLL (Dynamic Link Library) skeleton and how to access it from an EXE user application. Basic COM object construction, custom interfaces, and techniques for coding their reuse using aggregation and containment are covered in COMOBJ and COMUSER. The implementation and use of the standard IUnknown interface is covered. The implementation and use of the custom ICar, IUtility, and ICruise interfaces is covered. Basic COM component construction, class factories, component object registration, and techniques for housing COM objects in COM component servers are covered in REGISTER, DLLSERVE, and DLLCLIEN. The implementation and use of the standard IClassFactory interface is covered. COM component Licensing is covered in LICSERVE and LICCLIEN. The implementation and use of the standard IClassFactory2 interface is covered. Out-of-Process local servers and the standard marshaling of custom interfaces are covered in MARSHAL, LOCSERVE, and LOCCLIEN. Explicit self-registration in the standard marshaling DLL is covered in MARSHAL2. The use of the MIDL language to specify custom interfaces is covered. The use of the MIDL compiler to produce proxy/stub marshaling servers is also covered. Apartment model server and client construction are covered in APTSERVE and APTCLIEN. Construction of multiple single-threaded apartments (STAs) in the same process is covered. DCOM (Distributed COM) with custom interfaces operating between client and server across machine boundaries is covered in REMCLIEN. Specifying the remote machine name in the COSERVERINFO structure is covered. Detailed DCOM security issues are not covered. Free-threaded COM components and their access by free-threaded clients are covered in FRESERVE and FRECLIEN. The use of several client worker threads in the multi-threaded apartment (MTA) is covered. The implementation and use of a custom IBall interface is covered. Connectable COM object technology is covered in CONSERVE and CONCLIEN. Event source and sink construction is covered. Implementation and use of the IConnectionPointContainer, IConnectionPoint, IEnumConnectionPoints, and IEnumConnections standard interfaces are covered. The implementation and use of the custom IBall and IBallSink interfaces is covered. Structured storage using COM's compound file technology is covered in STOSERVE and STOCLIEN. A COM-based scribble drawing application is used. Use of the IStorage and IStream standard interfaces is covered. The implementation and use of the custom IPaper and IPaperSink interfaces is covered. Persistent COM objects are covered in PERSERVE, PERTEXT, PERDRAW, and PERCLIEN. IPersistStream is covered in the PERSERVE components. IPersistStreamInit is covered in the PERTEXT components. IPersistStorage is covered in the PERDRAW components. The PERCLIEN client functions these various persistent objects and manages storage for all of them in various substorages and streams within one structured storage compound file. The implementation and use of the custom IPageList, ITextPage, IDrawPage, IPageListSink, ITextPageSink, and IDrawPageSink interfaces is covered. DCOM (Distributed COM) Security is covered in DCDMARSH, DCDSERVE, and DCOMDRAW. The use of CoInitializeSecurity is shown in the DCDSERVE and DCOMDRAW samples. Multiple clients accessing a shared single COM object across machine boundaries is shown in a simple network shared-drawing application. Process and activation security within NT network domains is discussed. Registry AppIDs and their LaunchPermission, AccessPermission, and RunAs named values are discussed. The use of the DCOMCNFG utility is covered. Back to page top © 1995-1998 Microsoft Corporation
谢谢你的回复,我好好看看
赵4老师 2016-05-26
  • 打赏
  • 举报
回复
COM Tutorial Samples Tutorial Home Using Samples First Lesson List of Lessons Click a lesson link below to jump to the tutorial narrative for the associated code sample. Directory/Sample Lesson Topic APPUTIL Lesson 0 Win32 Basics: Win32 Application Utility Library READTUT Lesson 1 Win32 Basics: Tutorial Reader and Linking to APPUTIL EXESKEL Lesson 2 Win32 Basics: Win32 EXE Skeleton Application DLLSKEL Lesson 3 Win32 Basics: Win32 DLL Skeleton DLLUSER Lesson 4 Win32 Basics: EXE User of a DLL COMOBJ Lesson 5 COM Objects: Containment and Aggregation in a DLL COMUSER Lesson 6 COM Objects: Nested Aggregation in an EXE User REGISTER Lesson 7 COM Components: Component Object Registration DLLSERVE Lesson 8 COM Components: Component Object DLL Server DLLCLIEN Lesson 9 COM Components: Client Application of DLL Server LICSERVE Lesson 10 COM Components: DLL Licensed Server LICCLIEN Lesson 11 COM Components: Client Application of Licensed Server MARSHAL Lesson 12 COM Components: Custom Interface Standard Marshaling MARSHAL2 Lesson 13 COM Components: Marshaling DLL Self-Registration LOCSERVE Lesson 14 COM Components: Local Server LOCCLIEN Lesson 15 COM Components: Client Application of Local Server APTSERVE Lesson 16 COM Components: Local Server with Multiple Apartments APTCLIEN Lesson 17 COM Components: Client of Multiple Apartment Server REMCLIEN Lesson 18 COM Components: Distributed COM (DCOM) Remote Client FRESERVE Lesson 19 COM Components: Free-threaded Server FRECLIEN Lesson 20 COM Components: Client of Free-threaded Server CONSERVE Lesson 21 COM Components: Connectable Object Server CONCLIEN Lesson 22 COM Components: Client of Connectable Object Server STOSERVE Lesson 23 COM Components: Structured Storage Server STOCLIEN Lesson 24 COM Components: Client of Structured Storage Server PERSERVE Lesson 25 COM Components: IPersistStream Persistent Object Server PERTEXT Lesson 26 COM Components: IPersistStreamInit Persistent Object Server PERDRAW Lesson 27 COM Components: IPersistStorage Persistent Object Server PERCLIEN Lesson 28 COM Components: Client of Persistent Object Servers DCDMARSH Lesson 29 COM Components: Standard Marshaling for DCOM DCDSERVE Lesson 30 COM Components: DCOM Server Using Security DCOMDRAW Lesson 31 COM Components: DCOM Client Using Security INC -- Common include directory used by the code samples LIB -- Common library directory used by the code samples TUTSAMP -- Main branch directory with TUTORIAL.EXE, MAKEALL.BAT, etc. Throughout the sample sequence a clear differentiation is maintained between client and server, with a separate lesson sample for each. Usually, each client/server pair covers an area of COM technology. Here is an overview of the technologies covered by the lessons. Basic Win32 application programming is covered in the APPUTIL, READTUT, EXESKEL, DLLSKEL, and DLLUSER lessons. APPUTIL provides a utility framework for building Win32 applications. It also contains some tools needed for tutorial purposes. READTUT is a very simple EXE application that shows how to link to the APPUTIL static library and call utility functions in it. READTUT also shows how to invoke the COM tutorial Web page reader that is used throughout the sample series. EXESKEL shows a basic Win32 skeleton EXE application built using APPUTIL. DLLSKEL and DLLUSER simularly show a basic Win32 DLL (Dynamic Link Library) skeleton and how to access it from an EXE user application. Basic COM object construction, custom interfaces, and techniques for coding their reuse using aggregation and containment are covered in COMOBJ and COMUSER. The implementation and use of the standard IUnknown interface is covered. The implementation and use of the custom ICar, IUtility, and ICruise interfaces is covered. Basic COM component construction, class factories, component object registration, and techniques for housing COM objects in COM component servers are covered in REGISTER, DLLSERVE, and DLLCLIEN. The implementation and use of the standard IClassFactory interface is covered. COM component Licensing is covered in LICSERVE and LICCLIEN. The implementation and use of the standard IClassFactory2 interface is covered. Out-of-Process local servers and the standard marshaling of custom interfaces are covered in MARSHAL, LOCSERVE, and LOCCLIEN. Explicit self-registration in the standard marshaling DLL is covered in MARSHAL2. The use of the MIDL language to specify custom interfaces is covered. The use of the MIDL compiler to produce proxy/stub marshaling servers is also covered. Apartment model server and client construction are covered in APTSERVE and APTCLIEN. Construction of multiple single-threaded apartments (STAs) in the same process is covered. DCOM (Distributed COM) with custom interfaces operating between client and server across machine boundaries is covered in REMCLIEN. Specifying the remote machine name in the COSERVERINFO structure is covered. Detailed DCOM security issues are not covered. Free-threaded COM components and their access by free-threaded clients are covered in FRESERVE and FRECLIEN. The use of several client worker threads in the multi-threaded apartment (MTA) is covered. The implementation and use of a custom IBall interface is covered. Connectable COM object technology is covered in CONSERVE and CONCLIEN. Event source and sink construction is covered. Implementation and use of the IConnectionPointContainer, IConnectionPoint, IEnumConnectionPoints, and IEnumConnections standard interfaces are covered. The implementation and use of the custom IBall and IBallSink interfaces is covered. Structured storage using COM's compound file technology is covered in STOSERVE and STOCLIEN. A COM-based scribble drawing application is used. Use of the IStorage and IStream standard interfaces is covered. The implementation and use of the custom IPaper and IPaperSink interfaces is covered. Persistent COM objects are covered in PERSERVE, PERTEXT, PERDRAW, and PERCLIEN. IPersistStream is covered in the PERSERVE components. IPersistStreamInit is covered in the PERTEXT components. IPersistStorage is covered in the PERDRAW components. The PERCLIEN client functions these various persistent objects and manages storage for all of them in various substorages and streams within one structured storage compound file. The implementation and use of the custom IPageList, ITextPage, IDrawPage, IPageListSink, ITextPageSink, and IDrawPageSink interfaces is covered. DCOM (Distributed COM) Security is covered in DCDMARSH, DCDSERVE, and DCOMDRAW. The use of CoInitializeSecurity is shown in the DCDSERVE and DCOMDRAW samples. Multiple clients accessing a shared single COM object across machine boundaries is shown in a simple network shared-drawing application. Process and activation security within NT network domains is discussed. Registry AppIDs and their LaunchPermission, AccessPermission, and RunAs named values are discussed. The use of the DCOMCNFG utility is covered. Back to page top © 1995-1998 Microsoft Corporation
zgl7903 2016-05-26
  • 打赏
  • 举报
回复
CXXXX5 访问量方法地址 点击中断, 结合调用堆栈 调试解决之

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧