Problems with Imported Type Library Declarations in Delphi

Since I’ve been working on the HTML/ASP.NET designer one of the things I’ve
noticed over the years is that our type library import tool improperly imports
certain declarations particularly when it comes to const (or [in]) parameters. 
I’ve run into method declarations where at least one parameter is declared as
“var” when in fact it should have been declared as “const”.  At best, this
results in no error but more frequently results in an application AV.  I’ve
talked to our type library gurus but unfortuantely this didn’t get addressed in
Delphi 2005 thus our
importer still suffers this problem.  Don’t worry I’ll keep at it.  🙂

Anyway, if you have imported a type library and you are pulling your hair
trying to call a method on some object try double checking that the method in
question is actually declared correctly.

For example, IWebBrowser2.Navigate is declared as follows once imported using

procedure Navigate(const URL: WideString; var
Flags: OleVariant; var TargetFrameName: OleVariant;
var PostData: OleVariant; var Headers: OleVariant); safecall;

Notice that the last four parameters are declared as “var” when in fact they
should have been declared as “const”.  If you try using this method you could
run into problems as a result of this misdeclaration.  The actual declaration
should be:

procedure Navigate(const URL: WideString; const
Flags: OleVariant; const TargetFrameName: OleVariant;
const PostData: OleVariant; const Headers: OleVariant); safecall;

If you take a look at the declaration here
on MSDN you’ll notice that all of the parameters are specificed as [in] in the
Parameters section so it doesn’t make a lot of sense for them to be declared as
“var“ in Pascal.  So, if you can’t figure out why you’re unable to call an
interface method from Delphi from a type library that you’ve imported double
check that the method is in fact declared correctly.

On a bit of a side note, I generated the MSHTML.pas
file that we use in the IDE with the following tlibimp.exe command

tlibimp -Hs- -Hr- -Fe- \WINNT\SYSTEM32\mshtml.tlb

If you are running into problems specifically with MSHTML.pas please leave a
comment and if there is sufficient interest I’ll look to post our version of
MSHTML.pas to CodeCentral.