Search code examples
delphicomdelphi-xetypelib

Replace RIDL file and typelib in Delphi XE3


I wrote a test project which exposes a COM factory and object. Having tested and found our implementation to be working - i now want to move the code + typelibrary into our main project. I copied the RIDL + TLB file into the main-project folder. I renamed both files accordingly (to the main project name). I also deleted the project resource file just in case Delphi places the TLB data here. And naturally i deleted the old TLB and RIDL files (I also deleted all DCU files, just in case). Last, I added the RIDL file to the main project and compiled.

The RIDL file is picked up by Delphi during compilation, it generates the TLB file as expected, but somehow it doesnt inject the TLB data into the exefile. So when i start the application i simply get an error message "Unable to load typelibrary". In my code i automatically create the COM factory on startup and update the windows registry. On shutdown the reverse happens.

Having fiddled with the typelibrary editor for hours, trying to register the typelibrary/exe from both Delphi and shell/regsvr32, delphi for some magical reason picked up the new file and everything worked as expected.

Problem is, I have no idea "why" it didnt work to begin with, nor do i know why it worked after hours of trying different combinations. So I am unable to reproduce the success. When updating our codebase I had to do the same on another computer/revision -- but this time it refused to work.

How can I force Delphi to inject the typelibrary? Is there some "trick" (except the obvious like refreshing the RIDL, which generates the unit correctly but doesnt do anything about the TLB resource problem).


Solution

  • As I understand it the steps are as follows:

    1. Add the .ridl file to the project. This will result in the build process compiling it to a .tlb file.
    2. Link the .tlb resource with a $R conditional directive like this: {$R MyProject.tlb}