4年前用VS2005的C#编写了一个应用程序,用于调试VC++写的AMINE计算包。自从发布出去以后,就已经很久没有用过了。这两天有要求要重新改良一下这个软件,于是把这个程序移植到了VS2010,在Windows 7 64bit下重新编译通过,但是当运行case时,却发现出问题了。
界面每次调用DLL中的amDBRInitialize()进行初始化时,就报错。试了一下放在我的VM下的32bit的WIN7,XP下,也都会出这问题。但是在别人的windows XP下就是好的,在我另一台XP下也是好的。更奇怪的时,用以前vs2005编译的界面和DLL,也都存在同样的问题。
还好VS下抓错误信息很方便,错误为An attempt was made to load a program with an incorrect format.
查找后发现,这还是因为x86和x64兼容性的问题。因为dll是32位编译的,而编译界面时,无论以前的VS2005还是现在的VS2010,用的都是any cpu,结果在此64位计算机上,就出现了调用不谐调--64位的程序调用32位的DLL。所以,在VS2010上,把编译指令从ANY CPU改成X86就能解决问题。
修改以后,错误消失,这个问题算是解决了。但是奇怪的是,为什么之前在VM的32位的OS也一样不行呢?
前文"C#调用C++函数入口点的问题"说了一点关于使用DLLImport进行导入函数的事. C#调用C++的函数其实不止这一种方法, 还有一种方法是用delegate申明函数委托进行调用,这种方法略显麻烦,但是可以进行回调并应用指针.
在C#中,首先先要定义一个类,用来把DLL中函数地址转换成委托:
- public class DLLWrapper
- {
- ///<summary>
- /// API LoadLibrary
- ///</summary>
- [DllImport("Kernel32")]
- public static extern int LoadLibrary(String funcname);
- ///<summary>
- /// API GetProcAddress
- ///</summary>
- [DllImport("Kernel32")]
- public static extern int GetProcAddress(int handle, String funcname);
- ///<summary>
- /// API FreeLibrary
- ///</summary>
- [DllImport("Kernel32")]
- public static extern int FreeLibrary(int handle);
- ///<summary>
- ///通过非托管函数名转换为对应的委托, by jingzhongrong
- ///</summary>
- ///<param name="dllModule">Get DLL handle by LoadLibrary</param>
- ///<param name="functionName">Unmanaged function name</param>
- ///<param name="t">ManageR type对应的委托类型</param>
- ///<returns>委托实例,可强制转换为适当的委托类型</returns>
- public static Delegate GetFunctionAddress(int dllModule, string functionName, Type t)
- {
- int address = GetProcAddress(dllModule, functionName);
- if (address == 0)
- return null;
- else
- return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
- }
- ///<summary>
- ///将表示函数地址的IntPtr实例转换成对应的委托, by jingzhongrong
- ///</summary>
- public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
- {
- if (address == IntPtr.Zero)
- return null;
- else
- return Marshal.GetDelegateForFunctionPointer(address, t);
- }
- ///<summary>
- ///将表示函数地址的int转换成对应的委托,by jingzhongrong
- ///</summary>
- public static Delegate GetDelegateFromIntPtr(int address, Type t)
- {
- if (address == 0)
- return null;
- else
- return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t);
- }
- }
然后, 用delegate声明函数:
- delegate void _amDBRSetThermoModel(int mid, ref int errid);
再然后, 自己写个private的函数封装DLL中的函数, hModule()函数的作用是取得DLL的地址,用在多个输出函数中
- private int hModule()
- {
- int _hModule = DLLWrapper.LoadLibrary(DLLPATH);
- if (_hModule == 0)
- {
- return 0;
- }
- return _hModule;
- }
- private void amDBRInitialize()
- {
- try
- {
- _amDBRInitialize amf = (_amDBRInitialize)DLLWrapper.GetFunctionAddress(hModule(), "amDBRInitialize", typeof(_amDBRInitialize));
- amf();
- }
- catch (Exception e)
- {
- throw e;
- }
- }



