Compiling DLL files without output function names in Delphi

Source: Internet
Author: User
With Delphi with long, always found that some and MS is different. For example, the Public Library of MS often hides many important functions, which often play a very huge role in the system. Once you know how to call it, You can provide strong functions and flexibility for your applications. However, these functions usually have no function name (that is, using exclusive to view the export table of the DLL file does not see the function meaning), and only one serial number is used for representation. Sometimes I think again, why can't I learn to hide some functions that I don't want to disclose when I write a program?
In fact, when using Delphi to write a DLL, you can use simple techniques to hide the function name. Let's take a look at the following DLL source code: Library proDll;
Uses
Windows;
{$ R *. res}
Procedure ShowMessageA (hWnd: HWND); stdcall;
Begin
MessageBox (hWnd, 'you are calling the ShowMessageA function', 'dll function information ',
MB_ICONINFORMATION );
End;
Procedure ShowMessageB (hWnd: HWND); stdcall;
Begin
MessageBox (hWnd, 'you are calling the ShowMessageB function', 'dll function information ',
MB_ICONINFORMATION );
End;
Exports
ShowMessageA index 1 name '',
ShowMessageB index 2 name '';
Begin
End.

 

Note: In the exports section, use the index keyword to specify the serial number of the output function, followed by a name keyword to specify the name of the output function. The key is here. name is followed by an empty string, which generates an empty string name for the function. The actual effect is to hide the name of the output function. Is it easy?
How can we call such an output function? Without the function name, we can call it differently. In fact, you don't have to worry about it. The call is also very simple. I have created two projects for static call and dynamic call. The source code is as follows:
Static call example: Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type
TForm1 = class (TForm)
Button1: TButton;
Button2: TButton;
Procedure Button1Click (Sender: TObject );
Procedure Button2Click (Sender: TObject );
Private
{Private declarations}
Public
{Public declarations}
End;
Var
Form1: TForm1;
Implementation
{$ R *. dfm}
Procedure ShowMessageA (hWnd: HWND); stdcall; external 'prodll. dll 'index 1;
Procedure ShowMessageB (hWnd: HWND); stdcall; external 'prodll. dll 'index 2;
Procedure TForm1.Button1Click (Sender: TObject );
Begin
ShowMessageA (Handle );
End;
Procedure TForm1.Button2Click (Sender: TObject );
Begin
ShowMessageB (Handle );
End;
End.

 

Dynamic call example: Unit Unit2;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
Type
TForm2 = class (TForm)
Button1: TButton;
Button2: TButton;
Procedure button1click (Sender: tobject );
Procedure button2click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form2: tform2;
Implementation
{$ R *. DFM}
Type
Tdllshowmessagefunc = procedure (hwnd: hwnd); stdcall;
VaR
Hdllhandle: thandle;
Showmessagea, showmessageb: tdllshowmessagefunc;
Procedure loadfuncdll;
Begin
If hDllHandle = 0 then
Begin
HDllHandle: = LoadLibrary ('prodll. dll ');
If hDllHandle = 0 then
Raise Exception. Create ('loading proDll. dll failed ');
Try
{
LpProcName: the second argument of function GetProcAddress
Points to a null-terminated string containing the function name,
Or specifies the function's ordinal value. If this parameter is
An ordinal value, it must be in the low-order word; the high-order
Word must be zero.
}
@ ShowMessageA: = GetProcAddress (hDllHandle, Pointer (HiWord (0) or LoWord (1 )));
If @ ShowMessageA = nil then
Raise Exception. Create ('the ShowMessageA function is not output in proDll. dll ');
@ ShowMessageB: = GetProcAddress (hDllHandle, Pointer (HiWord (0) or LoWord (2 )));
If @ ShowMessageB = nil then
Raise Exception. Create ('the ShowMessageB function is not output in proDll. dll ');
Except
FreeLibrary (hDllHandle );
HDllHandle: = 0;
Raise;
End;
End;
End;
Procedure FreeFuncDll;
Begin
If hDllHandle <> 0 then
Begin
FreeLibrary (hDllHandle );
HDllHandle: = 0;
@ ShowMessageA: = nil;
@ ShowMessageB: = nil;
End;
End;
Procedure tform2.button1click (Sender: tobject );
Begin
If @ showmessagea = nil then loadfuncdll;
Showmessagea (handle );
End;
Procedure tform2.button2click (Sender: tobject );
Begin
If @ showmessageb = nil then loadfuncdll;
Showmessageb (handle );
End;
Initialization
// Do nothing
Finalization
Freefuncdll;
End.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.