본문 바로가기
VisualStudio.C++.C#

C# . VC++ DLL 활용 . DllImport

by i.got.it 2026. 1. 19.

 

C# 에서 DLL 활용 

 

DLL 에서 정의된 무인자 함수

extern "C" __declspec(dllexport) void __stdcall OnActivated_CyNinjaX();
extern "C" __declspec(dllexport) void __stdcall OnTerminated_CyNinjaX();

 

 

  • extern "C" → C++ name mangling을 막아서 C-style 이름으로  export
  • __stdcall → 호출 규약 (C#에서는 CallingConvention.StdCall)
  • 반환형 void → C#에서는 void로 매핑 가능

C# . DLL  임포팅 코드 예 

- DLL 활용하는 기능을 C# 클래스로 캡슐화 

- 본 DLL 은 여러 인스턴스 생성하면 안되는 것이어서 static 클래스로 하였음. 

using System;
using System.Runtime.InteropServices;

namespace CyS91LE1_CyNinjaX
{

    /// CyS91LE1_CyNinjaX DLL 함수 래퍼 클래스 
    public static class CyNinjaX
    {
        private const string DllName = "CyS91LE1_CyNinjaX.dll";

        [DllImport(DllName, CallingConvention = CallingConvention.StdCall)]
        private static extern void OnActivated_CyNinjaX();

        [DllImport(DllName, CallingConvention = CallingConvention.StdCall)]
        private static extern void OnTerminated_CyNinjaX();

        public static void OnActivated()
        {
            OnActivated_CyNinjaX();
        }

        public static void OnTerminated()
        {
            OnTerminated_CyNinjaX();
        }
    }
}

 

 

호출 예 

- static 클래스이므로 instance 생성 없이 바로 호출한다.  

using CyS91LE1_CyNinjaX;

class Program
{
    static void Main()
    {
        // DLL 함수 호출
        CyNinjaX.Activate();
        Console.WriteLine("Main UI 생성 완료");

        Console.WriteLine("아무 키나 누르면 종료...");
        Console.ReadKey();

        CyNinjaX.Terminate();
        Console.WriteLine("Main UI 종료 완료");
    }
}

 

 

 

DLL 에서 정의된 함수 인자가 구조체 포인터  

- 먼저 구조체 부터 C# 에서 

C++  측의 구조체 

#pragma pack(push, 8)
namespace CyFinX::Raw {

struct Tick
{
    double  ask;
    double  bid;
    int64_t time_msec;
};

struct Tick_MultiSymbol
{
    int32_t CyFinXSymbolID;
    Tick    stTick;
};

}
#pragma pack(pop)

 

C# 측의 구조체 


using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct Tick
{
    public double ask;
    public double bid;
    public long   time_msec;
}

[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct Tick_MultiSymbol
{
    public int  CyFinXSymbolID;
    public Tick stTick;
}

 

C++ 함수 

- 위 구조체의 포인터 인자 

-  __stdcall  아닌 경우 


extern "C" __declspec(dllexport)
void func(const CyFinX::Raw::Tick_MultiSymbol* p);

 

C# 측 

[DllImport("YourDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void func(ref Tick_MultiSymbol p);

혹은 
public static extern void func(IntPtr p);

 

 

 

C# 에서 사용 가능한 DLL 함수 인자 일반 

불가능한 것들 . C 에 없는 개념들. 

C++ 전용타입 : std ::string 등 모든 std 류 ,MFC  CString, ... C++ 객체, 레퍼런스 , 템플릿. 

 

기본타입 

C/C++ C#
char sbyte / byte
int int
float float
double double

 

 

C/C++ C# 키워드 CLR 타입 크기
int8_t sbyte System.SByte 1 byte
uint8_t byte System.Byte 1 byte
int16_t short System.Int16 2 byte
uint16_t ushort System.UInt16 2 byte
int32_t int System.Int32 4 byte
uint32_t uint System.UInt32 4 byte
int64_t long System.Int64 8 byte
uint64_t ulong System.UInt64 8 byte

 

 

 

포인터 

void*  - Intptr 

 

고정길이 배열 

char name[60];

 

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 60)]
string name;

또는 

fixed byte name[60];

 

 

 

 

구조체 

struct S { int a; double b; };

 

C#

[StructLayout(LayoutKind.Sequential)]
struct S { public int a; public double b; }

 

 

 

 

 

 

상위정리

 

 

Visual Studio , Visual C++ 활용 정리.

Visual Studio C++ 활용 Visual StudioVisual C++ / C#1.설치, 설정 주제 비고. Visual Studio 2010 설치. Visual Studio 2013 (Community)무료 설치. Visaul Studio 2015 (Community)무료 설치.- Visual Studio 2015 버전은 이전 버전대비 급

igotit.tistory.com

 

 


첫 등록 : 2026.01.18

최종 수정 : 

단축 주소 : https://igotit.tistory.com/6414


 

댓글



비트코인




            암호화폐/외환/나스닥/골드          
       
현물 |선물 인버스 |선물 USDT , bybit MT5               프랍 트레이딩. MT4,MT5