C# 调用第三方DLL完整实例
时间:2010-07-01 来源:kuncomputer
以下代码为本人在实际项目中编写的调用第三方DLL接口程序的完整代码。
public class ExecuteDLL : Form
{
...//忽略与调用DLL无关的代码
private IntPtr hModule = IntPtr.Zero;
/// <summary>
/// 调用HIS系统提供的DLL接口程序查看检查申请单
///DLL的名称:H62AppQueryToPacs.dll
///函数名说明:
///function THandle HLoginProc(THandlea AppHandle, THandle aCallWinHandle,THandle aPluginHandle
/// PChar aBqno,PChar aEmpno,PChar aPatno);
///传入变量参数说明:
///// aAppHandle: THandle; //应用程序句柄
///// aCallWinHandle: THandle; //调用窗口句柄
///// aPluginHandle: THandle; // 插件的句柄
///// aEmpno,PChar;//医生工号
///// aBqno: PChar;//病区代码
///// aPatno: PChar;//病人记账号
/// </summary>
/// <param name="lpFileName"></param>
/// <returns></returns>
//申明外部API
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll")]
static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)]
static extern bool FreeLibrary(IntPtr hModule);
//申明委托
private delegate IntPtr HLoginProc(IntPtr aAppHandle, IntPtr aCallWinHandle, IntPtr aPluginHandle,
string aBqno, string aEmpno, string aPatno);
//获取函数地址
private Delegate GetFunctionAddress(IntPtr dllModule, string functionName, Type t)
{
IntPtr address = GetProcAddress(dllModule, functionName);
if (address == IntPtr.Zero)
return null;
else
return Marshal.GetDelegateForFunctionPointer(address, t);
}
//按钮单击事件
private void button_CheckList_Click(object sender, EventArgs e)
{
try
{
SIS_Model.MWorkList smWorkList;
PACS_Model.MWorkList pmWorkList;
string strNo = "";
switch (GetConfig.DALAndModel)
{
case "SIS":
smWorkList = (SIS_Model.MWorkList)this.iWorkList;
strNo = (smWorkList.INPATIENTNO == null ? "" : smWorkList.INPATIENTNO.ToString());// 病人记账号 "875624";
break;
case "PACS":
pmWorkList = (PACS_Model.MWorkList)this.iWorkList;
strNo = (pmWorkList.INPATIENTNO == null ? "" : pmWorkList.INPATIENTNO.ToString());// 病人记账号 "875624";
break;
}
if (strNo.Trim() == "")
{
MessageBox.Show("无法获取患者ID,请确认操作步骤是否正确");
return;
}
//加载DLL
try
{
string strDLLPath = Environment.CurrentDirectory + "\\HuiTong\\JianChaShenQingDan\\H62AppQueryToPacs.dll";
hModule = LoadLibrary(strDLLPath);
if (hModule.Equals(IntPtr.Zero))
{
MessageBox.Show("导入DLL失败");
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
FreeLibrary(hModule);
hModule = IntPtr.Zero;
return;
}
//将要调用的方法转换为委托:hModule为DLL的句柄,"HLoginProc"为DLL中方法的名称
HLoginProc farProc = (HLoginProc)this.GetFunctionAddress(hModule, "HLoginProc", typeof(HLoginProc));
if (farProc == null)
{
FreeLibrary(hModule);
hModule = IntPtr.Zero;
return;
}
//利用委托执行DLL文件中的接口方法
farProc(hModule, IntPtr.Zero, IntPtr.Zero, null, null, strNo);
FreeLibrary(hModule);
hModule = IntPtr.Zero;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
FreeLibrary(hModule);
hModule = IntPtr.Zero;
return;
}
}
}
版权申明: 此文为作者原创,转载请提供原始文章链接,谢谢!
版权申明: 此文为作者原创,转载请提供原始文章链接,谢谢!
相关阅读 更多 +