delphicomqueryinterface

Access violation after GetInterface/QueryInterface in Delphi


First, I'm very new in Delphi and COM, but I should build COM application in Delphi. I read a lot of articles and notes on the internets, but COM and COM in Delphi are still not clear to me.

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

I try to make a COM Interface and Impl class - it works if I call an interface method from Delphi (I create an impl object via TestClient.Create), but if I try to create an object from outer world (from Java, via com4j) my application crashed with 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 come out from function - all crashes.

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


Solution

  • I made a dll with COM from scratch and

    1. I use DllRegisterServer - it gave me a possibility to control server registration (over TComObjectFactory.RegisterClassObject in my first attempt)
    2. I remove QueryInterface from my TestComImpl
    3. Com4j supports only STA (Apartment) threading model (I assume RegisterClassObject uses MTA)
    4. So if class registered as Apartment (STA) or Both - com4j can create instances.

    Thanks everyone for help!