The communication call between the DLL and the EXE and the thread execution space of the callback function

Source: Internet
Author: User

There are many kinds of communication between DLL and EXE, this paper uses the method of callback function, this article will also study multi-threaded, multi-module case, the callback function where the thread, do not say, the first attached code:

The following is the DLL module, the DLL's project file:

[Delphi]View Plaincopy
  1. Library Dllapp;
  2. Uses
  3. Windows
  4. Sysutils,
  5. Classes,
  6. Dllclass in ' Dllclass.pas ';
  7. {$R *.res}
  8. var
  9. Gdllserver:tdllserver;
  10. function Addserver (adispatchfunc:tdispatchfunc): HRESULT; stdcall;
  11. begin
  12. Result: = error_invalid_function;
  13. If not Assigned (gdllserver) Then
  14. Exit;
  15. Gdllserver.  Addserver (Adispatchfunc);
  16. Result: = ERROR_SUCCESS;
  17. end;
  18. function Datadispatch (acommand:integer): HRESULT; stdcall;
  19. begin
  20. Result: = error_invalid_function;
  21. Gdllserver.  Datadispatch (Acommand);
  22. Result: = 0;
  23. end;
  24. function Dllinitialize:hresult;
  25. begin
  26. Result: = 1;
  27. Gdllserver: = Tdllserver. Create;
  28. Result: = ERROR_SUCCESS;
  29. end;
  30. function Dllfinalize:hresult;
  31. begin
  32. Result: = error_invalid_function;
  33. Gdllserver.  Free;
  34. Gdllserver: = nil;
  35. Result: = ERROR_SUCCESS;
  36. end;
  37. Exports
  38. Addserver,
  39. Datadispatch,
  40. Dllinitialize,
  41. Dllfinalize;
  42. Begin
  43. End.

The type file in the DLL

[Delphi]View Plaincopy
  1. Unit Dllclass;
  2. Interface
  3. Uses
  4. Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, Stdctrls;
  6. Type
  7. Tdispatchfunc = function (acommand:integer): Hresult;stdcall;
  8. Type
  9. Tdllserver = CLAss;
  10. Tdllthread = Class (TThread)
  11. Private
  12. Fcount:integer;
  13. Fdllserver:tdllserver;
  14. Public
  15. procedure Execute; override;
  16. Public
  17. constructor Create;
  18. end;
  19. Tdllserver = class
  20. Private
  21. Fdispatchfunc:tdispatchfunc;
  22. Fdllthread:tdllthread;
  23. Public
  24. procedure Addserver (Adispatchfunc:tdispatchfunc);
  25. procedure Datadispatch (Acommand:integer);
  26. Public
  27. constructor Create;
  28. destructor Destroy;
  29. end;
  30. Implementation
  31. {Tdllserver}
  32. Save EXE's callback function pointer
  33. Procedure Tdllserver.  Addserver (Adispatchfunc:tdispatchfunc);
  34. Begin
  35. Fdispatchfunc: = Adispatchfunc;
  36. End
  37. Constructor Tdllserver. Create;
  38. Var
  39. lthreadid:cardinal;
  40. Begin
  41. ///
  42. Lthreadid: = GetCurrentThreadID;
  43. ShowMessage (' dll,create: ' +inttostr (Lthreadid));
  44. Fdllthread: = Tdllthread. Create;
  45. Fdllthread.  Fdllserver: = self;
  46. Fdllthread.  Resume;
  47. End
  48. DLL accepts EXE pass-through instruction, DLL leaves EXE to invoke an excuse
  49. Procedure Tdllserver.  Datadispatch (Acommand:integer);
  50. Var
  51. lthreadid:cardinal;
  52. Begin
  53. If not Assigned (fdispatchfunc) Then
  54. Exit;
  55. Fdispatchfunc (Acommand);
  56. Lthreadid: = GetCurrentThreadID;
  57. ShowMessage (' Dll,datedispatch: ' +inttostr (Lthreadid));
  58. End
  59. destructor Tdllserver.  Destroy;
  60. Begin
  61. ////
  62. End
  63. {Tdllthread}
  64. Constructor Tdllthread. Create;
  65. Var
  66. lthreadid:cardinal;
  67. Begin
  68. Fcount: = 0;
  69. inherited Create (True);
  70. End
  71. The purpose of this function is to verify the execution thread of the callback function under different threads under different modules.
  72. Procedure Tdllthread.  Execute;
  73. Var
  74. lthreadid:cardinal;
  75. Begin
  76. inherited;
  77. While not Terminated do
  78. begin
  79. INC (Fcount);
  80. if Fcount = 2 Then
  81. begin
  82. Lthreadid: = GetCurrentThreadID;
  83. ShowMessage (' dll,thread--' +inttostr (Lthreadid));
  84. end;
  85. If Fcount = 5 Then
  86. begin
  87. if assigned (fdllserver) Then
  88. begin
  89. Fdllserver.  Fdispatchfunc (2);
  90. end;
  91. end;
  92. Sleep (1000);
  93. end;
  94. End
  95. End.

EXE's files

[C-sharp]View Plaincopy
  1. Unit Execlass;
  2. Interface
  3. Uses
  4. Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, Stdctrls;
  6. Type
  7. Tdispatchfunc = function (Acommand:integer): Hresult;stdcall;
  8. Tserverfunc = function (adispatchfunc:tdispatchfunc): Hresult;stdcall;
  9. Tproc = Function:hresult;
  10. Type
  11. TForm2 = Class (Tform)
  12. Button1:tbutton;
  13. Memo1:tmemo;
  14. Procedure Formdestroy (Sender:tobject);
  15. Procedure Button1Click (Sender:tobject);
  16. Procedure Formcreate (Sender:tobject);
  17. Private
  18. {Private declarations}
  19. Fserverdispatch:tserverfunc; ///exe--->dll
  20. Fdatadispatch:tdispatchfunc; ///exe--->dll
  21. Fdllinitialize:tproc;
  22. Fdllfinalize:tproc;
  23. Public
  24. {Public declarations}
  25. End
  26. Var
  27. Form2:tform2;
  28. Ghandlelibrary:thandle;
  29. Gcount:integer;
  30. Implementation
  31. {$R *.DFM}
  32. DLL--->exe, the callback function left for DLL calls
  33. function Datadispatch (Acommand:integer): HRESULT; stdcall;
  34. Var
  35. lthreadid:cardinal;
  36. Begin
  37. Lthreadid: = GetCurrentThreadID;
  38. ShowMessage (' EXE: ' +inttostr (Lthreadid));
  39. End
  40. Procedure Tform2.button1click (Sender:tobject);
  41. Begin
  42. INC (Gcount);
  43. Fdatadispatch (Gcount);
  44. End
  45. Procedure Tform2.formdestroy (Sender:tobject);
  46. Begin
  47. Fdllfinalize;
  48. Fserverdispatch: = nil;
  49. Fdatadispatch: = nil;
  50. Fdllinitialize: = nil;
  51. Fdllfinalize: = nil;
  52. FreeLibrary (ghandlelibrary);
  53. End
  54. Procedure Tform2.formcreate (Sender:tobject);
  55. Var
  56. lthreadid:cardinal;
  57. Begin
  58. Ghandlelibrary: = LoadLibrary (PChar (' DLLAPP.dll '));
  59. @FDllInitialize: = GetProcAddress (ghandlelibrary, ' dllinitialize ');
  60. @FDllFinalize: = GetProcAddress (ghandlelibrary, ' dllfinalize ');
  61. @FServerDispatch: = GetProcAddress (ghandlelibrary, ' addserver ');
  62. @FDataDispatch: = GetProcAddress (ghandlelibrary,' Datadispatch ');
  63. Fdllinitialize;
  64. Fserverdispatch (Datadispatch);
  65. Lthreadid: = GetCurrentThreadID;
  66. MEMO1.LINES.ADD (IntToStr (Lthreadid));
  67. End
  68. End.

1, the simple implementation of the DLL and the communication between the EXE, in fact, the use of the DLL's export function, first think of the DLL to pass the address of a callback function, for the DLL-oriented EXE communication. EXE DLL-oriented communication directly execute the DLL export function can be

2, this article is the most basic implementation, of course, the callback function, as well as the handler function of the DLL, can be implemented at the level of the packet, that is: define different packets, according to the command to execute different functions, so long as the export of a function, retain a callback function can achieve a large number of functions

3, on the thread execution space of the callback function, depending on the caller's thread, such as the callback EXE function in the DLL thread, the callback function is executed on the DLL thread. If it is the main thread that executes the callback, then the main thread. The above code Gdllclass creation process is the export function, so the main thread of Gdllclass is the main thread of EXE, they are in a threaded space

http://blog.csdn.net/procedure1984/article/details/3985127

The communication call between the DLL and the EXE and the thread execution space of the callback function

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.