메뉴 닫기

SAFEARRAY를 사용한 VARIANT ARRAY(배열) 다루기(VC++)

VB의 경우 VARIANT관련해서 조작은 아주 간편하다. VB만 사용하는 개발자라면 VARIANT  Type의 자료형이 존재한다는 사실조차 의식하지 못할 정도이다.

MFC (VC++)에서 VARIANT 와 VARIANT ARRAY(배열)를 사용하는 방법은 다소 까다롭다.
물론 내가 처음 VC++을 접할 때 OLE,COM등에 대해 별로 관심이 깊지 않았던 탓에 VARIANT를 다룰 기회가 없었던 이유도 있지만, 사용의 필요성을 느끼지 못했던 것도 사실이다.

그런데 어느 순간부터 사용하는 개발툴이 하나 둘 늘어나기 시작했다.
현업에서 일을 하다 보면 내가 사용하지 않았던 개발언어를 다루어야 할 경우가 생긴다. 다른 소스를 참조해야 하거나 여러 부서 또는 다른 회사와 공동작업을 해야 하는 경우도 있다.
이럴 때 dll,ATL,COM 을 사용하면 개발언어간의 장벽 사이에서 유연하게 재사용 가능한 코드를 작성할 수 있다.  그런데 라이브러리에 서 작성한 함수에 인자로 전달하는 변수형의 호환성이 항상 문제가 된다. VARIANT는 다른 종류의 개발 언어 사이에서 서로호환 되는 변수형이다. 

또한 VARIANT ARRAY(배열)을 다루는 함수는 SAFEARRAY임을 잘 기억하자.

아래의 예는 복합적인 변수형식을 VARINT Type 포인터로 넘겨서 처리 하는 방법 이다.

//thread에서 처리 가능하도록 Void Type의 자료넘김
DWORD  Function(LPVOID lpParam)
{
LPMITEM p_item=(LPMITEM)lpParam;//파라메터로 넘기기 위해 정의된 구조체
p_item->b_Thread_Run=TRUE;
//내가만든 ATL Class를 인자로 넘겨 받는다
CComObject<CMyclass>* p_Simul
=(CComObject<CMyclass>*)p_item->p_sim;
p_Simul->Test((int*)&b_OK);  //ATL Class에 정의된 함수 호출
////////////////////////////////////////
// 함수의 실행 결과를 다중배열 복합 Data로 받기 위한 Variant Point
VariantInit(&p_item->v_Rtn);   // Variant 초기화
V_VT(&p_item->v_Rtn) = (VT_VARIANT|VT_ARRAY);//Variant Type의 배열선언
//2개의 Variant 배열생성
V_ARRAY(&p_item->v_Rtn) = SafeArrayCreateVector(VT_VARIANT, 0, 2);
////////////////////////////////////////
VARIANT *p_var=NULL,*pv_child=NULL;
VARIANT t_var;
//Variant의 Array를 가져오기 및 Lock 해제
SafeArrayAccessData(p_item->v_Rtn.parray, reinterpret_cast<void**>(&p_var));
///첫 번째 배열에 2개의 Variant 배열을 할당하여 만들기
V_VT(&p_var[0]) = (VT_VARIANT|VT_ARRAY);
V_ARRAY(&p_var[0]) = SafeArrayCreateVector(VT_VARIANT, 0, 2);
//할당된 배열에서 Variant Point가져오기
SafeArrayAccessData(p_var[0].parray, reinterpret_cast<void**>(&pv_child));
//문자열 형태의 변수 넣기
t_var=_variant_t("NAME");
VariantCopy(&pv_child[0],&t_var);
//넘겨진 클래스의 멤버변수의 값을 가져오기
t_var=_variant_t(p_Simul->id_Name);
VariantCopy(&pv_child[1],&t_var);
//첫 번째 배열에
Variant 배열을 넣고 Lock실행 SafeArrayUnaccessData(p_var[0].parray);
/////////////////////////////////////////// /// 첫 번째 배열과 같은 방법으로 두 번째 배열도 값을 할당
V_VT(&p_var[1]) = (VT_VARIANT|VT_ARRAY);
V_ARRAY(&p_var[1]) = SafeArrayCreateVector(VT_VARIANT, 0, 2);
SafeArrayAccessData(p_var[1].parray, reinterpret_cast<void**>(&pv_child));
t_var=_variant_t("RESULT");
VariantCopy(&pv_child[0],&t_var);
t_var=_variant_t(b_OK);
VariantCopy(&pv_child[1],&t_var);
SafeArrayUnaccessData(p_var[1].parray);
/////////////////////////////////////////// //최상위 배열
Lock SafeArrayUnaccessData(p_item->v_Rtn.parray);
VariantClear(&t_var);
VariantClear(p_var);
VariantClear(pv_child);
p_item->b_Thread_Run=FALSE;
p_item=NULL;
p_Simul=NULL;
return 0;
}

여기서 주목 해야 할 부분은 SafeArrayAccessData에서 Variant Point로 접근하는 부분과 SafeArrayUnaccessData를 사용해서 Lock을 시키는 부분이다.
 

35 Comments

  1. g also

    Howdy! I know this is somewhat off-topic however I had to ask.
    Does managing a well-established blog like yours take a massive amount
    work? I’m brand new to operating a blog however I do write
    in my journal every day. I’d like to start a blog so I will be able to share my
    own experience and views online. Please let me know if you have any recommendations or tips for new aspiring bloggers.

    Appreciate it!

  2. https://tinyurl.com/rsacwgxy g

    I loved as much as you’ll receive carried out right here.

    The sketch is attractive, your authored material stylish.

    nonetheless, you command get got an nervousness over
    that you wish be delivering the following.
    unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case
    you shield this increase.

  3. Teena Doyscher

    I just want to mention I am just all new to blogging and site-building and truly liked you’re web blog. Most likely I’m likely to bookmark your website . You surely have fantastic articles and reviews. With thanks for sharing your webpage.

  4. aluminium security doors

    Excellent blog! Do you have any tips and hints for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely confused .. Any tips? Bless you!

  5. John Deere Diagnostic and Test Manuals

    Hi, very exciting posting. My sister and I have recently been looking for comprehensive details about this type of stuff for some time, however we could hardly until now. Do you consider you can create several youtube videos about this, I believe your webblog would be far more thorough in case you did. In any other case, oh well. I am going to be checking on this web site within the forseeable future. Email me to maintain me up to date. granite countertops cleveland

  6. how to sale cbd oil online

    Youre so great! I do not suppose Ive check out anything similar to this before. So good to find somebody with some initial thoughts on this subject. realy thanks for starting this up. this website is something that is required on the internet, someone with a little creativity. useful job for bringing something brand-new to the net!

  7. how to get pure cbd oil for sale

    After study a few of the blog posts on your web site now, and also I genuinely like your method of blog writing. I bookmarked it to my bookmark site list as well as will be inspecting back soon. Pls have a look at my web site too as well as let me understand what you believe.

  8. web hosting providers

    After I initially commented I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is added I recieve four emails
    with the exact same comment. There has to be a way you can remove me from that service?

    Appreciate it!

  9. such a good point

    There are definitely a great deal of details like that to take into account. That is an excellent indicate raise. I provide the thoughts above as general ideas however clearly there are inquiries like the one you raise where one of the most vital thing will certainly be operating in straightforward good faith. I don?t understand if best methods have arised around things like that, however I am sure that your work is clearly recognized as a level playing field. Both boys and ladies really feel the effect of simply a moment?s satisfaction, for the remainder of their lives.

  10. index

    After study a few of the post on your web site now, and also I genuinely like your means of blogging. I bookmarked it to my bookmark web site list and also will certainly be checking back quickly. Pls take a look at my web site too and let me understand what you assume.

  11. her explanation

    Youre so great! I do not intend Ive read anything like this prior to. So nice to find someone with some original ideas on this subject. realy thanks for beginning this up. this web site is something that is required on the web, someone with a little originality. useful job for bringing something brand-new to the web!

  12. have a peek here

    Can I simply claim what a relief to locate somebody that in fact knows what theyre talking about on the web. You definitely know exactly how to bring a problem to light and also make it vital. Even more people require to read this as well as understand this side of the story. I cant think youre not extra popular because you definitely have the gift.

  13. right here

    Good post. I learn something a lot more tough on various blogs day-to-day. It will certainly always be stimulating to review content from various other authors and also practice a little something from their store. I?d like to make use of some with the material on my blog site whether you don?t mind. Natually I?ll give you a web link on your internet blog site. Many thanks for sharing.

  14. that site

    Youre so awesome! I do not intend Ive check out anything like this before. So great to locate somebody with some original ideas on this subject. realy thanks for beginning this up. this web site is something that is needed online, a person with a little originality. helpful work for bringing something brand-new to the internet!

  15. go now

    Aw, this was a really wonderful blog post. In idea I would like to place in creating like this additionally? taking some time and actual effort to make a great article? however what can I claim? I procrastinate alot and also never appear to get something done.

  16. 핑백:how to rank my site in alexa

댓글 남기기

이메일은 공개되지 않습니다.