môn lập trình hướng đối tượnganhtt/contents/teaching/semester10910... · web viewsau...

33
Môn Lập trình hướng đối tượng Bài thực hành số 14 : Xây dựng COM & ứng dụng dùng COM 14.1 TÍNH CHẤT CỦA COM COM (Component Object Model) là công nghệ của Microsoft để xây dựng linh kiện phần mềm có tầm vực sử dụng ở cấp hệ thống (trên Windows của máy đang cài COM và các máy khác trong mạng chứa máy đó). 14.2 QUI TRÌNH XÂY DỰNG 1 COM Giả sử ta cần xây dựng COM chuyển mã tiếng Việt tên là UniTrans với interface sử dụng gồm 2 tác vụ sau : // chuyển chuỗi từ Unicode dựng sẳn về tổ hợp 1. int Pre2Comp(BSTR src, int len, BSTR dst); // chuyển chuỗi từ Unicode tổ hợp về dựng sẳn 2. int Comp2Pre(BSTR src, int len, BSTR dst); 1. Để tạo 1 COM bằng VC++, trước hết chạy Visual C++ 6.0 từ Windows (thí dụ chọn mục Start.Programs.Microsoft Visual Studio 6.0/Microsoft Visual C++ 6.0). 2. Chọn menu File.New để tạo Project quản lý COM. Khi cửa sổ New hiển thị, chọn tab Project, chọn mục "ATL COM ApppWizard", chọn Location, nhập tên Project :

Upload: others

Post on 09-Apr-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Môn Lập trình hướng đối tượngBài thực hành số 14 : Xây dựng COM & ứng dụng dùng COM

14.1 TÍNH CHẤT CỦA COMCOM (Component Object Model) là công nghệ của Microsoft

để xây dựng linh kiện phần mềm có tầm vực sử dụng ở cấp hệ thống (trên Windows của máy đang cài COM và các máy khác trong mạng chứa máy đó).

14.2 QUI TRÌNH XÂY DỰNG 1 COMGiả sử ta cần xây dựng COM chuyển mã tiếng Việt tên là UniTrans với interface sử dụng gồm 2 tác vụ sau :// chuyển chuỗi từ Unicode dựng sẳn về tổ hợp1. int Pre2Comp(BSTR src, int len, BSTR dst);// chuyển chuỗi từ Unicode tổ hợp về dựng sẳn2. int Comp2Pre(BSTR src, int len, BSTR dst);

1. Để tạo 1 COM bằng VC++, trước hết chạy Visual C++ 6.0 từ Windows (thí dụ chọn mục Start.Programs.Microsoft Visual Studio 6.0/Microsoft Visual C++ 6.0). 2. Chọn menu File.New để tạo Project quản lý COM. Khi cửa sổ New hiển thị, chọn tab Project, chọn mục "ATL COM ApppWizard", chọn Location, nhập tên Project :

3. Chọn button OK, cửa sổ Wizard sau đây sẽ hiển thị :

4. Chọn button Finish để chấp nhận các tham số mặc định, cửa sổ tổng kết các tham số về Project sau đây sẽ hiển thị :

5. Chọn button OK để chấp nhận tạo Project, cửa sổ ban đầu sẽ hiển thi như sau. Để tạo mới 1 COM trong Project hiện hành, chọn menu Insert.New ATL Object... :

6. Cửa sổ ATL Object Wizard sẽ hiện thị như sau :

7. Chọn mục Objects, chọn icon "Simple Object" rồi button Next để hiển thị cửa sổ Properties sau đây. Nhập tên của COM vào textbox "Short Name", các field khác sẽ tự động được cập nhật theo :

8. Ấn phải chuột trên interface IUniTrans, rồi chọn mục "Add Method..." để thêm 1 tác vụ mới vào COM :

9. Đặc tả tác vụ Pre2Comp : tên tác vụ, danh sách tham số hình thức rồi chọn OK để tạo ra tác vụ đó. Lưu ý ứng với mỗi tham số của tác vụ, ta phải xác định các thuộc tính của nó như chiều chuyển động ([in] | [out] | [inout] | [out, retval]), kiểu dữ liệu, tên gợi nhớ của tham số theo cú pháp của ngôn ngữ MIDL :

Cụ thể, trong textbox "parameters" của tác vụ Pre2Comp và Comp2Pre, bạn hãy nhập chuỗi đặc tả sau :[in] BSTR src, [in] int len, [out] BSTR* dst, [out,retval] int* ret10. Tiếp tục đặc tả cho tác vụ Comp2Pre như y như tác vụ Pre2Comp11. Trong cửa sổ Project, mở rộng mục root (tên là VnTrans classes), mở rộng interface IUniTrans để thấy danh sách các tác vụ vừa tạo ra. Ấn kép chuột trên tác vụ Pre2Comp để hiển thị cửa sổ đặc tả tác vụ đó (bằng ngôn ngữ MIDL trong file VnTrans.idl). Chỉ xem và quan sát chứ không cần hiệu chỉnh gì cả :

12. Trong cửa sổ Project, mở rộng mục CUniTrans, IUniTrans để thấy danh sách các method tương ứng với các tác vụ trong interface. Ấn kép chuột trên tác vụ Pre2Comp để hiển thị cửa sổ soạn code cho nó rồi viết code theo chức năng kỳ vọng :

13. Tiếp tục viết code cho method Comp2Pre theo chức năng kỳ vọng. Code cụ thể 2 hàm như sau :// UniTrans.cpp : Implementation of CUniTrans#include "stdafx.h"#include "VnTrans.h"#include "UniTrans.h"

/////////////////////////////////////////////////////////////////////////////// CUniTrans

// =======================================================================#define VN_CAA 0x301#define VN_CGA 0x300#define VN_CHA 0x309

#define VN_CTD 0x303#define VN_CDB 0x323#define VN_BRE 0x306#define VN_CIR 0x302#define VN_HOR 0x31b

STDMETHODIMP CUniTrans::VnComp2Pre(BSTR SrcStr, int cnt, BSTR* DstStr, int* ret){

// TODO: Add your implementation code hereint i;WORD c, a1, a2;int idxo = 0;i = 0;BSTR dst = *DstStr;while(i < cnt) {

c = SrcStr[i++];switch (c) {case 'a' :

if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xe1; goto precomp;case VN_CGA: c = 0xe0; goto precomp;case VN_CHA: c = 0x1ea3; goto precomp;case VN_CTD: c = 0xe3; goto precomp;case VN_CDB: c = 0x1ea1; goto precomp;case VN_BRE:

c = 0x103;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1eaf; goto precomp;case VN_CGA: c = 0x1eb1; goto precomp;case VN_CHA: c = 0x1eb3; goto precomp;case VN_CTD: c = 0x1eb5; goto precomp;case VN_CDB: c = 0x1eb7; goto precomp;default : i--; goto precomp;}

case VN_CIR:c = 0xe2;if (i == cnt) goto precomp;

a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ea5; goto precomp;case VN_CGA: c = 0x1ea7; goto precomp;case VN_CHA: c = 0x1ea9; goto precomp;case VN_CTD: c = 0x1eab; goto precomp;case VN_CDB: c = 0x1ead; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'A' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xc1; goto precomp;case VN_CGA: c = 0xc0; goto precomp;case VN_CHA: c = 0x1ea2; goto precomp;case VN_CTD: c = 0xc3; goto precomp;case VN_CDB: c = 0x1ea0; goto precomp;case VN_BRE:

c = 0x102;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1eae; goto precomp;case VN_CGA: c = 0x1eb0; goto precomp;case VN_CHA: c = 0x1eb2; goto precomp;case VN_CTD: c = 0x1eb4; goto precomp;case VN_CDB: c = 0x1eb6; goto precomp;default : i--; goto precomp;}

case VN_CIR:c = 0xc2;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ea4; goto precomp;case VN_CGA: c = 0x1ea6; goto precomp;case VN_CHA: c = 0x1ea8; goto precomp;case VN_CTD: c = 0x1eaa; goto precomp;case VN_CDB: c = 0x1eac; goto precomp;

default : i--; goto precomp;}

default: i--; goto precomp;}

case 'e' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xe9; goto precomp;case VN_CGA: c = 0xe8; goto precomp;case VN_CHA: c = 0x1ebb; goto precomp;case VN_CTD: c = 0x1ebd; goto precomp;case VN_CDB: c = 0x1eb9; goto precomp;case VN_CIR:

c = 0xea;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ebf; goto precomp;case VN_CGA: c = 0x1ec1; goto precomp;case VN_CHA: c = 0x1ec3; goto precomp;case VN_CTD: c = 0x1ec5; goto precomp;case VN_CDB: c = 0x1ec7; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'E' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xc9; goto precomp;case VN_CGA: c = 0xc8; goto precomp;case VN_CHA: c = 0x1eba; goto precomp;case VN_CTD: c = 0x1ebc; goto precomp;case VN_CDB: c = 0x1eb8; goto precomp;case VN_CIR:

c = 0xca;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ebe; goto precomp;

case VN_CGA: c = 0x1ec0; goto precomp;case VN_CHA: c = 0x1ec2; goto precomp;case VN_CTD: c = 0x1ec4; goto precomp;case VN_CDB: c = 0x1ec6; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'i' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xed; goto precomp;case VN_CGA: c = 0xec; goto precomp;case VN_CHA: c = 0x1ec9; goto precomp;case VN_CTD: c = 0x129; goto precomp;case VN_CDB: c = 0x1ecb; goto precomp;default: i--; goto precomp;}

case 'I' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xcd; goto precomp;case VN_CGA: c = 0xcc; goto precomp;case VN_CHA: c = 0x1ec8; goto precomp;case VN_CTD: c = 0x128; goto precomp;case VN_CDB: c = 0x1eca; goto precomp;default: i--; goto precomp;}

case 'o' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xf3; goto precomp;case VN_CGA: c = 0xf2; goto precomp;case VN_CHA: c = 0x1ecf; goto precomp;case VN_CTD: c = 0xf5; goto precomp;case VN_CDB: c = 0x1ecd; goto precomp;case VN_CIR:

c = 0xf4;if (i == cnt) goto precomp;

a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ed1; goto precomp;case VN_CGA: c = 0x1ed3; goto precomp;case VN_CHA: c = 0x1ed5; goto precomp;case VN_CTD: c = 0x1ed7; goto precomp;case VN_CDB: c = 0x1ed9; goto precomp;default : i--; goto precomp;}

case VN_HOR:c = 0x1a1;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1edb; goto precomp;case VN_CGA: c = 0x1edd; goto precomp;case VN_CHA: c = 0x1edf; goto precomp;case VN_CTD: c = 0x1ee1; goto precomp;case VN_CDB: c = 0x1ee3; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'O' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xd3; goto precomp;case VN_CGA: c = 0xd2; goto precomp;case VN_CHA: c = 0x1ece; goto precomp;case VN_CTD: c = 0xd5; goto precomp;case VN_CDB: c = 0x1ecc; goto precomp;case VN_CIR:

c = 0xd4;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ed0; goto precomp;case VN_CGA: c = 0x1ed2; goto precomp;case VN_CHA: c = 0x1ed4; goto precomp;case VN_CTD: c = 0x1ed6; goto precomp;case VN_CDB: c = 0x1ed8; goto precomp;

default : i--; goto precomp;}

case VN_HOR:c = 0x1a0;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1eda; goto precomp;case VN_CGA: c = 0x1edc; goto precomp;case VN_CHA: c = 0x1ede; goto precomp;case VN_CTD: c = 0x1ee0; goto precomp;case VN_CDB: c = 0x1ee2; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'u' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xfa; goto precomp;case VN_CGA: c = 0xf9; goto precomp;case VN_CHA: c = 0x1ee7; goto precomp;case VN_CTD: c = 0x169; goto precomp;case VN_CDB: c = 0x1ee5; goto precomp;case VN_HOR:

c = 0x1b0;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ee9; goto precomp;case VN_CGA: c = 0x1eeb; goto precomp;case VN_CHA: c = 0x1eed; goto precomp;case VN_CTD: c = 0x1eef; goto precomp;case VN_CDB: c = 0x1ef1; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'U' :if (i == cnt) goto precomp;a1 = SrcStr[i++];

switch (a1) {case VN_CAA: c = 0xda; goto precomp;case VN_CGA: c = 0xd9; goto precomp;case VN_CHA: c = 0x1ee6; goto precomp;case VN_CTD: c = 0x168; goto precomp;case VN_CDB: c = 0x1ee4; goto precomp;case VN_HOR:

c = 0x1af;if (i == cnt) goto precomp;a2 = SrcStr[i++];switch (a2) {case VN_CAA: c = 0x1ee8; goto precomp;case VN_CGA: c = 0x1eea; goto precomp;case VN_CHA: c = 0x1eec; goto precomp;case VN_CTD: c = 0x1eee; goto precomp;case VN_CDB: c = 0x1ef0; goto precomp;default : i--; goto precomp;}

default: i--; goto precomp;}

case 'y' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xfd; goto precomp;case VN_CGA: c = 0x1ef3; goto precomp;case VN_CHA: c = 0x1ef7; goto precomp;case VN_CTD: c = 0x1ef9; goto precomp;case VN_CDB: c = 0x1ef5; goto precomp;default: i--; goto precomp;}

case 'Y' :if (i == cnt) goto precomp;a1 = SrcStr[i++];switch (a1) {case VN_CAA: c = 0xdd; goto precomp;case VN_CGA: c = 0x1ef2; goto precomp;case VN_CHA: c = 0x1ef6; goto precomp;case VN_CTD: c = 0x1ef8; goto precomp;case VN_CDB: c = 0x1ef4; goto precomp;default: i--; goto precomp;}

case 'd': if (i == cnt) goto precomp; a1 = SrcStr[i++]; switch (a1){ case 0x2d: c = 0x111; goto precomp; default: i--; goto precomp; } case 'D': if (i == cnt) goto precomp; a1 = SrcStr[i++]; switch (a1){ case 0x2d: c = 0x110; goto precomp; default: i--; goto precomp; }

}precomp:

dst[idxo++] = c;}dst[idxo] = 0;*ret = idxo;//MessageBoxW (NULL,SrcStr,*DstStr,MB_OK);

return S_OK;}

WORD VnPreComposedChar[134] = {0x00c0,0x00c1,0x00c2,0x00c3,0x00c8,0x00c9,0x00ca,0x00cc,0x00cd,0x00d2,0x00d3,0x00d4,0x00d5,0x00d9,0x00da,0x00dd,

0x00e0,0x00e1,0x00e2,0x00e3,0x00e8,0x00e9,0x00ea,0x00ec,0x00ed,0x00f2,0x00f3,0x00f4,0x00f5,0x00f9,0x00fa,0x00fd,0x0102,0x0103,0x0110,0x0111,0x0128,0x0129,0x0168,0x0169,0x01a0,0x01a1,0x01af,0x01b0,0x1ea0,0x1ea1,0x1ea2,0x1ea3,0x1ea4,0x1ea5,0x1ea6,0x1ea7,0x1ea8,0x1ea9,0x1eaa,0x1eab,0x1eac,0x1ead,

0x1eae,0x1eaf,0x1eb0,0x1eb1,0x1eb2,0x1eb3,0x1eb4,0x1eb5,0x1eb6,0x1eb7,0x1eb8,0x1eb9,0x1eba,0x1ebb,0x1ebc,0x1ebd,0x1ebe,0x1ebf,0x1ec0,0x1ec1,0x1ec2,0x1ec3,0x1ec4,0x1ec5,0x1ec6,0x1ec7,0x1ec8,0x1ec9,0x1eca,0x1ecb,0x1ecc,0x1ecd,0x1ece,0x1ecf,0x1ed0,0x1ed1,0x1ed2,0x1ed3,0x1ed4,0x1ed5,0x1ed6,0x1ed7,

0x1ed8,0x1ed9,0x1eda,0x1edb,0x1edc,0x1edd,0x1ede,0x1edf,0x1ee0,0x1ee1,0x1ee2,0x1ee3,0x1ee4,0x1ee5,0x1ee6,0x1ee7,0x1ee8,0x1ee9,0x1eea,0x1eeb,0x1eec,0x1eed,0x1eee,0x1eef,0x1ef0,0x1ef1,0x1ef2,0x1ef3,0x1ef4,0x1ef5,0x1ef6,0x1ef7,0x1ef8,0x1ef9};

WORD VnComposedChar[134][4] = {{0x0041,0x0300,0},{0x0041,0x0301,0},{0x0041,0x0302,0},{0x0041,0x0303,0},{0x0045,0x0300,0},

{0x0045,0x0301,0},{0x0045,0x0302,0},{0x0049,0x0300,0},{0x0049,0x0301,0},{0x004f,0x0300,0},{0x004f,0x0301,0},{0x004f,0x0302,0},{0x004f,0x0303,0},{0x0055,0x0300,0},{0x0055,0x0301,0},{0x0059,0x0301,0},{0x0061,0x0300,0},{0x0061,0x0301,0},{0x0061,0x0302,0},{0x0061,0x0303,0},{0x0065,0x0300,0},{0x0065,0x0301,0},{0x0065,0x0302,0},{0x0069,0x0300,0},{0x0069,0x0301,0},{0x006f,0x0300,0},{0x006f,0x0301,0},{0x006f,0x0302,0},{0x006f,0x0303,0},{0x0075,0x0300,0},{0x0075,0x0301,0},{0x0079,0x0301,0},{0x0041,0x0306,0},{0x0061,0x0306,0},{0x0110,0},{0x0111,0},{0x0049,0x0303,0},{0x0069,0x0303,0},{0x0055,0x0303,0},{0x0075,0x0303,0},{0x004f,0x031b,0},{0x006f,0x031b,0},{0x0055,0x031b,0},{0x0075,0x031b,0},{0x0041,0x0323,0},{0x0061,0x0323,0},{0x0041,0x0309,0},

{0x0061,0x0309,0},{0x0041,0x0302,0x0301,0},{0x0061,0x0302,0x0301,0},{0x0041,0x0302,0x0300,0},{0x0061,0x0302,0x0300,0},{0x0041,0x0302,0x0309,0},{0x0061,0x0302,0x0309,0},{0x0041,0x0302,0x0303,0},{0x0061,0x0302,0x0303,0},{0x0041,0x0302,0x0323,0},{0x0061,0x0302,0x0323,0},{0x0041,0x0306,0x0301,0},{0x0061,0x0306,0x0301,0},{0x0041,0x0306,0x0300,0},{0x0061,0x0306,0x0300,0},{0x0041,0x0306,0x0309,0},{0x0061,0x0306,0x0309,0},{0x0041,0x0306,0x0303,0},{0x0061,0x0306,0x0303,0},{0x0041,0x0306,0x0323,0},{0x0061,0x0306,0x0323,0},{0x0045,0x0323,0},{0x0065,0x0323,0},{0x0045,0x0309,0},{0x0065,0x0309,0},{0x0045,0x0303,0},{0x0065,0x0303,0},{0x0045,0x0302,0x0301,0},{0x0065,0x0302,0x0301,0},{0x0045,0x0302,0x0300,0},{0x0065,0x0302,0x0300,0},{0x0045,0x0302,0x0309,0},{0x0065,0x0302,0x0309,0},{0x0045,0x0302,0x0303,0},{0x0065,0x0302,0x0303,0},{0x0045,0x0302,0x0323,0},{0x0065,0x0302,0x0323,0},{0x0049,0x0309,0},{0x0069,0x0309,0},{0x0049,0x0323,0},{0x0069,0x0323,0},{0x004f,0x0323,0},

{0x006f,0x0323,0},{0x004f,0x0309,0},{0x006f,0x0309,0},{0x004f,0x0302,0x0301,0},{0x006f,0x0302,0x0301,0},{0x004f,0x0302,0x0300,0},{0x006f,0x0302,0x0300,0},{0x004f,0x0302,0x0309,0},{0x006f,0x0302,0x0309,0},{0x004f,0x0302,0x0303,0},{0x006f,0x0302,0x0303,0},{0x004f,0x0302,0x0323,0},{0x006f,0x0302,0x0323,0},{0x004f,0x031b,0x0301,0},{0x006f,0x031b,0x0301,0},{0x004f,0x031b,0x0300,0},{0x006f,0x031b,0x0300,0},{0x004f,0x031b,0x0309,0},{0x006f,0x031b,0x0309,0},{0x004f,0x031b,0x0303,0},{0x006f,0x031b,0x0303,0},{0x004f,0x031b,0x0323,0},{0x006f,0x031b,0x0323,0},{0x0055,0x0323,0},{0x0075,0x0323,0},{0x0055,0x0309,0},{0x0075,0x0309,0},{0x0055,0x031b,0x0301,0},{0x0075,0x031b,0x0301,0},{0x0055,0x031b,0x0300,0},{0x0075,0x031b,0x0300,0},{0x0055,0x031b,0x0309,0},{0x0075,0x031b,0x0309,0},{0x0055,0x031b,0x0303,0},{0x0075,0x031b,0x0303,0},{0x0055,0x031b,0x0323,0},{0x0075,0x031b,0x0323,0},{0x0059,0x0300,0},{0x0079,0x0300,0},{0x0059,0x0323,0},{0x0079,0x0323,0},{0x0059,0x0309,0},

{0x0079,0x0309,0},{0x0059,0x0303,0},{0x0079,0x0303,0}};

STDMETHODIMP CUniTrans::VnPre2Comp(BSTR SrcStr, int cnt, BSTR* DstStr, int* ret){

// TODO: Add your implementation code herelong i, j;long min, max;long idxo = 0;BSTR dst = *DstStr;for (i=0; i<cnt; i++) { min = 0; max = 133; while (min <= max) { j = (max + min) >> 1; if (SrcStr[i] == VnPreComposedChar[j]) break; if (SrcStr[i] <VnPreComposedChar[j]) max = j-1; else min = j+1; } if (min <= max) { int k = 0; while (VnComposedChar[j][k]) dst[idxo++] = VnComposedChar[j][k++]; } else dst[idxo++] = SrcStr[i];}dst[idxo] = 0;*ret= idxo;//MessageBoxW (NULL,SrcStr,*DstStr,MB_OK);

return S_OK;}Sau khi viết xong code (hay từng phần), có thể dịch để kiểm tra lỗi từ vựng và cú pháp bằng cách chọn menu Build.ReBuild All (hay Build VnTrans.dll) :

Sau khi dịch xong, VC++ sẽ tự động đăng ký file COM (VnTrans.dll) vào Windows để mọi ứng dụng trên máy có thể dùng nó. Nếu copy file COM qua máy khác để dùng, bạn phải đăng ký COM vào Windows của máy đó thủ công hay bằng lập trình (dùng tiện ích regsrv32.exe).14.3 SỬ DỤNG COM TRONG ỨNG DỤNG C++Bây giờ ta hãy viết 1 ứng dụng nhỏ để demo việc dùng COM vừa tạo được ở bước

trước.1. Chạy Visual C++ 6.0 từ Windows (thí dụ chọn mục Start.Programs.Microsoft

Visual Studio 6.0/Microsoft Visual C++ 6.0). Màn hình của VC++ được hiển thị như sau :

2. Bước 1 : tạo project phần mềm bằng Wizard. Từ menu File, chọn mục New..., cửa sổ sau được hiển thị. Chọn button Projects, chọn mục MFC AppWizard (exe), chọn thư mục chứa project chương trình cần tạo ở textbox "Location", thí dụ thư mục d:\Samples\BkTrans. Nhập tên project chứa ứng dụng vào textbox "Project Name" (thí dụ TestCOMVnTrans) như vậy các file tạo thành ứng dụng sẽ được chứa trong thư mục d:\Samples\BkTrans\TestCOMVnTrans :

3. Chọn button Ok, cửa sổ Step 1 sau được hiển thị để bạn chọn loại giao diện của chương trình. Chương trình của ta dùng cửa sổ dạng hộp thoại, chọn mục "Dialog based " rồi chọn button Finish để chấp nhận các tham số mặc định còn lại :

4. Sau khi hoàn tất 4 bước Wizard, nàn hình cho chương trình sẽ được hiển thị như sau :

5. Hãy thiết kế form giao diện như sau. Có 2 listbox hiển thị danh sách bản mã nguồn và đích, 2 textbox và 1 button phục vụ chuyển mã chuỗi văn bản, 2 textbox và 1 button phục vụ chuyển mã tên font từ bản mã này sang bản mã khác :

6. Tạo 2 hàm xử lý sự kiện click chuột vào 2 button, rồi viết code cho 2 hàm đó : //khai báo sử dụng file *.dll chứa COM cần dùng#import "d:\samples\VnTrans\VnTrans.dll" rename( "EOF", "adoEOF" ) // định nghĩa biến tham khảo đến COM cần dùngVNTRANSLib::IUniTransPtr m_bktrans;//( __uuidof( BKTRANSLib::IBkTransLib) );//hàm khởi tạo form ứng dụngBOOL CTestCOMVnTransDlg::OnInitDialog() {

CDialog::OnInitDialog();// code có sẵn// TODO: Add extra initialization here::CoInitialize(NULL);if (m_bktrans.CreateInstance

(__uuidof(VNTRANSLib::UniTrans)) != S_OK) {MessageBox(_T("Khong khoi dong duoc UniTrans"));return FALSE;

} return TRUE; // return TRUE unless you set the focus to a control

}

// hàm xử lý button Pre2Comp để chuyển chuỗi từ dựng sẳn sang tổ hợpvoid CTestCOMVnTransDlg::OnPre2Comp() {

// TODO: Add your control notification handler code hereUpdateData(TRUE);BSTR ps = m_srcstr.GetBuffer(50);long cnt = wcslen(ps);BSTR pd = new WORD(cnt*2);long len = m_bktrans->VnPre2Comp(ps,cnt,&pd);m_dststr = pd;UpdateData(FALSE);

}

// hàm xử lý button Comp2Pre để chuyển chuỗi từ tổ hợp sang dựng sẳnvoid CTestCOMVnTransDlg::OnComp2Pre() {

// TODO: Add your control notification handler code hereUpdateData(TRUE);BSTR ps = m_srcstr.GetBuffer(50);long cnt = wcslen(ps);BSTR pd = new WORD(cnt);long len = m_bktrans->VnComp2Pre(ps,cnt,&pd);m_dststr = pd;UpdateData(FALSE);

}

7. Dịch và chạy thử ứng dụng.