Get it on Google Play


Wm뮤 :: DLL 생성 예제

블로그 이미지
가끔 그림그리거나 3D모델링하거나
취미로 로봇만드는
퇴직한 전자과 게임프로그래머
2020.3.48f1 , 2022.3.6f1 주로 사용
모카쨩
@Ahzkwid

Recent Comment

Archive


2021. 2. 11. 20:16 윈도우폼

깔아라

 

 

 

 

초 심플하게 8을 리턴하는 함수이다. 뭔가 이것저것 빠져있는데 왜 뺐는지 기억이 안난다. 잘 작동한다

#include <windows.h>
#include "stdafx.h"

extern "C" __declspec(dllexport) int m_test8()//완성판
{
	return 8;
};

눌러라

 

 

dll은 프로젝트폴더의 Debug폴더에 있다. 디버그가 싫으면 릴리즈로 하면 릴리즈 폴더에 생긴다

dll만 쓸거면 dll만 뽑으면 됨

 

 

불러오기 위해 C#폴더에 넣을건데 C#은 경로가 조금 다르다 

프로젝트 폴더/프로젝트이름 폴더/bin/Debug 폴더에 넣는다

프로젝트 폴더가 두개있는게 이상하지만 얘내가 그렇게 만든거니까 그러려니 하자

 

 

불러올때

using System;
using System.Windows.Forms;

namespace Dll_Import_Test
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [System.Runtime.InteropServices.DllImport("user32.dll")] //키보드 상태
        public static extern int GetKeyboardState(byte[] pbKeyState);


        [System.Runtime.InteropServices.DllImport("Dll_Export_Test.dll")]
        public static extern int m_test8();


        [System.Runtime.InteropServices.DllImport("Dll_Export_Test.dll")]
        public static extern void m_test38(int[] _array);
        
        [STAThread]
        static void Main()
        {
            
            if (m_test8() == 8)
            {
                MessageBox.Show("m_test8() == 8");
            }
            else
            {
                MessageBox.Show("m_test8() 읽기 에러");
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }
    }
}

동일 DLL에서 여러 함수를 불러오는 예제와 윈도우즈에 포함된 DLL을 불러오는 예제로 되어있다

 

 

 

 

 

38버전은 dll에서 배열로드버전이다

// Dll_Export_Test.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
#include <windows.h>
#include "stdafx.h"

//extern은 내보낸다는 소리
//"C"은 C언어 규약으로 작성이랬던가 확실하지 않음, 이거 안넣으면 읽기실패
//__declspec(dllexport)은 dll로 내보내겠다는 뜻
//__stdcall 이건 호출방식인데 일반적으로 C#에선 __stdcall을씀(c++ 기본값은 __cdecl)
extern "C" __declspec(dllexport) void __stdcall m_test38(int _array[])//완성판
{
	for (int i = 0; i < 8; i++)
	{
		_array[i] = i;
	}
	return;
};

 

40버전은 길이를 입력받아서 처리하는거, 가능하면 자동화하고 싶었지만

C++의 배열시스템이 너무 후진적이라 이따구로 함

배열포인터에는 길이정보가 없다고 하니까 더 많은걸 바라지 말아라.

extern "C" __declspec(dllexport) void __stdcall m_test40(int _array[],int _length)
{
	for (int i = 0; i < _length; i++)
	{
		_array[i] = i;
	}
	return;
};

불러올떄

using System;
using System.Windows.Forms;

namespace Dll_Import_Test
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [System.Runtime.InteropServices.DllImport("Dll_Export_Test.dll")]
        public static extern void m_test40(int[] _array,int _length);
        
        [STAThread]
        static void Main()
        {
            
            var _array = new int[16];
            m_test40(_array, _array.Length);
            MessageBox.Show($"m_test40(): [{string.Join(", ", _array)}]");
            
            

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }
    }
}

 

결과

 

 

 

여기까진 맛보기고 이제 다중 DLL 참조가 있다

'윈도우폼' 카테고리의 다른 글

잘 안 쓰는 윈폼코드 모음  (0) 2021.07.19
윈폼 키보드마우스  (0) 2021.01.17
c# 멀티스레드 기본소스  (0) 2020.11.23
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입