关于套间的第二个程序,疑问多多
2006-08-03 10:00
274 查看
组件程序十分简单,组件的线程模型为STA,代码如下:
STDMETHODIMP CTheMath::Add(long IOp1, long IOp2, long* plResult)
{
*plResult = IOp1 + IOp2;
printf("IOp1 + IOp2 = %d", *plResult);
return S_OK;
}
调用组件的程序代码如下:
// ATLSTLExe.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <iostream>
#include "ATLSTATest.h"
#include "ATLSTATest_i.c"
using namespace std;
int ExeProc(LPVOID p)
{
CoInitializeEx(0, COINIT_MULTITHREADED);
ITheMath *ptrMath;
HRESULT hr = CoCreateInstance(CLSID_TheMath,
NULL,
CLSCTX_SERVER,
IID_ITheMath,
(void **)&ptrMath);
if FAILED(hr)
{
cout<<"Get ptrMath error!"<<endl;
CoUninitialize();
return -1;
}
cout<<"Now Begin to create file!"<<endl;
long* plresult;
ptrMath->Add(100, 200, plresult);
cout<<"100+200 = "<<*plresult<<endl;
ptrMath->Release();
CoUninitialize();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
i = 0;
HANDLE hr;
DWORD ThreadID;
hr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ExeProc, (LPVOID)&i, 0, &ThreadID);
if (hr)
CloseHandle(hr);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
当上述代码中红色部分改为:
CoInitializeEx(0, COINIT_APARTMENTTHREADED); //STA
程序提示如下调试信息:
加载模块: ATLSTLExe.exe
加载模块: ole32.dll
加载模块: coredll.dll
Get ptrMath error!
线程 'ExeProc' (0xe7a47242) 已退出,返回值为 -1 (0xffffffff)。
当代码不变的时候,com为组件创建一个STA套间,调用组件的程序中创建的线程运行在MTA套间中,对于跨套间的调用,com自动列集接口,实现同步,调试信息如下:
加载模块: ATLSTLExe.exe
加载模块: ole32.dll
加载模块: coredll.dll
加载模块: oleaut32.dll
加载模块: atlstatest.dll
Now Begin to create file!
IOp1 + IOp2 = 300100+200 = 300
卸载模块: atlstatest.dll
卸载模块: oleaut32.dll
线程 'ExeProc' (0x87a1e95a) 已退出,返回值为 0 (0x0)。
以上输出表明程序运行正常,哪位网友能说说原因?
STDMETHODIMP CTheMath::Add(long IOp1, long IOp2, long* plResult)
{
*plResult = IOp1 + IOp2;
printf("IOp1 + IOp2 = %d", *plResult);
return S_OK;
}
调用组件的程序代码如下:
// ATLSTLExe.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <iostream>
#include "ATLSTATest.h"
#include "ATLSTATest_i.c"
using namespace std;
int ExeProc(LPVOID p)
{
CoInitializeEx(0, COINIT_MULTITHREADED);
ITheMath *ptrMath;
HRESULT hr = CoCreateInstance(CLSID_TheMath,
NULL,
CLSCTX_SERVER,
IID_ITheMath,
(void **)&ptrMath);
if FAILED(hr)
{
cout<<"Get ptrMath error!"<<endl;
CoUninitialize();
return -1;
}
cout<<"Now Begin to create file!"<<endl;
long* plresult;
ptrMath->Add(100, 200, plresult);
cout<<"100+200 = "<<*plresult<<endl;
ptrMath->Release();
CoUninitialize();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
i = 0;
HANDLE hr;
DWORD ThreadID;
hr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ExeProc, (LPVOID)&i, 0, &ThreadID);
if (hr)
CloseHandle(hr);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
当上述代码中红色部分改为:
CoInitializeEx(0, COINIT_APARTMENTTHREADED); //STA
程序提示如下调试信息:
加载模块: ATLSTLExe.exe
加载模块: ole32.dll
加载模块: coredll.dll
Get ptrMath error!
线程 'ExeProc' (0xe7a47242) 已退出,返回值为 -1 (0xffffffff)。
当代码不变的时候,com为组件创建一个STA套间,调用组件的程序中创建的线程运行在MTA套间中,对于跨套间的调用,com自动列集接口,实现同步,调试信息如下:
加载模块: ATLSTLExe.exe
加载模块: ole32.dll
加载模块: coredll.dll
加载模块: oleaut32.dll
加载模块: atlstatest.dll
Now Begin to create file!
IOp1 + IOp2 = 300100+200 = 300
卸载模块: atlstatest.dll
卸载模块: oleaut32.dll
线程 'ExeProc' (0x87a1e95a) 已退出,返回值为 0 (0x0)。
以上输出表明程序运行正常,哪位网友能说说原因?
相关文章推荐
- 关于个人的第二个程序——黑白棋续
- 关于我的第二个程序——黑白棋
- 关于java程序加载的疑问
- 小程序大问题,MSDN中一个小小示例所带来的疑问,一个关于DataList的一个简单应用
- 个人关于程序和系统方面知识的疑问--------:exe 、dll 、库、api 该咋理解???
- 关于《Windows程序设计(第五版)》中一个实例程序的疑问
- 小程序大问题,MSDN中一个小小示例所带来的疑问,一个关于DataList的一个简单应用
- 关于Tomcat之response.encodeURL的实现疑问和发现
- 关于在小米手机安装程序时出现Installation failed with message INSTALL_CANCELED_BY_USER.
- 关于读取手机联系人的小程序制作笔记(二)
- 关于服务器端程序总结
- 关于如何在C#程序中访问windows用户的密码:
- 你脑子里关于字符集的疑问都收录在这!
- 关于malloc函数后free内存空间的疑问
- 关于c++检测内存泄露相关知识(windows程序调试)
- 第十三周项目1:阅读、修改和运行关于交通工具类的程序(1)
- EBS系统<关于Oracle数据库的包的编译(对AP程序的)影响>
- 多多V8实现九块邮功能,9,9包邮功能 程序 模版
- 有关于C++的一些程序设定问题
- 关于程序的编译和链接