Microsoft MVP성태의 닷넷 이야기
글쓴 사람
쿠리마 (coolfh at empal.com)
홈페이지
첨부 파일
[code.zip]    

안녕하세요. 고수님들께 도움을 받기 위해 질문 올립니다.

C# COM Server 코드는 아래와 같습니다.

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using System.Reflection;

namespace MyInterop
{
    [Guid("3E0E2EB2-CC13-40fb-9346-34809CB2418C")]
    public interface IMyDotNetInterface
    {
        void ShowDialog(out tagUnitEx[] pArrUnit);
    }

    [ClassInterface(ClassInterfaceType.None)]
    [Guid("3A13EB34-3930-4e06-A3EB-10724CCC2F4B")]
    public class MyDotNetClass : IMyDotNetInterface
    {
        public MyDotNetClass() {}

        public void ShowDialog(out tagUnitEx[] pArrUnit)
        {
            tagUnitEx[] arrIndex;
            arrIndex = new tagUnitEx[3];

            arrIndex[0].strName = "coolfh";
            arrIndex[0].nIndex = 11;
            arrIndex[1].strName = "ssim";
            arrIndex[1].nIndex = 21;
            arrIndex[2].strName = "mist93";
            arrIndex[2].nIndex = 31;
            
            pArrUnit = arrIndex;
        }
    }

    public struct tagUnitEx
    {
        public string strName;
        public long nIndex;
    }
}

코드에서 처럼 tagUnitEx란 구조체에 string 및 long type의 맴버를 선언했고, Client쪽에서 pArrUnit을 요청하면 COM Server에서 배열의 크기(코드에서는 3)를 할당하고 값을 채워서 넘겨주는 방식입니다.

tagUnitEx 맴버로 long type만 선언했을 경우에는 잘 동작하는데요. string type 맴버를 추가했더니 에러가 발생합니다. (아래 코드 참조)

다음은 C++ Client 코드입니다.

m_pMyComp = IMyDotNetInterfacePtr(__uuidof(MyDotNetClass));

if(m_pMyComp)
{
            tagSAFEARRAY* psArrIndex = NULL;
        
            try
    {
        m_pMyComp->ShowDialog(&psArrIndex);
    }
    catch(_com_error &e)
    {
        CString strDescription = (LPCSTR)(e.Description());
        return TRUE;
    }

    tagUnitEx* pSaBuffer = NULL;
    ::SafeArrayAccessData(psArrIndex, reinterpret_cast<void HUGEP**>(&pSaBuffer));
    ::SafeArrayUnaccessData(psArrIndex);

             return TRUE;
}


위의 코드에서 m_pMyComp->ShowDialog(&psArrIndex); 여기서 에러가 발생하며, error description에는 "이전 형식이거나 잘못된 형식 라이브러리입니다." 라고 보여집니다.

구조체 맴버에 스트링이 없으면 잘 동작하는 코드인데요.

어떤 방법이던 간에 스트링을 포함한 구조체 배열을 C# 컴포넌트에서 C++ Client로 out(또는 ref)로 넘길 수만 있으면 됩니다.

코드를 첨부합니다.

고수님들의 조언 부탁드립니다.

좋은 하루 되세요.








[최초 등록일: ]
[최종 수정일: 9/29/2006]


비밀번호

댓글 작성자
 




[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
5968orion4/30/2025271winform DataSet.xsd의 암호화 된 ConnectionString 사용 질문 [2]
5966GM B4/30/2025322C#과 python 연동 질문 [1]
5962SunN...4/21/2025577Contextmenu margin 조절 [1]파일 다운로드2
5961GM B4/10/2025970winform => wpf 마이그레이션 방법 질문 [4]파일 다운로드1
5960Kim ...4/9/20251214안녕하세요.. 혹 AI & C# 에 대해서도 다루시나요? [1]
5959vict...2/10/20251908비주얼 스투디오 CLR 코드 탑색 질문입니다. [1]
5958Supe...1/18/20252908C#으로 DLL을 만들때 WebVeiw2을 이용하여 Vb6에서 사용할수 있는지 문의드립니다. [2]
5956fc1/11/20252896한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지 [1]
5955tree...11/8/20244528C#12 책 179 페이지 문의 사항 [2]
5954SunN...10/10/20244995textbox에서 글자입력후 textBox1_KeyUp함수에서listBox1_SelectedIndexChanged 강제호출방법 [1]
5953Roya10/4/20244984c# com host dll을 c++에서 사용할 때, sdk 관련 runtime 을 설치 안하고 제공할수 있을까요?? [1]
5952우광현9/5/20246116서버용 소켓 포트 동적포트 적용시 클라이언트가 호출하는 방법? [2]
5951Kun ...8/21/20245707c++에서 복합데이터 Struct를 .NET Framework에서 구현할 수 있는 방법이 있을까요? [2]
5950SunN...8/18/20246275정석대로 한다면 response.Close() 해야되는지요? [4]
5949SunN...8/16/20246380SqlConnection Open()에서 에러가나면 Close()하는 시점 [3]
5948SunN...8/14/20246244WebRequestMethods.Ftp.MakeDirectory 해제 [3]
5947Roya8/7/20247006c# dll을 cpp lib에서 사용을 할려고 하는데요. CoCreateInstance 에서 지정된 파일을 찾을수 없다고 나오는대요. [4]
5946SunN...8/5/20247124TaskDefinition 에서 사용저가로그온상관없이실행 [3]파일 다운로드1
5945SunN...8/3/20247002윈도우 작업스케줄러 [1]
5944roya8/1/20246944C# dll을 C++ 프로젝트에서 사용을 하고 싶은데, 레지스트리 등록 없이 가능한걸까요?? [2]
5943SunN...8/1/20246822윈도우C#에서 리눅스FTP로 업로드 및 리눅스에 폴더생성(WindowsOS에서C#으로) [3]
5942vict...7/24/20246848dll 을 c#에서 쓰고 싶습니다 [1]
5941vict...7/17/20248349DependencyObject와 같은 스레드에서 DependencySource를 만들어야 합니다. 오류 [2]
5940ckm7/16/20248767선생님 안녕하세요 async await 교착 관련 질문입니다 [1]
5939괴물신인7/9/20248471템플릿 대체할수있는방법이 있을까요? [6]
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...