Traps in the interface type when mixing calls

Source: Internet
Author: User

[Delphi]View PlainCopy 
    1. Function ABC (A:integer): IUnknown;

This is a Delphi function declaration, it looks very simple, only one parameter, but the real situation? After being translated into binary code, there are actually 2 parameters for the function!


In order to explain the problem in more detail, first use Delphi to write a DLL, export an interface, the interface has a show method.

[Delphi]View PlainCopy  
  1. Library Project1;
  2. Uses
  3. Windows;
  4. {$R *.res}
  5. Type
  6. ITest = Interface
  7. procedure Show (); stdcall;
  8. end;
  9. TTest = Class (Tinterfacedobject, ITest)
  10. Public
  11. procedure Show (); stdcall;
  12. end;
  13. function gettest:itest; stdcall;
  14. Begin
  15. Result: = TTest.  Create;
  16. End
  17. Exports
  18. Gettest;
  19. {TTest}
  20. Procedure TTest.  Show;
  21. Begin
  22. OutputDebugString (' Hello World ');
  23. End
  24. Begin
  25. End.


Callers are written in C + +

[CPP]View PlainCopy 
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <Windows.h>
  4. Interface ITest: public IUnknown
  5. {
  6. virtual void __stdcall Show () = 0;
  7. };
  8. typedef itest* (WINAPI *getitest) ();
  9. int _tmain (int argc, _tchar* argv[])
  10. {
  11. hmodule h = LoadLibrary (TEXT ("Project1.dll"));
  12. if (h! = 0)
  13. {
  14. Getitest get = (getitest) GetProcAddress (H, "gettest");
  15. ITest *test = Get ();
  16. Test->show ();
  17. Test->release ();
  18. }
  19. System ("pause");
  20. return 0;
  21. }


Directly eject a memory error after running

The error statement is in the DLL

[Delphi]View PlainCopy 
    1. function gettest:itest; stdcall;
    2. Begin
    3. Result: = TTest.  Create;
    4. End


You can see the problem by looking at the function in the form of disassembly code.

As you can see, when the function return value is an interface type, the return value is actually an implicit parameter and is a level two pointer type. The use of Dephi does not find the problem because it is automatically optimized.

In multi-language mixed programming, there is an empty memory error when you return an interface or an object directly.

The modified calling code

[CPP]View PlainCopy 
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <Windows.h>
  4. Interface ITest: public IUnknown
  5. {
  6. virtual void __stdcall Show () = 0;
  7. };
  8. The correct function prototype
  9. typedef VOID (WINAPI *getitest) (itest**);
  10. int _tmain (int argc, _tchar* argv[])
  11. {
  12. hmodule h = LoadLibrary (TEXT ("Project1.dll"));
  13. if (h! = 0)
  14. {
  15. Getitest get = (getitest) GetProcAddress (H, "gettest");
  16. //Modified call method
  17. ITest *test = nullptr;
  18. Get (&test);
  19. Test->show ();
  20. Test->release ();
  21. }
  22. System ("pause");
  23. return 0;
  24. }


Finally, a little experience can be summed up, when the Delphi function return value is the interface type, the function will consider the first parameter is an interface buffer, used to accept the interface instance object.

Is it possible to return an interface pointer directly without changing the way the C + + side calls? The answer is yes, as long as you change the return data type to the underlying type

[Delphi]View PlainCopy 
    1. function Gettest:pointer; stdcall;
    2. Var
    3. Temp:itest;
    4. Begin
    5. Temp: = TTest.  Create;
    6. Temp. _addref;
    7. Result: = Pointer (Temp);
    8. End


Since the function return value is no longer an interface type, Delphi does not invoke the AddRef method of the interface to call the reference count + 1, so the AddRef method must be called manually after the interface is created.

Otherwise, the function will automatically release temp after the end, causing the return value to be a wild pointer.

http://blog.csdn.net/aqtata/article/details/19079737

Traps in the interface type when mixing calls

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.