Win32 Interop - C/C++ DLL로부터 이중 포인터 버퍼를 C#으로 받는 예제
아래와 같은 질문이 있는데요,
C++의 double pointer를 C#에서 구현하는 방법이 잘 안됩니다.
; https://www.sysnet.pe.kr/3/0/5922
이런 경우, C#이 제대로 동작하고 있는지 확신이 없다면 간단한 C/C++ DLL 예제를 만들어 검증하는 단계를 거치면 됩니다.
코드도 매우 간단한데요, C/C++ 측은 다음과 같은 코드를 만들어 두고,
#include "stdafx.h"
#include "InteropWin32.h"
DWORD* g_pItem = nullptr;
INTEROPWIN32_API void fnInterop(DWORD** ppItem)
{
    if (g_pItem == nullptr)
    {
        g_pItem = new DWORD[10];
        for (int i = 0; i < 10; i++)
        {
            g_pItem[i] = i;
        }
    }
    *ppItem = g_pItem;
}
C# 측에서는 저 함수를 호출하는 코드를 이런 식으로 만들 수 있습니다.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace InteropTest
{
    class Program
    {
        // x86 대상인 경우, 기본 Win32 API의 호출 규약이 Cdecl이므로,
        // [DllImport("InteropWin32.dll", CallingConvention = CallingConvention.Cdecl)]
        // x64 대상인 경우, CallingConvention 필요 없음
        [DllImport("InteropWin32.dll")]
        public static extern void fnInterop(ref IntPtr data);
        static void Main(string[] args)
        {
            IntPtr ptr = IntPtr.Zero;
            fnInterop1(ref ptr);
            int[] elems = new int[10];
            Marshal.Copy(ptr, elems, 0, 10);
            foreach (int elem in elems)
            {
                Console.Write($"{elem},"); // 출력 결과: 0,1,2,3,4,5,6,7,8,9,
            }
        }
    }
}
쉽죠?!!! ^^ 만약 "ref IntPtr"이 못 미덥다면 포인터 연산을 직접 하는 것도 가능합니다.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace InteropTest
{
    unsafe class Program
    {
        [DllImport("InteropWin32.dll")]
        public static extern void fnInterop(int **ppItem);
        static void Main(string[] args)
        {
            int* p = null;
            fnInterop2(&p);
            for (int i = 0; i < 10; i ++)
            {
                Console.Write($"{p[i]},"); // 출력 결과: 0,1,2,3,4,5,6,7,8,9,
            }
        }
    }
}
이렇게 검증이 되었으니, 이제 질문자의 상황에서는 다른 요인으로 인한 문제가 있음을 예상할 수 있습니다.
(
첨부 파일은 이 글의 예제 코드를 포함합니다.)
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]