????????????????????????DLL??
????1?????????????У???????????????????????ó????????????????????
????2??????DLL??
????int hModule = DLLWrapper.LoadLibrary(dllFilePath);
????if (hModule == 0)
????return false;
????3??????????????????
????FOO foo = (FOO)DLLWrapper.GetFunctionAddress(hModule?? "foo"?? typeof(FOO));
????if (foo == null)
????{
????DLLWrapper.FreeLibrary(hModule);
????return false;
????}
????4?????ú?????
????foo(...);
????5??.NET???????????????????DLL????????????????DLL???????????DLL??
????DLLWrapper.FreeLibrary(hModule);
???????????????????????????????????????????????????У?????????DllImport???????????÷?????????C#?ж?DLL?к???????????????Щ?????????????????????????е???????
????1????????????????C++?е??????C#??????????????????C++?е?long?????C#?е?Int32??????long?????????μ??y???????
????2?????????????StructLayout????????????????????????????MSDN??
???????LayoutKind??????г???????????????????Sequential??
????[StructLayout(LayoutKind.Sequential)]
????struct StructVersionInfo
????{
????public int MajorVersion;
????public int MinorVersion;
????}
??????????????????????????????????????????????????????????C#???????class??
????[StructLayout(LayoutKind.Sequential)]
????class StructVersionInfo
????{
????public int MajorVersion;
????public int MinorVersion;
????}
???????C++?е???????
????typedef struct _VERSION_INFO
????{
????int MajorVersion;
????int MinorVersion;
????} VERSION_INFO?? *PVERSION_INFO;
??????????????????????????????????????????
????[StructLayout(LayoutKind.Sequential??CharSet=CharSet.Unicode)]
????????????????????????????У???
????C++???????????
????wchar_t Comments[120];
????C#??
????[MarshalAs(UnmanagedType.ByValTStr?? SizeConst = 120)]
????public string Comments;
????C++???????
????VERSION_INFO ver;
????C#
????publicStructVersionInfo ver;
????C++?????????????
????PFOO pFoo; //????????????????沿??
????C#:
????publicIntPtr pFoo; //?????? public int pFoo;
????//????????????????????????DLLWrapper????????????????????????
??????????????????union????????????FieldOffset???????λ?á?
????3????е???????
??????C++??д??DLL????????????????????????????????????
????void getVersionInfo(VERSION_INFO *ver);
??????????C#???????class???????VERSION_INFO?????class??
????delegate voidgetVersionInfo(VERSION_INFO ver);
??????????????struct?????????????????????
????delegate voidgetVersionInfo(refVERSION_INFO ver);
?????????????ref??????
???????DLL???????????????????????????????
????BOOL __stdcall jingzhongrong1(const wchar_t* lpFileName?? int* FileNum);
??????????????????ú?????????????C#????????????У?
????delegate bool jingzhongrong1(
????[MarshalAs(UnmanagedType.LPWStr)]String FileName??
????ref int FileNum);
?????????????[MarshalAs(UnmanagedType.LPWStr)]??String??????????
??????????DLL?????д???????????????????????
????void __stdcall jingzhongrong2(
????wchar_t* lpFileName?? //????????????
????int* Length);
?????????????????????У?
????//?????д???й??????????д????????????
????//??????????????????????StringBuilder?????????
????delegate void jingzhongrong2(
????[MarshalAs(UnmanagedType.LPWStr)] StringBuilder lpFileName??
????ref int Length??
????);
????????ú??????????StringBuilder???????????????????????
????StringBuilder fileName = new StringBuilder(FileNameLength);