
Dll을 만들다 보면 c#앱에서 불러올때 여러 오류가 발생한다. 나중에 dll 오류가 날때를 대비해 대응방법과 만들때 주의할점을 기록한다. 아직 뉴비라 많이 부족합니다. 틀린 부분이나 고칠 부분이 있으면 알려주세요.
#pragma once
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
extern "C"{
MYDLL_API bool MyFunction(char* buffer, int len);
}
[DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.I1)]
private static extern bool mydll_function([Out][MarshalAs(UnmanagedType.LPStr)] StringBuilder buffer, int bufferMaxLength);
An attempt was made to load a program with an incorrect format. (0x8007000B)
dumpbin /headers mydll.dll | findstr machine
DllNotFoundException
BadImageFormatException
dumpbin /exports mydll.dll
AccessViolationException
| 상황 | C++ | C# |
|---|---|---|
| 문자열 | char* | StringBuilder, [Out] + LPStr |
| 문자열 | char[64] | [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] string |
| bool | bool | [return: MarshalAs(UnmanagedType.I1)] |
| 구조체 | struct | [StructLayout(LayoutKind.Sequential)] |
| 함수 호출 | __cdecl | CallingConvention.Cdecl |
| 함수 호출 | __stdcall | CallingConvention.StdCall |

