🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.language
Article #26

dll�Ĵ��������

#26
From: �Ϲ⡤��������
Date: Wed, 13 Mar 2002 19:15
219 lines
14603 bytes
tiDLL�������Լ��
��������������������������������������������
��      ����˳��        ����ɾ��
stdcall     ������        ������
cdecl       ���ҵ���        ���÷���
pascal      ������        ������
register    ������        ������
��������������������������������������������
�˳���̱���ʱ����ر�stack_checking����������ñ���ָʾ {$S-} ����
������������������������������������������
//��������������������dll�Ĵ���
brary mydll
{$S-}
//����������������uses��Ԫ
uses
  classes,stdsys, form in 'form.pas'{form};
//��������������������������
var
  love:string;
  baby:integer;
  SaveExit: Pointer;��
//����������������������͹��
procedure myinnerproc();stdcall;         //�ڲ�ʹ�ù��
begin
 {�������}
end;
procedure myproc(var love:string);stdcall;export; //������Ե��ù��
begin
 {�������}
end;
function  myfunction(baby:integer):integer;stdcall;export;//�ɵ��ú���
begin
 {�������}
end;
procedure LibExit; far;
begin
  if ExitCode = wep_System_Exit then
    begin
     { ϵͳ�ر�ʱ����Ӧ���� }
    end
  else
    begin
    { DLLж��ʱ����Ӧ���� }
    end;
  ExitProc := SaveExit; { �ָ�ԭ�����˳����ָ�� }
end;��
//�����������������������˵��
exports
 myproc name 'myproc' index 1,
 myfunction name 'myfuntion' index 2 risdent;//�����Ϣʼ�ձ������ڴ���{risd
ent}
//����������������������ʼ������
begin
{DLL�ij�ʼ������ }
 SaveExit := ExitProc; { ����ԭ�����˳����ָ�� }
 ExitProc := @LibExit; { ��װ�µ��˳���� }
End.
������������������������������������������������������������������
��10.1 ExitCode��ȡֵ������ ��ȡ ֵ �� ��
������������������������������������������
WEP_System_Exit                 Windows�رա�
WEP_Free_DLLx                   DLLs���
��������������������������������������������
//--------------����dll
1����̬����
�� �ھ�̬����һ��DLLs�еĹ�̻���ʱ��externalָʾ���ӵ���̻�����������
�С�
�����õĹ�̻���������Զ����ģʽ�������ʹ��far���ָʾ��һ��{$F +}����ָ
ʾ��
Delphiȫ��֧�ִ�ͳWindows��̬���ӿ����е����ֵ��÷�ʽ�������ǣ�
������ ͨ����/������
������ ͨ����/����ı���
������ ͨ����/�����˳��š�
//������������������������������������������̬���þ���
unit windows
interface
 function FindWindowsEx(Parent,Child:hwnd;classname,windowsname:pchar):hwnd;
stdcall;
const
 user32='user32.dll'
implementation
 function FindWindowEx; external user32 name 'FindWindowExA'
������������������������������������������������������������������
2. ��̬���á�
2.1 ��̬�����е�API����
������̬������ʹ�õ�Windows API������Ҫ�����,����Loadlibrary��GetProcAddres
s��Freelibrary��
  1.Loadlibrary: ��ָ����ģ��װ���ڴ�
�����﷨Ϊ������function Loadlibrary(LibFileName: PChar): THandle;��
    LibFileNameָ����Ҫװ��DLLs���ļ������LibFileNameû�а�һ��·������
Windows������˳����в��ң�
��(1)��ǰĿ¼��
��(2)WindowsĿ¼(��win.com��Ŀ¼)������GetWindowDirectory������һĿ¼��·��
��
��(3)WindowsϵͳĿ¼(��ϵͳ�ļ���gdi.exe��Ŀ¼)������GetSystemDirectory����
��һĿ¼��·����
��(4)��ǰ�����ִ���ļ���Ŀ¼�����ú���GetModuleFileName���Է�����һĿ¼
��·����
��(5)����PATH���������е�Ŀ¼��
��(6)�����ӳ��Ŀ¼�б?
���������ִ�гɹ����򷵻�װ�ؿ�ģ���ʵ������򣬷���һ��С��HINSTANC
E_ERROR�Ĵ�����롣���������������±?��
��10.2 Loadlibrary���ش�����������
����������������������������������������������������������������������������

������롡�����������������⡡����������������
����������������������������������������������������������������������������

 0      ϵͳ�ڴ治������ִ���ļ����ƻ�����÷Ƿ�
 2      �ļ�û�б�����
 3      ·��û�б�����
 5      ��ͼ��̬����һ�����������һ����������籣����
 6      ����ҪΪÿ���������������ݶ�
 8      û���㹻���ڴ�����Ӧ�ó���
 10     Windows�汾����ȷ
 11     ��ִ���ļ��Ƿ������߲���WindowsӦ�ó��򣬻�����.EXEӳ�����д���
 12     Ӧ�ó���Ϊһ����ͬ�IJ���ϵͳ���(��OS/2����)
 13     Ӧ�ó���ΪMS DOS4.0���
 14     ��ִ���ļ������Ͳ�֪��
 15     ��ͼװ��һ��ʵģʽӦ�ó���(Ϊ����Windows�汾���)
 16     ��ͼװ�ذ��д�Ķ����ݶεĿ�ִ���ļ��ĵڶ���ʵ��
 19     ��ͼװ��һ��ѹ���Ŀ�ִ���ļ����ļ����뱻��ѹ����ܱ�װ��
 20     ��̬���ӿ��ļ��Ƿ�
 21     Ӧ�ó�����Ҫ32λ��չ
��������������������������������������������������������������������������
������Ӧ�ó�����Loadlibrary����ijһģ��ǰ������Ӧ�ó����ѰѸ�ģ��װ���ڴ棬��
Loadlibrary������װ�ظ�ģ�����һʵ�����ʹ��ģ��ġ����ü����1����
2.GetProcAddress����ȡ��ģ���к���ĵ�ַ
���﷨Ϊ������function GetProcAddress(Module: THandle; ProcName: PChar): TFa
rProc;��
  Module����õĺ����ģ��ľ�����ֵ��Loadlibrary���ء�����Module��
��Ϊnil�����ʾҪ���õ�ǰģ�顣
��ProcName��ָ���к��������nil��β���ַ��ָ�룬����Ҳ�����Ǻ���Ĵ���ֵ
�����ProcName�����Ǵ���ֵ�������ô���ֵ�ĺ�����ģ���в�������ʱ��GetProcA
ddress�Է���һ����nil��ֵ���⽫������ҡ���˴󲿷�������ú�������һ�ָ�õ�
ѡ������ú������������ƴд�����붯̬���ӿ��ļ�EXPORTS���еĶ�Ӧƴд��
һ�¡�
���GetProcAddressִ�гɹ����򷵻�ģ���к�����ڴ��ĵ�ַ�����򷵻�nil��
3.Freelibrary�����ڴ����Ƴ���ģ��
�����﷨Ϊ����procedure Freelibrary(Module : THandle);��
    ModuleΪ��ģ��ľ�����ֵ��Loadlibrary���ء�
�������ڿ�ģ�����ڴ���ֻװ��һ�Σ�������Freelibrary����ʹ��ģ������ü����
һ��������ü����Ϊ0����ж����ģ�顣
 ��ÿ����һ��Loadlibrary��Ӧ����һ��FreeLibray���Ա�֤�����ж���Ŀ�ģ����Ӧ
�ó��������������ڴ��С���
//������������������������ ��̬���þ���
��������GetProcAddess���صĺ���ָ��ʱ���������ǿ������ת������
   Order := TInstr(PFunc)(text,Key);
 ��TInStr��һ��������˵ĺ������ͣ���
type
  TInStr = function(Source: PChar;Check: Char): Integer;
//---------------------------------
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
var
 order: Integer;
 txt: PChar;
 PFunc: TFarProc;
 Moudle: THandle;
begin
  Moudle := Loadlibrary('c:\dlls\example.dll');
  if Moudle > 32 then
     begin
           Edit2.text := '';
           Pfunc := GetProcAddress(Moudle,'Instr');
    txt := StrAlloc(80);
    txt := StrPCopy(txt,Edit1.text);
    Order := TInstr(PFunc)(txt,Key);
    if Order = -1 then
    Label1.Caption := '��������ַ� '
  end else
        Label1.Caption := 'λ�ڵ�'+IntToStr(Order+1)+'λ';
  Freelibrary(Moudle);
end;
//������������������������ ����ʵ����ݴ����DLLs�ı�д��
����ʵ����ݴ����DLLs��һ��DLLs�ı�д����ͬ�������ر�ĵط��ǣ�
1. ����һ��ȫ�ֱ������
 var
   hMem: THandle;
2. ����һ����̣����ظ�ȫ�ֱ����ľ��ù��Ҫ����exports�Ӿ��С��磺��
 function GetGlobalMem: THandle; export;
 begin
    Result := hMem;
 end;
3. �ڳ�ʼ�������з���ȫ���ڴ�飺
 �����嵥���£���
begin
  hMem := GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,num);
  if hMem = 0 then
  MessageDlg('Could not allocate memory',mtWarning,[mbOK],0);
end.
//����������������������������������������������������������������
��num��һ��Ԥ����ij���
��10.3 ȫ���ڴ��ķ����־ ���� ־ �� ��
������������������������������������������������������������������
gmem_DDEShare �������Ӧ�ó�������ڴ�
gmem_Discardable �����������ڴ�(ֻ��gmem_Moveable����)
gmem_Fixed ����̶��ڴ�
gmem_Moveable ������ƶ����ڴ�
gmem_Nocompact ��ȫ�ֶ��е��ڴ治�ܱ�ѹ��������
gmem_Nodiscard ��ȫ�ֶ��е��ڴ治�ܱ�����
gmem_NOT_Banked ���䲻�ܱ��ֶε��ڴ�
gmem_Notify ֪ͨ���ܡ������ڴ汻����ʱ����GlobalNotify����
gmem_Zeroinit ��������ڴ������ݳ�ʼ��Ϊ��
��������������������������������������������������������������������
������Ԥ����ij�������ǣ�
GHND = gmem_Moveable and gmem_Zeroinit
GPTK = gmem_Fixed and gmem_Zeroinit
��������������������������������������������������������������������
�Ը��ĵ�����
      hicaci (2001-12-6 14:18:58)
���� ̫���������������ⶫ���ء�лл��
��Ҫ����

--

     MM MM ��MM��MM����ࣻ
      �罫MM�����³����ɡ�
�� ��Դ:���콭ҹ�� bbs.gliet.edu.cn��[FROM: 192.168.89.122]
Technical Details
Message-ID: <3lJGRa$xPM@bbs.gliet.edu.cn>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!feedme.news.mediaways.net!priapus.visi.com!zeus.visi.com!news-out.visi.com!hermes.visi.com!nnxp1.twtelecom.net!logbridge.uoregon.edu!newsgate.cuhk.edu.hk!cuhk-bullish!zixia.net!jnunews.dhs.org!LiRiverBBS

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: faaec,53067204e535977d,start
X-Google-Attributes: gidfaaec,public
X-Google-ArrivalTime: 2002-03-13 04:01:38 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!feedme.news.mediaways.net!priapus.visi.com!zeus.visi.com!news-out.visi.com!hermes.visi.com!nnxp1.twtelecom.net!logbridge.uoregon.edu!newsgate.cuhk.edu.hk!cuhk-bullish!zixia.net!jnunews.dhs.org!LiRiverBBS
From: Single.bbs@bbs.gliet.edu.cn (�Ϲ⡤��������)
Newsgroups: cn.bbs.comp.language
Subject: dll�Ĵ��������
Date: 13 Mar 2002 19:15:38 GMT
Organization: �콭ҹ��BBS
Message-ID: <3lJGRa$xPM@bbs.gliet.edu.cn>
X-Filename: programming/M.1016046900.A
Lines: 219