티스토리 뷰

프로그래밍/MFC

C# COM 참조 여러 방법

에어버스 2017. 11. 12. 15:21
C# COM 참조 여러 방법

COM instance를 만드는 방법은 여러 가지가 있으니 선택해서 사용하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
 
//#import <mscorlib.tlb> raw_interfaces_only
#import "_COMTest.tlb" no_namespace named_guids
 
int main(void)
{
    long nResult;
    I_COMTest* cpi;
    CoInitialize(NULL);
    HRESULT hr = CoCreateInstance(CLSID_Class1, NULL, CLSCTX_INPROC_SERVER, IID_I_COMTest, reinterpret_cast<void**>(&cpi));
    // CLSID_Classs1 과 IID_I_COMTest 값은 _COMTest.tlh 에서 참조된다.
    // 함수들은 _COMTest.tli 에서 참조하는듯...
    if (hr != S_OK)
        return 0;
    cpi->computedAdd(78&nResult);
    printf("7 + 8 = %d\n", nResult);
 
    long nResult1 = cpi->computedAdd1(100200);
    printf("100 + 200 = %d\n", nResult1);
 
    char str[20= { 0, };
    strcpy_s(str, cpi->computedAdd2("123"));
    printf("%s\n\n", str);
 
    I_COMTestPtr pCom;
    hr = pCom.CreateInstance(CLSID_Class1);
    // CLSID_Classs1 과 IID_I_COMTest 값은 _COMTest.tlh 에서 참조된다.
    // 함수들은 _COMTest.tli 에서 참조하는듯...
    if (hr != S_OK)
        return 0;
    pCom->computedAdd(78&nResult);
    printf("7 + 8 = %d\n", nResult);
 
    nResult1 = pCom->computedAdd1(100200);
    printf("100 + 200 = %d\n", nResult1);
 
    ZeroMemory(str, 20);
    strcpy_s(str, pCom->computedAdd2("123"));
    printf("%s\n\n", str);
 
    hr = pCom.CreateInstance(("_COMTest.Class1"));
    if (hr != S_OK)
        return 0;
    pCom->computedAdd(78&nResult);
    printf("7 + 8 = %d\n", nResult);
 
    nResult1 = pCom->computedAdd1(100200);
    printf("100 + 200 = %d\n", nResult1);
 
    ZeroMemory(str, 20);
    strcpy_s(str, pCom->computedAdd2("123"));
    printf("%s\n", str);
    pCom.Release();
 
    CoUninitialize();
    getchar();
    return 0;
}
cs

11행
27행
42행 

실행결과

소스파일 : _COMTestClient.zip

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31