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 세마포어의 개념과 사용법 digipine 2017.10.29 765
44 [Windows] DOS 명령어 실행하고 결과 스트링 가져오는 샘플 코드 digipine 2017.11.02 2689
43 [WINCE] 키보드 및 마우스 메시지 후킹하기 digipine 2017.10.29 809
42 [WINCE] 메모리카드 상태 감시 digipine 2017.10.29 327
41 [WINCE] Process, Thread API 함수 사용법 digipine 2017.10.29 960
40 [WINCE] MulDiv 함수 구현 digipine 2017.10.29 702
39 [WINCE] IAT Hooking 방법과 소스 코드 digipine 2017.10.29 659
38 [WIN32] 파일 핸들로 파일 명 구하기 digipine 2017.10.29 1067
37 [WIN32] 실행 중인 프로세스를 외부에서 강제로 종료, 안전한 TerminateProcess digipine 2017.10.29 3467
36 [WIN32] Process ID로 HWND 구하기 digipine 2017.10.29 5036
35 [Win32] HBITMAP Contrast 조절하는 코드 - RGB 이미지 보정 엉뚱도마뱀 2018.05.04 738
34 [WIN32] API Hook 정리 문서 digipine 2017.10.29 1971
33 [WIN32, WINCE] 디스크 용량 구하는 방법 API GetDiskFreeSpaceEx digipine 2017.10.29 1571
32 [Win API]프로세스 아이디와 윈도우 핸들을 이용 파일명 구하기 digipine 2017.10.29 1396
31 [VC++, WInAPI] 폴더를 통채로 지우기, 서브 폴더 포함, DeleteAllFiles digipine 2017.10.29 1763
30 [MFC] Dialog에서 부모 윈도우 알아내기 digipine 2017.10.28 923
29 [DirectShow] 화면 원본 비율유지 digipine 2017.10.29 961
28 [C#] 프로그램 종료 방법 lizard2019 2019.01.23 6847
27 [C#] 코드 실행 시간 측정 및 DateTime 스트링으로 변환 포맷 lizard2019 2019.01.23 23126
26 [C#] StreamReader 에서의 한글 Encoding 문제 digipine 2017.10.29 1001
Board Pagination Prev 1 2 3 Next
/ 3