项目中使用WebApi(VS2013开发,AnyCPU)来实现一个接收文件的服务,
现在接收文件没有问题,但是接收文件后需要进行一些处理,
所以使用VC6写了一个Dll,想在WebApi中动态调用。
DllImport的方式不够灵活,所以使用委托,具体代码如下:
现在接收文件没有问题,但是接收文件后需要进行一些处理,
所以使用VC6写了一个Dll,想在WebApi中动态调用。
DllImport的方式不够灵活,所以使用委托,具体代码如下:
namespace webServer.Classes
{
public class PreProcDllLoader
{
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string strDllFile);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string strProcName);
[DllImport("kernel32.dll")]
private static extern IntPtr FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll")]
private static extern int GetLastError();
// 委托声明
delegate bool DATAPREPROC(string strDataFile, StringBuilder strError, bool bLogEnabled, string strLogFile, int iLogLevel);
// 预处理动态接口句柄
private IntPtr hModule;
// 函数地址
private IntPtr pDataPreProc;
// 函数接口(函数指针)
private DATAPREPROC InDirectDataPreProc;
// 构造函数
public PreProcDllLoader()
{
hModule = IntPtr.Zero;
pDataPreProc = IntPtr.Zero;
InDirectDataPreProc = null;
}
// 析构函数
~PreProcDllLoader()
{
if (hModule != IntPtr.Zero)
FreeLibrary(hModule);
hModule = IntPtr.Zero;
}
public bool Load(String strDllFile, String strFuncName, StringBuilder strError)
{
String strString;
strString = string.Empty;
// 间接调用
//*/
if (!File.Exists(strDllFile))
{
strString = string.Empty;
strString = string.Format("接口文件不存在!");
strError.Clear();
strError.Append(strString);
return false;
}
if (strFuncName == string.Empty)
{
strString = string.Empty;
strString = string.Format("接口函数为空!");
strError.Clear();
strError.Append(strString);
return false;
}
try
{
hModule = IntPtr.Zero;
hModule = LoadLibrary(strDllFile);
if (hModule == IntPtr.Zero)
{
strString = string.Empty;
strString = string.Format("加载接口失败: {0}", GetLastError());
strError.Clear();
strError.Append(strString);
return false;
}
pDataPreProc = IntPtr.Zero;
pDataPreProc = GetProcAddress(hModule, strFuncName);
if (pDataPreProc == IntPtr.Zero)
{
strString = string.Empty;
strString = string.Format("加载函数失败: {0}", GetLastError());
strError.Clear();
strError.Append(strString);
return false;
}
InDirectDataPreProc = null;
InDirectDataPreProc = (Delegate)Marshal.GetDelegateForFunctionPointer(pDataPreProc, typeof(DATAPREPROC)) as DATAPREPROC;
if (InDirectDataPreProc == null)
{
strString = string.Empty;
strString = string.Format("委托函数失败!");
strError.Clear();
strError.Append(strString);
return false;
}
}
catch (Exception ex)
{
strString = string.Empty;
strString = string.Format("发生异常: {0}", ex.Message);
strError.Clear();
strError.Append(strString);
return false;
}
//*/
return true;
}
public bool ExecuteFunction(string strDataFile, StringBuilder strError, bool bLogEnabled, string strLogFile, int iLogLevel)
{
string strString;
strString = string.Empty;
try
{
//*/
if (InDirectDataPreProc == null)
{
strString = string.Empty;
strString = string.Format("未加载接口!");
strError.Clear();
strError.Append(strString);
return false;
}
return InDirectDataPreProc(strDataFile, strError, bLogEnabled, strLogFile, iLogLevel);
//*/
}
catch (Exception ex)
{
strString = string.Empty;
strString = string.Format("发生异常: {0}", ex.Message);
strError.Clear();
strError.Append(strString);
return false;
}
}
public void Free()
{
if (hModule != IntPtr.Zero)
FreeLibrary(hModule);
hModule = IntPtr.Zero;
}
}
}
在开发环境中(PC1,Win7x64,安装了VC6和VS2013,.NetFx4.5)使用没有任何问题,不管是接收文件还是Load库进行处理;
在另一个环境(PC2,Win7x32,安装了VC6和VS2010,.NetFx4.0)也没有问题。
在生产环境中(PC3,Win7x64,只安装了.NetFx4.0),只能接收文件,LoadLibrary失败,GetLastError得到的错误码193.
不知道为什么?
只有25分了。
解决方案
10
把 AnyCPU 改成 x86 试试。
15
1、别用AnyCPU,改成和dll同样的
2、不一定是这个dll,假设你还依赖b.dll,碰巧系统里只有一种,也会出这个错误