主要用于了解一个COM 组件的开发过程和调用方式。
参考:
http://www.cppblog.com/woaidongmao/archive/2011/01/10/138250.html
还有以下几个问题以后有时间、兴趣的话再来解决。
TODO: 1. 如何使用 ATL 开发一个带界面(比如文本输入)。
(PS:VC的自动生成代码太多了,都有点晕了)
2. 开发一个 MFC ActiveX 控件。
3. 分别使用 VC,VB调用上述两个含窗口界面的控件/组件。
FIXME: 下面的示例中,VB调用该COM组件时,只读字符串属性 MyTxt 返回值不正确,VC调用却木问题。
MyAtlCom.zip VC 开发的 COM组件 实现文件:
MyText.cpp
- // MyText.cpp : Implementation of CMyText
-
#include "stdafx.h"
-
#include "MyAtlCom.h"
-
#include "MyText.h"
-
-
/////////////////////////////////////////////////////////////////////////////
-
// CMyText
-
-
long _MyNum = 0;
-
char * _MyTxt = "Hi~~";
-
-
STDMETHODIMP CMyText::MyAdd(long Num1, long Num2, long *ReturnVal)
-
{
-
// TODO: Add your implementation code here
-
*ReturnVal = Num1 + Num2;
-
return S_OK;
-
}
-
-
STDMETHODIMP CMyText::get_MyNum(long *pVal)
-
{
-
// TODO: Add your implementation code here
-
*pVal = _MyNum;
-
return S_OK;
-
}
-
-
STDMETHODIMP CMyText::put_MyNum(long newVal)
-
{
-
// TODO: Add your implementation code here
-
_MyNum = newVal;
-
return S_OK;
-
}
-
-
STDMETHODIMP CMyText::get_MyTxt(BSTR *pVal)
-
{
-
// TODO: Add your implementation code here
-
*pVal = CComBSTR(_MyTxt);
-
return S_OK;
-
}
======================================Client
VC 的命令行 Client
MAIN.CPP
- #include <iostream.h>
-
#include <atlbase.h>
-
#import "MyAtlCom.dll" no_namespace
-
//using namespace MYATLCOMLib;
-
-
-
-
-
void main(void) {
-
-
const IID IID_IMyText = {0x2C197EE8,0xA71C,0x4545,{0xB6,0x13,0xE5,0xE3,0xA5,0xAF,0x36,0x4C}};
-
const IID LIBID_MYATLCOMLib = {0x15AC74AB,0x7178,0x4CF5,{0xAD,0x7A,0xED,0x9B,0xD4,0x91,0x93,0xED}};
-
const CLSID CLSID_MyText = {0x5BDB5A8F,0xED08,0x4AD7,{0xB5,0x52,0x24,0xF3,0xF3,0xDE,0x81,0x76}};
-
-
HRESULT hr;
-
// IMyText* i = NULL;
-
-
hr = CoInitialize(NULL);
-
if(SUCCEEDED(hr)){
-
CComPtr<IMyText> p ;
-
hr = p.CoCreateInstance( CLSID_MyText);
-
if(SUCCEEDED(hr)) {
-
long ReturnValue;
-
hr = p->MyAdd(5, 7, &ReturnValue);
-
cout << "The answer for 5 + 7 is: " << ReturnValue << endl;
-
-
cout <<"MyNum = "<<p->GetMyNum()<< endl;
-
p->PutMyNum(333);
-
cout <<"MyNum = "<<p->GetMyNum()<< endl;
-
-
cout <<"MyTxt = "<<p->GetMyTxt()<< endl;
-
-
} else {
-
cout << "CoCreateInstance Failed." << endl;
-
}
-
}
-
CoUninitialize();
-
}
------------- VB Client
Form1.frm
- VERSION 5.00
-
Begin VB.Form Form1
-
Caption = "Form1"
-
ClientHeight = 3090
-
ClientLeft = 60
-
ClientTop = 450
-
ClientWidth = 4680
-
LinkTopic = "Form1"
-
ScaleHeight = 3090
-
ScaleWidth = 4680
-
StartUpPosition = 3 'Windows Default
-
Begin VB.CommandButton Command1
-
Caption = "Command1"
-
Height = 735
-
Left = 1440
-
TabIndex = 0
-
Top = 720
-
Width = 1575
-
End
-
End
-
Attribute VB_Name = "Form1"
-
Attribute VB_GlobalNameSpace = False
-
Attribute VB_Creatable = False
-
Attribute VB_PredeclaredId = True
-
Attribute VB_Exposed = False
-
Private Sub Command1_Click()
-
Dim m As MYATLCOMLib.MyText
-
Set m = New MyText
-
Dim l As Long
-
-
m.MyAdd 1, 2, l
-
MsgBox "MyAdd(1,2) = " & l
-
-
Dim s As String
-
s = m.MyTxt
-
MsgBox "MyTxt = " & m.MyTxt 'FIXME wrong, 应该是 "Hi~~", 实际是 "MyTx"
-
-
-
MsgBox " myNum = " & m.MyNum
-
m.MyNum = 333
-
MsgBox " myNum = " & m.MyNum
-
End Sub
阅读(1160) | 评论(0) | 转发(0) |