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)
5834guest2/24/20239624Python IDE - 비주얼스튜디오 [3]
5833무지남2/23/20239023Async 메서드 그리고 나서 Bool 메서드 [5]
5832김지우2/21/20239638event와 delegate의 차이 , event를 써야하는 이유 [1]
5831이우람2/20/202310505ref 전역변수가 pinned가 될수 있나요? [2]
5830냉수마찰2/19/20239930C# GridView에 Column별 데이터 추가하는 방법에 대해 [1]
5829수박942/19/202310895키움 API를 윈폼과 WPF의 네임스페이스 없이 콘솔이나 WinUI3에서 사용할 수 있는 방법이 있나요? [2]파일 다운로드1
5828김재영2/19/202310505장기적으로는 this 구문을 안쓰는게 맞을까요? [2]
5827lee2/18/202311435파이썬 설치 오류 질문입니다 [1]
5826Syong2/14/202311399Socket 관련 Leak (OverlappedAsyncResult, OverlappedData) 관련 문의 [7]파일 다운로드1
5825박성원2/14/202310981Listview 컨트롤의 화면 전환 시 갱신 속도 [1]
5823검은콩2/13/202312496catch(Exception ex)의 line번호를 쉽게 알 수 없는지요? [7]
5822김지우2/11/202312526책을 보면서 sync, async 이해가 되지 않는 부분이 있습니다. [5]파일 다운로드2
5821검은콩2/9/202310049Async 신뢰성과 소켓데이터 [4]
5820차가워2/8/202310059다른 프로세스 실행 후 포커스 가져오기 [3]
5819취준생2/7/202310105WPF 관련 실무가 궁금합니다. [3]
5818윤길2/7/20239172ObservableCollection 에서 INotifyPropertyChanged 구현해야하나요? [2]
5817흰털너부리2/7/20239225배포 시 winform 실행 콘솔로그 보는 방법 [1]
5816흰털너부리2/6/20239243.net core json array validation 질문 드립니다. [1]
5815김재영2/6/20239267종단간 암호화에 대해 시나리오인데 타당한 시나리오일까요? [2]
5814한예지 donator2/6/202310277decompile? [9]
5813김재영2/5/202310077openssl genrsa 2048시 키 생성이 다르게 됩니다. - 파일첨부 [4]파일 다운로드1
5812김재영2/5/202310493openssl genrsa 2048시 키 생성이 다르게 됩니다. [2]
5811치르바2/3/202310227MiniDumpWriteDump API로 덤프수집을 했는데요.. [3]
5810이건우1/31/202310645윈도우서비스를 통한 웹통신관련 질문입니다 [3]
5809이상훈1/31/202311042다채널 영상 디스플레이어 개발 관련 질문입니다. [3]
5808근우1/30/202310814WPF 에서 UserControl 과 ControlTemplate 의 차이점은 무엇인가요? [6]
1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...