1 #include "../Simple2/Simple2_i.h"
2 #include "../Simple2/Simple2_i.c"
3
4 int _tmain(int argc, _TCHAR* argv[])
5 {
6 CoInitialize(NULL);
7
8 IUnknown *pUnk;
9 IFunc *pFunc;
10 HRESULT hr;
11
12 try
13 {
14 //============================================
15 // get the interface of IFunc
16 hr = CoCreateInstance(CLSID_Func, NULL, CLSCTX_INPROC_SERVER, IID_IFunc, (LPVOID*)&pUnk);
17 if (FAILED(hr))
18 {
19 throw("COM object is not registered.\n");
20 }
21 hr = pUnk->QueryInterface(IID_IFunc, (LPVOID*)&pFunc);
22 if (FAILED(hr))
23 {
24 throw("No IID_IFunc interface.\n");
25 }
26 //============================================
27 // begin to invoke the com object.
28 int ret;
29
30 // invoke Add method
31 hr = pFunc->Add(1, 2, &ret);
32 if (FAILED(hr))
33 {
34 throw("Invoke Add method failed!\n");
35 }
36 printf("the return value of Add is:%d\n", ret);
37
38 // invoke subtract method
39 BSTR s1 = SysAllocString(L"hello");
40 BSTR s2 = SysAllocString(L" world");
41 BSTR s3;
42 hr = pFunc->Cat(s1, s2, &s3);
43
44 if (FAILED(hr))
45 {
46 throw("Invoke Cat method failed!\n");
47 }
48 char *ansiRet;
49 USES_CONVERSION;
50 ansiRet = W2A(s3);
51 printf("the return value of Cat is :%s\n", ansiRet);
52
53 // s3 should be released now
54 SysFreeString(s3);
55
56 // don't forget to release the object.
57 pFunc->Release();
58 pUnk->Release();
59 }
60 catch (LPCSTR str)
61 {
62 printf("%s", str);
63 }
64
65
66 CoUninitialize();
67 return 0;
68 }