C++/기초

[Cpp][별*] C# dll , Cpp 호출

딸기우유중독 2024. 7. 31. 14:11
1.COM 사용
2.AOT 사용
3.DllExport 사용
4.CLR 사용

 

 

1. COM 사용

 

 

# .NetFramework4.8 사용 시

https://dlsenfl.tistory.com/entry/C-C-dll-COM

 

 

# .comhost.dll 사용 시

.net6 이상  

 

1.wpf ( .net9.0 이상)

 

.exe
<TargetFramework>net6.0-windows</TargetFramework>

에서 
comhost.dll
<TargetFramework>net9.0-windows</TargetFramework>

안불러와짐

둘다 
<TargetFramework>net9.0-windows</TargetFramework>
여야 에러 안남.

 

TargetFramework 다를경우 

호출 제대로 못함.

 

 

WPF Class Library

 

 

 

 

 

 

 

2 .net core

이렇게 만들어야 dll Com 등록할 때 등록이 됨.

 

 

EX)

 

 


 

https://learn.microsoft.com/ko-kr/dotnet/core/native-interop/expose-components-to-com

 

COM에 .NET Core 구성 요소 공개 - .NET

이 자습서에서는 .NET Core에서 COM에 클래스를 노출하는 방법을 보여 줍니다. 레지스트리 없는 COM을 위한 COM 서버 및 병렬 서버 매니페스트를 생성합니다.

learn.microsoft.com

 

https://github.com/dotnet/samples/tree/main/core/extensions/COMServerDemo

 

samples/core/extensions/COMServerDemo at main · dotnet/samples

Sample code referenced by the .NET documentation. Contribute to dotnet/samples development by creating an account on GitHub.

github.com

 

 

 

x64만 가능.

  <PlatformTarget>x64</PlatformTarget>

https://blog.naver.com/techshare/223275048687

 

.NET Core/5+ 기반의 COM Server를 tlb 파일을 생성하는 방법(tlbexp)

예전에 작성한 COM 개체를, C# COM 개체를 C++에서 사용하는 예제 ; https://www.sysnet.pe.kr/2/0...

blog.naver.com

 

 

https://www.sysnet.pe.kr/2/0/13458

 

닷넷: 2170. .NET Core/5+ 기반의 COM Server를 tlb 파일을 생성하는 방법(tlbexp)

글쓴 사람 정성태 (techsharer at outlook.com) 홈페이지 첨부 파일 부모글 보이기/감추기 (연관된 글이 2개 있습니다.) (시리즈 글이 4개 있습니다.) 닷넷: 2170. .NET Core/5+ 기반의 COM Server를 tlb 파일을 생성

www.sysnet.pe.kr

 

 

 

 

 

Class Library(.Net Framework) 사용시

 

Class Library(.Net Framework)만 regasm 등록 됨.

 




https://blog.naver.com/techshare/222476375544

 

C# COM 개체를 C++에서 사용하는 예제

다음의 질문이 나온 김에, C++ C# API 연동 관련 ; https://www.sysnet.pe.kr/3/0/5546 정리를 해보겠...

blog.naver.com

 

 

https://petra.tistory.com/1130

 

C# COM 만들기

C# COM 만들기 윈도우10, VisualStudio2017 기준 MFC Client : http://petra.tistory.com/1133 1. Visual Stidio 2017을 관리자 모드로 실행한다. 2. 아래 그림처럼 클래스 라이브러리(.NET Framework) _COMTest 프로젝트를 만든다.

petra.tistory.com

 

https://benstagram.tistory.com/384

 

C# 라이브러리를 C++에서 사용하는 방법

https://developer-joe.tistory.com/140 C# 프로그램을 .dll Library로 만들기(C++에서 사용하기 위해) C#에서 .dll로 library를 만들고 이것을 C++에서 사용하기 (C++에서 C# .dll 라이브러리 사용하는 법) C#은 MFC에 비

benstagram.tistory.com

 

https://blog.naver.com/ocllos/220464927065

 

[C++ / C#] C++ 에서 C# dll 사용하기 [1탄]

오랜만에 올리는 프로그래밍 강의입니다. 오늘 강의는 C++ 에서 C# dll 을 사용하는 방법에 대해 알려드...

blog.naver.com

 


2. .NET AOT 사용

 

char*  to string

unsafe public static char* GetInstance(char* name)
{
	Marshal.PtrToStringAnsi((IntPtr)name)
}

 

char* 문자열 사이즈

 int length = GetTextLen(name);
 
 
   unsafe private static int GetTextLen(char* ptr)
  {
      int len = 0;

      while (*ptr != 0)
      {
          len++;
          ptr++;
      }

      return len;
  }

 

메모리 할당

        nint pBuffer = Marshal.AllocHGlobal(size);

 

할당한 메모리에 스트링 copy

char* pText = (char*)Marshal.StringToHGlobalUni(serviceName).ToPointer();
System.Buffer.MemoryCopy(pText, pBuffer, size, length * 2);

 

 

 

 


 

 

http://cbuilder.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_qna&no=75515

 

볼랜드포럼: Re:안녕하세요 C# dll 파일을 c++에서 사용

gksqjs94 님이 쓰신 글 : : 안녕하세요. 제목그대로 Visual studio C#에서만든 dll을 c++에서 사용할려고합니다. : 게시글을 검색해보니까 나와있는데로 : : implib Test.lib Test.dll : implib _a Test.lib Test.dll : : 이

cbuilder.borlandforum.com

 

 

https://www.sysnet.pe.kr/3/0/5032

 

C# 으로 만들 dll 등록

 

www.sysnet.pe.kr

 

https://www.sysnet.pe.kr/2/0/13464

 

닷넷: 2174. C# - .NET 7부터 UnmanagedCallersOnly 함수 export 기능을 AOT 빌드에 통합

글쓴 사람 정성태 (techsharer at outlook.com) 홈페이지 첨부 파일 [unmanaged_export_sample.zip]     부모글 보이기/감추기 (연관된 글이 4개 있습니다.) (시리즈 글이 7개 있습니다.) .NET Framework: 969. .NET Framewor

www.sysnet.pe.kr

 

 

 

https://jke94.medium.com/c-has-a-new-friend-net-native-aot-bcaa0a4c24c0

 

C++ has a new friend: .NET Native AOT

This post contains a demonstration about how to use .NET Native AOT (ahead-of-time) technology to make HTTP requests in .NET, exporting…

jke94.medium.com

 

 


 


3. nuget  DllExport 

 

DllExport 누겟 설치

 

 

https://github.com/3F/DllExport

 

https://github.com/3F/DllExport/releases/tag/1.8

 

DllExport.bat 실행하면 GUI창 나옴

 

 

 

빨간 상자 체크

 

Use our IL Assemler , RebaseSystem Object  체크 해줘야  .net core 라이브러리 cpp에서 호출 가능.!!

 

Apply하면 DllExport Attribute 적용가능.

 

Rebase System Object: mscorlib 하면

C#에서 GetType 에러 나옴.

 

 

 

 

 

C#  .net library

 

ExportNetStandard.dll 파일

cpp working folder에 복사

 

Cpp console

 

실행.

 

 

 

참고 

C# dll , Cpp 호출.
Cpp .exe , C# 호출.
Cpp .dll , C# 호출.

 

https://www.youtube.com/watch?v=9Hyg3_WE9Ks

 

String , struct, Array Etc.. 
여러 타입 C#, C++에서 호출하는 방법.

 

https://www.youtube.com/watch?v=QXMj9-8XJnY

 

 


 

https://s-engineer.tistory.com/318

 

C# Call C# DLL in C++ - C++에서 C# 라이브러리 사용하기

C#으로 만든 클래스 라이브러리(DLL)는 C++ 라이브러리처럼 간단히 사용할 수 없다. C++에서 C# 라이브러리를 사용해 보자. ※ 참고 https://github.com/3F/DllExport https://youtu.be/9Hyg3_WE9Ks https://youtu.be/sBWt-KdQt

s-engineer.tistory.com

 

https://www.sysnet.pe.kr/2/0/12118

 

개발 환경 구성: 466. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 세 번째 이야기

글쓴 사람 정성태 (techsharer at outlook.com) 홈페이지 첨부 파일 부모글 보이기/감추기 (연관된 글이 3개 있습니다.) (시리즈 글이 6개 있습니다.) 개발 환경 구성: 300. C# DLL에서 Win32 C/C++처럼 dllexport 함

www.sysnet.pe.kr

 

 

dll 정보 보는 프로그램

 

https://ntcore.com/explorer-suite/

 

Explorer Suite – NTCore

Download the Explorer Suite Current Version: III (18/11/2012) Small announcement: If you or your organization needs professional PE inspection, then take a look at Cerbero Suite (the commercial product of my company), which properly supports many file form

ntcore.com

 

 

 


 

Interop

 


 

 

https://daniel-albuschat.blogspot.com/2011/05/using-net-and-wpf-in-win32-legacy.html

 

Using .NET and WPF in Win32 legacy applications, Part 1: The basics

Introduction There are applications that are just impossible to port to any new technology, because of sheer size. Unfortunately, most of th...

daniel-albuschat.blogspot.com

 

 

 

 


 

 

4.CLR

 

 

 

 


 

https://dlsenfl.tistory.com/entry/WPF-WPF-UI-Call-from-MFC-Feat-CLR

 

[WPF] WPF UI Call from MFC (Feat. CLR)

Visual Studio 2022에 기본적으로 CLR 설치 안되어 있음. Installer 들어가서 C++/CLI Support 인스톨 해야 함.   WPF Library (.Net Framework) WPF로 만든 공용 UI (.net Framework 4.xx~로 만들어야 함)  CLR Class Library (.N

dlsenfl.tistory.com

 

 

https://djcho.github.io/dotnet/dot-net-1/

 

[.NET]C++에서 WPF호출 방법

 

djcho.github.io

 

 

 

728x90