logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

[C#] C#에서 C++ DLL의 Call by Referance out 인수 사용하는 방법

by digipine posted Oct 29, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

C# 프로그래밍을 하다보면 C++에서 만들어 둔 DLL을 사용해야 할 경우가 많이 있지요.
in 기능의 인수들을 그냥 대충 바꾸면 되는데
out 기능의 포인터를 사용한 Call by Referance 인수들을 참 난감합니다.
그러나 아래와 같이 선언하면 사용이 가능합니다.
참고 하세요.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SolarCellDGUI
{
    class SCMInterface
    {

        [DllImport("P:SolarCell_CTCShellExecDllsSCMBusClient.dll")]
            public static extern int ConnectToSCM(string MyIPAddr,
                [MarshalAs(UnmanagedType.LPArray)] Int32[] ConnectionID,
                [MarshalAs(UnmanagedType.LPArray)] byte[] EquipmentName);

        [DllImport("P:SolarCell_CTCShellExecDllsSCMBusClient.dll")]
            public static extern int DisconnectFromSCM(string MyIPAddr, int ConnetionID);

        [DllImport("P:SolarCell_CTCShellExecDllsSCMBusClient.dll")]
            public static extern int SendEventToSCM(int ConnectionID, string Source,
                string Destination, string EventName, string EventData, int EventDataLen,
                [MarshalAs(UnmanagedType.LPArray)] byte[] ResultData,
                [MarshalAs(UnmanagedType.LPArray)] Int32[] ResultDataLen);


        public bool SendEventSCM(int ConnectionID, string Source, string Destination,
            string EventName, string EventData, int EventDataLen, ref string ResultData, ref int ResultDataLen)
        {
            int res = 0;
            byte[] resData = new byte[100];
            Int32[] resDataLen = new Int32[1];
            char[] tempchr = new char[100];
            int i;

            res = SendEventToSCM(ConnectionID, Source, Destination, EventName, EventData,
                        EventDataLen, resData, resDataLen);

            if (res == 1)
            {
                ResultDataLen = resDataLen[0];

                for (i = 0; i < 100; i ++)
                    tempchr[i] = System.Convert.ToChar(resData[i]);
                ResultData = new string(tempchr);
                return true;
            }
            else
            {
                return false;
            }

        }

        public bool ConnectSCM(string MyIPAddr, ref int ConnectionID, ref string EquipmentName)
        {
            int res = 0;
            byte[] eqpName = new byte[80];
            Int32[] conID = new Int32[1];
            char[] tempchr = new char[80];
            int i;

            res = ConnectToSCM(MyIPAddr, conID, eqpName);

            if (res == 1)
            {
                ConnectionID = conID[0];

                for (i = 0; i < 80; i++)
                    tempchr[i] = System.Convert.ToChar(eqpName[i]);

                EquipmentName = new string(tempchr);
                return true;
            }
            else
            {
                return false;
            }

        }

        public bool DisconnectSCM(string MyIPAddr, int ConnetionID)
        {
            int res = 0;

            res = DisconnectFromSCM(MyIPAddr, ConnetionID);
           
            if (res == 1)
                return true;
            else
                return false;
        }

 

    }
}

TAG •

List of Articles
No. Subject Author Date Views
45 C# - 한글로된 폰트명 처리 방법 개선 (Font Name Localization) digipine 2017.11.02 1396
44 C# 으로 구현한 화면 캡춰 클래스 1 digipine 2017.11.02 34022
43 CreateSemaphore Semaphore Manager digipine 2017.10.29 447
42 Customizing GINA, Part 1 digipine 2017.10.28 21759
41 Customizing GINA, Part 2 digipine 2017.10.28 96759
40 DLL과 EXE간의 데이타 공유하기 digipine 2017.10.29 837
39 GINA(Graphical Identification aNd Authentication), SAS(Secure Attention Sequence) digipine 2017.10.29 1261
38 MS의 Hot Fix API의 유형 연구 digipine 2017.10.29 312
37 Mutex, Critical Section Class 만들기 digipine 2017.10.29 392
36 RegEnumKeyEx 함수 사용법 digipine 2017.10.29 756
35 RPC에 대하여... (1) : RPC 가 사용하는 TCP/IP 포트는 ? digipine 2017.10.29 1296
34 RPC에 대하여... (2) : RPC 가 사용하는 포트를 바꿔보자 digipine 2017.10.29 1118
33 RPC에 대하여... (3) : RPC 작동을 위한 테스트 방법 digipine 2017.10.29 543
32 Serialize를 이용한 객체 복사하기 (Copy constructor) digipine 2017.10.29 499
31 The .Net Developer's Guide to Directory Services Programming digipine 2017.10.29 7242
30 VC++ UTF8 변환 관련 매크로 digipine 2017.11.02 8779
29 VC++ 에서 대소문자 변경하는 함수 digipine 2017.10.29 311
28 VC++(MFC)에서 MDB 생성 / 압축 / 연동관리자 digipine 2017.10.29 2662
27 VS2003 이상에서 iostream 구현의 문제점 digipine 2017.10.29 256
26 VS2005 ConvertBSTRToString 에서 LNK2019 에러 대처법 digipine 2017.10.29 192
Board Pagination Prev 1 2 3 Next
/ 3