GetProcAddress

本文解释了如何通过GetProcAddress函数显式链接到DLL并获取其导出函数的地址,然后使用返回的函数指针调用DLL函数。文章还讨论了在调用函数时确保参数正确的重要性,以及使用导出序号与函数名之间的区别。
显式链接到 DLL 的进程调用  GetProcAddress 来获取 DLL 导出函数的地址。使用返回的函数指针调用 DLL 函数。 GetProcAddress 将(由  LoadLibraryAfxLoadLibrary 或  GetModuleHandle 返回的)DLL 模块句柄和要调用的函数名或函数的导出序号用作参数。

由于是通过指针调用 DLL 函数并且没有编译时类型检查,需确保函数的参数是正确的,以便不会超出在堆栈上分配的内存和不会导致访问冲突。帮助提供类型安全的一种方法是查看导出函数的函数原型,并创建函数指针的匹配 typedef。例如:

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...

HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;

hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
                                           "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);
      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}

调用 GetProcAddress 时指定所需函数的方式取决于 DLL 的生成方式。

仅当要链接到的 DLL 是用模块定义 (.def) 文件生成的,并且序号在 DLL 的 .def 文件的 EXPORTS 部分中与函数一起列出时,才能获取导出序号。如果 DLL 具有许多导出函数,则相对于使用函数名,使用导出序号调用 GetProcAddress 的速度稍快一些,因为导出序号是 DLL 导出表的索引。使用导出序号,GetProcAddress 可直接定位函数,而不是将指定名称与 DLL 导出表中的函数名进行比较。但是,仅当有权控制 .def 文件中导出函数的序号分配时,才应使用导出序号调用 GetProcAddress

原文出处:http://www.cnblogs.com/kangwang1988/archive/2010/10/09/1846447.html

function LoadDLL_wpFunc: Boolean; begin Result := False; lhwpFunc := SafeLoadLibrary(DM.AppPath + DL_Funcs); if lhwpFunc <> 0 then try // @WriteToReg := GetProcAddress(lhwpFunc, PAnsiChar('_WriteToReg')); // @ReadFromReg := GetProcAddress(lhwpFunc, PAnsiChar('_ReadFromReg')); @FindTask := GetProcAddress(lhwpFunc, PAnsiChar('_FindTask')); @WpGetTempPath := GetProcAddress(lhwpFunc, PAnsiChar('_WpGetTempPath')); @DelTree := GetProcAddress(lhwpFunc, PAnsiChar('_DelTree')); // @XorCrypt := GetProcAddress(lhwpFunc, PAnsiChar('_XorCrypt')); @HelpShow := GetProcAddress(lhwpFunc, PAnsiChar('_HelpShow')); @EraseNode := GetProcAddress(lhwpFunc, PAnsiChar('_EraseNode')); @GetTreeText := GetProcAddress(lhwpFunc, PAnsiChar('_GetTreeText')); @FileSetAttribute := GetProcAddress(lhwpFunc, PAnsiChar('_FileSetAttribute')); @DeleteFiles := GetProcAddress(lhwpFunc, PAnsiChar('_DeleteFiles')); @WriteFormPos := GetProcAddress(lhwpFunc, PAnsiChar('_WriteFormPos')); @ReadFormPos := GetProcAddress(lhwpFunc, PAnsiChar('_ReadFormPos')); @IsInList := GetProcAddress(lhwpFunc, PAnsiChar('_IsInList')); @SetAutoStart := GetProcAddress(lhwpFunc, PAnsiChar('_SetAutoStart')); @IsValidFileName := GetProcAddress(lhwpFunc, PAnsiChar('_IsValidFileName')); @StrToListBox := GetProcAddress(lhwpFunc, PAnsiChar('_StrToListBox')); @StrToIDList := GetProcAddress(lhwpFunc, PAnsiChar('_StrToIDList')); @Str2Int := GetProcAddress(lhwpFunc, PAnsiChar('_Str2Int')); @WriteDebugFile := GetProcAddress(lhwpFunc, PAnsiChar('_WriteDebugFile')); @EncodeStream := GetProcAddress(lhwpFunc, PAnsiChar('EncodeStream')); @DecodeStream := GetProcAddress(lhwpFunc, PAnsiChar('DecodeStream')); @EncodeString := GetProcAddress(lhwpFunc, PAnsiChar('EncodeString')); @DecodeString := GetProcAddress(lhwpFunc, PAnsiChar('DecodeString')); @EncodeBase64 := GetProcAddress(lhwpFunc, PAnsiChar('EncodeBase64')); @DecodeBase64 := GetProcAddress(lhwpFunc, PAnsiChar('DecodeBase64')); @PrinterExist := GetProcAddress(lhwpFunc, PAnsiChar('_PrinterExist')); @DateUnitUnformat := GetProcAddress(lhwpFunc, PAnsiChar('_DateUnitUnformat')); @DateToName := GetProcAddress(lhwpFunc, PAnsiChar('_DateToName')); // @PowerCheck := GetProcAddress(lhwpFunc, PAnsiChar('_PowerCheck')); @FileCompare := GetProcAddress(lhwpFunc, PAnsiChar('_FileCompare')); @GetOnlyFileName := GetProcAddress(lhwpFunc, PAnsiChar('_GetOnlyFileName')); //////////////////////////////////////////////////////////////////////////////// @CombinePeg := GetProcAddress(lhwpFunc, PAnsiChar('_CombinePeg')); @StrToPeg := GetProcAddress(lhwpFunc, PAnsiChar('_StrToPeg')); @PegMarksReal := GetProcAddress(lhwpFunc, PAnsiChar('_PegMarksReal')); @PegMarksChar := GetProcAddress(lhwpFunc, PAnsiChar('_PegMarksChar')); @PegMarkSQLStr := GetProcAddress(lhwpFunc, PAnsiChar('_PegMarkSQLStr')); @PegKeyPress := GetProcAddress(lhwpFunc, PAnsiChar('_PegKeyPress')); @DoubleKeyPress := GetProcAddress(lhwpFunc, PAnsiChar('_DoubleKeyPress')); @IntKeyPress := GetProcAddress(lhwpFunc, PAnsiChar('_IntKeyPress')); @ClearStringGrid := GetProcAddress(lhwpFunc, PAnsiChar('_ClearGrid')); @FillGridNo := GetProcAddress(lhwpFunc, PAnsiChar('_FillGridNo')); @InsLeftCol := GetProcAddress(lhwpFunc, PAnsiChar('_InsLeftCol')); @InsRightCol := GetProcAddress(lhwpFunc, PAnsiChar('_InsRightCol')); @DelCol := GetProcAddress(lhwpFunc, PAnsiChar('_DelCol')); @AddLeftXCol := GetProcAddress(lhwpFunc, PAnsiChar('_AddLeftXCol')); @AddRightXCol := GetProcAddress(lhwpFunc, PAnsiChar('_AddRightXCol')); @DelXCol := GetProcAddress(lhwpFunc, PAnsiChar('_DelXCol')); @AddUpRow := GetProcAddress(lhwpFunc, PAnsiChar('_AddUpRow')); @AddDownRow := GetProcAddress(lhwpFunc, PAnsiChar('_AddDownRow')); @DelRow := GetProcAddress(lhwpFunc, PAnsiChar('_DelRow')); @RowIsNull := GetProcAddress(lhwpFunc, PAnsiChar('_RowIsNull')); @CalcGridError := GetProcAddress(lhwpFunc, PAnsiChar('_CalcGridError')); @CreateSGPtFixedRect := GetProcAddress(lhwpFunc, PAnsiChar('_CreateSGPtFixedRect')); @MultiCellDraw := GetProcAddress(lhwpFunc, PAnsiChar('_MultiCellDraw')); @ComnGridDrawCell := GetProcAddress(lhwpFunc, PAnsiChar('_ComnGridDrawCell')); @ComnTrvDrawItem := GetProcAddress(lhwpFunc, PAnsiChar('_ComnTrvDrawItem')); @BallyPeg := GetProcAddress(lhwpFunc, PAnsiChar('_BallyPeg')); @BallyPegEx := GetProcAddress(lhwpFunc, PAnsiChar('_BallyPegEx')); @BallyDate := GetProcAddress(lhwpFunc, PAnsiChar('_BallyDate')); @CompareBEPeg := GetProcAddress(lhwpFunc, PAnsiChar('_CompareBEPeg')); @CenterTextOut := GetProcAddress(lhwpFunc, PAnsiChar('_CenterTextOut')); @DrawGridText := GetProcAddress(lhwpFunc, PAnsiChar('_DrawGridText')); @XColInARowIsNull := GetProcAddress(lhwpFunc, PAnsiChar('_XColInARowIsNull')); @XColIsNull := GetProcAddress(lhwpFunc, PAnsiChar('_XColIsNull')); @isPegMark := GetProcAddress(lhwpFunc, PAnsiChar('_isPegMark')); @Amend := GetProcAddress(lhwpFunc, PAnsiChar('_Amend')); @FormatDouble := GetProcAddress(lhwpFunc, PAnsiChar('_FormatDouble')); @FormatFS := GetProcAddress(lhwpFunc, PAnsiChar('_FormatFS')); @FormatSS := GetProcAddress(lhwpFunc, PAnsiChar('_FormatSS')); @FormatSSCanNull := GetProcAddress(lhwpFunc, PAnsiChar('_FormatSSCanNull')); @GetCanNullValue := GetProcAddress(lhwpFunc, PAnsiChar('_GetCanNullValue')); @GetFirstRow := GetProcAddress(lhwpFunc, PAnsiChar('_GetFirstRow')); @MyTrim := GetProcAddress(lhwpFunc, PAnsiChar('_MyTrim')); @DToD := GetProcAddress(lhwpFunc, PAnsiChar('_DToD')); @Floor := GetProcAddress(lhwpFunc, PAnsiChar('_Floor')); @FormatLandSlide := GetProcAddress(lhwpFunc, PAnsiChar('_FormatLandSlide')); @Average := GetProcAddress(lhwpFunc, PAnsiChar('_Average')); @StdDev := GetProcAddress(lhwpFunc, PAnsiChar('_StdDev')); @Variance := GetProcAddress(lhwpFunc, PAnsiChar('_Variance')); @Absoluteness := GetProcAddress(lhwpFunc, PAnsiChar('_Absoluteness')); @Precision := GetProcAddress(lhwpFunc, PAnsiChar('_Precision')); @Deputation := GetProcAddress(lhwpFunc, PAnsiChar('_Deputation')); @MaxValues := GetProcAddress(lhwpFunc, PAnsiChar('_MaxValues')); @MinValues := GetProcAddress(lhwpFunc, PAnsiChar('_MinValues')); @Sum := GetProcAddress(lhwpFunc, PAnsiChar('_Sum')); @Log10 := GetProcAddress(lhwpFunc, PAnsiChar('_Log10')); @Log2 := GetProcAddress(lhwpFunc, PAnsiChar('_LogN')); @LogN := GetProcAddress(lhwpFunc, PAnsiChar('_LogN')); @CalcBelieveT := GetProcAddress(lhwpFunc, PAnsiChar('_CalcBelieveT')); @GetAngleT := GetProcAddress(lhwpFunc, PAnsiChar('_GetAngleT')); @GetIntension := GetProcAddress(lhwpFunc, PAnsiChar('_GetIntension')); @GetBayDegree := GetProcAddress(lhwpFunc, PAnsiChar('_GetBayDegree')); @GetTemperatureAmend := GetProcAddress(lhwpFunc, PAnsiChar('_GetTemperatureAmend')); @Beton3Intensity := GetProcAddress(lhwpFunc, PAnsiChar('_Beton3Intensity')); @SortBomp := GetProcAddress(lhwpFunc, PAnsiChar('_SortBomp')); @Sort := GetProcAddress(lhwpFunc, PAnsiChar('_Sort')); @SortInteger := GetProcAddress(lhwpFunc, PAnsiChar('_SortInteger')); @FindIndex := GetProcAddress(lhwpFunc, PAnsiChar('_FindIndex')); @FindIndexB := GetProcAddress(lhwpFunc, PAnsiChar('_FindIndexB')); @FormatStr2Num := GetProcAddress(lhwpFunc, PAnsiChar('_FormatStr2Num')); @MessageShow := GetProcAddress(lhwpFunc, PAnsiChar('_MessageShow')); @ShowDWG := GetProcAddress(lhwpFunc, PAnsiChar('_ShowDWG')); @ShowTimeMessage := GetProcAddress(lhwpFunc, PAnsiChar('_ShowMSG')); @IIF := GetProcAddress(lhwpFunc, PAnsiChar('_IIF')); //040703 @CanFocusMe := GetProcAddress(lhwpFunc, PAnsiChar('_CanFocus')); //061026 @CheckPrinterStatus := GetProcAddress(lhwpFunc, PAnsiChar('_CheckPrinterStatus')); //040703 @ShowMsgFmt := GetProcAddress(lhwpFunc, PAnsiChar('_ShowMsgFmt')); //040703 @ShowMsgPos := GetProcAddress(lhwpFunc, PAnsiChar('_ShowMsgPos')); //040703 @DispMSG := GetProcAddress(lhwpFunc, PAnsiChar('_DispMSG')); //040703 @ShowMSG := GetProcAddress(lhwpFunc, PAnsiChar('_ShowMSG')); //040703 @FirmMSG := GetProcAddress(lhwpFunc, PAnsiChar('_FirmMSG')); //040703 @FirmTimeMSG := GetProcAddress(lhwpFunc, PAnsiChar('_FirmTimeMSG')); //060815 @DialogMsg := GetProcAddress(lhwpFunc, PAnsiChar('_DialogMsg')); //061210 @InputDlg := GetProcAddress(lhwpFunc, PAnsiChar('_InputDlg')); //040703 @SetAppSilence := GetProcAddress(lhwpFunc, PAnsiChar('_SetAppSilence')); @GetAppSilence := GetProcAddress(lhwpFunc, PAnsiChar('_GetAppSilence')); @SmallToBig := GetProcAddress(lhwpFunc, PAnsiChar('_SmallToBig')); @GetGridMinHeight := GetProcAddress(lhwpFunc, PAnsiChar('_GetGridMinHeight')); @AddToArray := GetProcAddress(lhwpFunc, PAnsiChar('_AddToArray')); @AddToSubArray := GetProcAddress(lhwpFunc, PAnsiChar('_AddToSubArray')); @CurrStrToCurr := GetProcAddress(lhwpFunc, PAnsiChar('_CurrStrToCurr')); @SeparatorStr := GetProcAddress(lhwpFunc, PAnsiChar('_SeparatorStr')); @StrToStrList := GetProcAddress(lhwpFunc, PAnsiChar('_StrToStrList')); //20180831 @MultiSeparator := GetProcAddress(lhwpFunc, PAnsiChar('_MultiSeparator')); @SQLCondition := GetProcAddress(lhwpFunc, PAnsiChar('_SQLCondition')); @NewID := GetProcAddress(lhwpFunc, PAnsiChar('_NewID')); @IntToTimeString := GetProcAddress(lhwpFunc, PAnsiChar('_IntToTimeString')); @GetPdfPageCount := GetProcAddress(lhwpFunc, PAnsiChar('_GetPdfPageCount')); @IndexOfStrList := GetProcAddress(lhwpFunc, PAnsiChar('_IndexOfStrList')); //20180831 @DynamicCreateField := GetProcAddress(lhwpFunc, PAnsiChar('_DynamicCreateField')); //100321 @CopyField := GetProcAddress(lhwpFunc, PAnsiChar('_CopyField')); //100321 @CopyCDSFields := GetProcAddress(lhwpFunc, PAnsiChar('_CopyCDSFields')); //100321 @GetTheLastNode := GetProcAddress(lhwpFunc, PAnsiChar('_GetTheLastNode')); //100321 @VarToInt := GetProcAddress(lhwpFunc, PAnsiChar('_VarToInt')); //100321 @VarToFloat := GetProcAddress(lhwpFunc, PAnsiChar('_VarToFloat')); //100321 @CDSModified := GetProcAddress(lhwpFunc, PAnsiChar('_CDSModified')); //100321 @StrToCondition := GetProcAddress(lhwpFunc, PAnsiChar('_StrToCondition')); //100321 @SetControlItems := GetProcAddress(lhwpFunc, PAnsiChar('_SetControlItems')); //100321 @SetControlItemsEx := GetProcAddress(lhwpFunc, PAnsiChar('_SetControlItemsEx')); //100321 @CheckImageType := GetProcAddress(lhwpFunc, PAnsiChar('_CheckImageType')); //20190802 finally Result := True; end; // Sleep(100); end;
最新发布
10-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值