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)
5921한예지 donator10/3/202315252마샬링 정의 및 목적이 궁금합니다. [5]
5920한예지 donator10/3/202314930C#과 WIN32 API 관계 질문드립니다. [4]
5919이건우9/27/202313604WinForm의 로딩속도 관련 질문입니다 [2]
5917한예지 donator9/14/202314148동기화 도구 질문 있습니다. [4]
5916한예지 donator9/3/202314617Thread.Sleep(500), await Task.Delay(500), Task.Delay(500) 차이점이 궁금합니다. [2]
5915한예지 donator8/30/202316405비동기 코드를 for 문 안에 작성한 경우 제어 변수가 올바르게 동작하지 않는 이유가 궁금합니다. [3]
5914한상욱8/11/202315715.net wpf에서 skiasharp 의 skelement 를 canvas로 사용 하고 있습니다. [1]
5913김태우8/10/202315247지역변수로 이해하는 메서드매개변수 게시글 댓글 [3]
5912guest4/25/202322077[참고 - 초보용] Sqlite 디비는 double이 없고 Real이 대신합니다 [3]
5911guest4/24/202314052Form1.cs와 외부 class.cs와 통신 (static async method포함) [4]파일 다운로드1
5910guest4/24/202313440Async 메서드와 try~catch [1]
5909guest4/22/202315204Visual Studio 구매 시(1인 개발자) [4]
5908guest4/22/202314442텅빈 원그리기 [5]
5907민성4/21/202314467안녕하세요 서버 백업 문제에 대해서 [2]
5906guest4/21/202314782Dispatcher 서비스 구현 질문 [1]
5905guest4/20/202315768tabControl의 tabPage가 여러 개일 때 순서를 바꾸기가 까다롭네요 [5]
5904guest4/18/202316268[신규자료첨부] DLL 'SQLite.Interop.dll'을 찾을 수 없습니다 [4]파일 다운로드1
5903guest4/18/202314904fileSystemWatcher 이벤트 관련 질문입니다 [2]
5902guest4/17/202315881c#으로 USB 관련 질문 [2]
5901guest4/17/202312779내솔루션 판매 시 1.0.0.0 폴더와 Sqlite 배포 [5]
5900guest4/17/202316481DLL 'SQLite.Interop.dll'을 찾을 수 없습니다 [2]파일 다운로드1
5899guest4/17/202314018Dictionary와 Linq [4]
5898차가워4/17/202313450CNTK 교육 문의 [1]
5897guest4/17/202313061Socket스레드와 UI thread [4]
5896HAN4/16/202313389c언어 return 에 대해 궁금한게 있어요. [1]파일 다운로드1
5895guest4/15/202313682Drag and Drop - 모든 컨트롤 [2]
1  2  [3]  4  5  6  7  8  9  10  11  12  13  14  15  ...