3

I have developed a COM library in Delphi. When I try to "add reference" in a Visual Studio project, VS crashes. I tried to build Interop assembly by tlbimp.exe and it failed with:

TlbImp : error TI1000 : The type library importer encountered an unexpected exception: System.AccessViolationException - Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Interestingly when I "add reference" to a Web Application (or a Web Site) project, it successfully generates Interop dll and adds the reference.

It is important to me to know how tlbimp.exe is employed by Web Application project because I want to customize Interop generation.

Bahribayli
  • 346
  • 2
  • 11
  • 1
    Try the open-source TlbImp, you can debug it: http://clrinterop.codeplex.com/releases/view/17579 – noseratio May 04 '14 at 03:52
  • 1
    @Noseratio It was a good idea. I debugged down the code and found an access violation when TlbImp tries to retrieve a function description using a function DISPID. I didn't have enough time to exactly spot the problem but possibly it's a bug in TlbImp. – Bahribayli May 06 '14 at 06:46
  • I guess the best thing you could do (besides [reporting a bug](http://connect.microsoft.com/VisualStudio/)) is to try to change the interface of your Delphi component, so it doesn't upset TlbImp. – noseratio May 06 '14 at 06:55
  • @Noseratio I will report the bug as soon as I gather enough information to reproduce the bug. The problem occurs just for one CoClass. Other interfaces and CoClasses are well. – Bahribayli May 06 '14 at 07:00

1 Answers1

1

There is most likely an error in your Delphi code. You can debug the COM library in Delphi like so:

enter image description here Choose run-> parameters...

enter image description here

(Never mind the fact that this says project28.exe; it works the same for Projectx.dll)

I commonly develop plugins for Excel using COM.
In that case my host application is Excel.exe.

When you've entered this info you'll be able to run the library.
When you do Delphi will start the host application and wait until it somehow invokes your library.
If you've set any breakpoints Delphi will pause the action and allow you to debug.

Because you know which functions you call in your C# code, it shouldn't be difficult to see where to set the first breakpoint.

Delphi will also break on any exceptions.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • @John, I test and debugged my COM library with a (non-dotnet) host application. My problem occurs when I "add reference" to a dotnet application. The reference can't be added because Interop dll isn't even generated. – Bahribayli May 03 '14 at 20:59
  • Try to import the library (the dll you created) in Delphi. If that works then perhaps you can use the files that Delphi generates in that process in C#. – Johan May 03 '14 at 21:04
  • @John, I use Delphi XE not Delphi Prism. So I don't think Delphi will generate any file useful for C#. Am I right? – Bahribayli May 03 '14 at 21:14
  • Not sure, but you A: will learn if the type-lib is importable at all. B: can perhaps use a tool to translate the Delphi file to a file C# will understand, C: perhaps import the Delphi file into C# directly. – Johan May 03 '14 at 21:53
  • @John, thanks. I could managed to use the COM library with other host applications. It, at least, gave me some idea. BTW when I "add reference" to a Web Site application in Visual Studio it successfully generates Interop dll that can be used in dotnet application. Just I wonder why it is possible that way and problematic the other way. I can't see how tlbimp.exe is employed in a Web Site project to generate the Interop dll. – Bahribayli May 03 '14 at 22:00
  • 1
    Must be a bug in tlbimp.exe – Johan May 04 '14 at 06:50
  • I think you are right. Although, debugging open source TlbImp, I have not yet spotted the exact problem but possibly its a bug in TlbImp.exe. – Bahribayli May 06 '14 at 06:49