Access Violation after GetInterface / QueryInterface in Delphi

Firstly, I am very new to Delphi and COM, but I need to create a COM application in Delphi. I have read many articles and notes on the internet, but COM and COM in Delphi are still not clear to me.

My sources are http://www.everfall.com/paste/id.php?wisdn8hyhzkt (about 80 lines).

I am trying to make a COM interface and Impl class - it works if I call the interface method from Delphi (I create an impl object through TestClient.Create), but if I try to create an object from the outside world (from Java, through com4j) my application crashed with the following exception:

Project Kernel.exe raised exception class $C0000005 with 
message 'access violation at 0x00000002: read of address 0x00000002'.

      

If I set a breakpoint in QueryInterface - it breaks, but when I exit the function - everything crashes.

What am I doing wrong? What am I still missing? What can / should I read about COM (in Delphi) to avoid stupid questions like this?

+2


a source to share


3 answers


I made a dll from COM from scratch and

  • I am using DllRegisterServer - it gave me control over server registration (more TComObjectFactory.RegisterClassObject in my first try)
  • I am removing QueryInterface from my TestComImpl
  • Com4j only supports STA (Apartment) threading model (I assume using RegisterClassObject MTA)
  • So, if a class registered as Apartment (STA) or Both is com4j can instantiate.


Thanks everyone for the help!

0


a source


There is no need to implement IUnkown.QueryInterface. Remove this method from TestComImpl and let TComObject handle it. Also don't forget to specify the ITestCom GUID interface.



+2


a source


If the crash occurs after QueryInterface returns, then I would put a breakpoint in the Java application when it calls QueryInterface and see what it tries to do next. This will give you an idea of ​​where to look.

Your comment seems to confirm this. It calls QueryInterface, returning a result that says the interface is good, and tries to use it for something that breaks immediately. But if you comment out the code that tells him the interface is good, he doesn't try to use the interface and nothing breaks.

If you are not familiar with Delphi, an access violation usually means dereferencing a null pointer. It says that the instruction pointer is at memory location 0x000002. This probably means that you have somehow tried to call a virtual method (or an interface method) on an object that has not yet been created.

Hope it helps!

0


a source







All Articles