Search code examples
.netwpfdelphicomcom-interop

Call WPF Windows from COM within a Delphi App


i trying to call a COM-Object which is described here

Is it possible to package WPF window as COM Object

from a Delphi App.

After importing the tlb, i'm using this code to call that COM Object.

var
  y: ITestWpfInterface;
begin
  y:=CreateComObject(CLASS_TestWpfInterface) as ITestWpfInterface;
  y.TestWpf();
end;

the COM Object gets instantiated and the method is called. but the COM Objects crashes the Delphi app, when it is reaching the

Window1 form = new Window1();

line. when i'm trying to do the same from word-VBA the wpf windows is show.

Sub test()
  Dim x As New WpfControlLibrary1.TestWpfInterface
  x.TestWpf
End Sub

catch blocks around the new window does not show any exceptions. i have debugged the application with windbg. it shows me a div by 0 exception, after loading the framework wpf assembly's

why does the delphi app crash? what causes the crash. how to solve it?

edit: this is the output from windbg

ModLoad: 76330000 7634d000   C:\WINDOWS\system32\IMM32.DLL
ModLoad: 5b0f0000 5b128000   c:\windows\system32\uxtheme.dll
ModLoad: 75250000 7527e000   C:\WINDOWS\system32\msctfime.ime
ModLoad: 76f90000 7700f000   C:\WINDOWS\system32\CLBCATQ.DLL
ModLoad: 77010000 770e3000   C:\WINDOWS\system32\COMRes.dll
ModLoad: 79000000 7904a000   C:\WINDOWS\system32\mscoree.dll
ModLoad: 603b0000 60417000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll
ModLoad: 76970000 76a21000   C:\WINDOWS\system32\sxs.dll
ModLoad: 79140000 797ae000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\clr.dll
ModLoad: 79060000 7911e000   C:\WINDOWS\system32\MSVCR100_CLR0400.dll
ModLoad: 77f40000 77fb6000   C:\WINDOWS\system32\SHLWAPI.dll
(2b4.244): Unknown exception - code 04242420 (first chance)
ModLoad: 79880000 7a641000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\mscorlib\a774bd593b8420bae4a8cf1d46af3ba2\mscorlib.ni.dll
ModLoad: 60340000 6034d000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\culture.dll
ModLoad: 60930000 60940000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\nlssorting.dll
ModLoad: 03270000 03549000   C:\WINDOWS\system32\xpsp2res.dll
ModLoad: 79810000 79870000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\clrjit.dll
ModLoad: 7a830000 7b0dd000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\System\5286fe2d0167eb835a9f11025f1cb756\System.ni.dll
ModLoad: 58c20000 58fd1000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\WindowsBase\9dacf8a5033dfbcb435be166d2f42cdf\WindowsBase.ni.dll
ModLoad: 68000000 68036000   C:\WINDOWS\system32\rsaenh.dll
ModLoad: 55590000 5607d000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\PresentationCore\8244412387a82c0acd3d63622e22cef5\PresentationCore.ni.dll
ModLoad: 56980000 57aad000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\PresentationFramewo#\813a0913bea1269e48613509609e72b4\PresentationFramework.ni.dll
ModLoad: 561c0000 562a1000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\WPF\wpftxt_v0400.dll
ModLoad: 55000000 5519a000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\WPF\wpfgfx_v0400.dll
ModLoad: 554c0000 55585000   C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\WPF\PresentationNative_v0400.dll
ModLoad: 59200000 593b5000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\System.Xaml\a3ffdc1316821b5ceb32c9a788334329\System.Xaml.ni.dll
ModLoad: 60c90000 60d83000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\System.Configuration\4844dd28e0611d1ebd1e449fe822c2a5\System.Configuration.ni.dll
ModLoad: 69760000 69cbe000   C:\WINDOWS\assembly\NativeImages_v4.0.30319_32\System.Xml\419103071a5a5d17738afbe9dd03d58a\System.Xml.ni.dll
(2b4.244): Unknown exception - code c0000090 (first chance)
(2b4.244): Unknown exception - code c0000090 (first chance)
(2b4.244): Unknown exception - code c0000090 (first chance)
eax=000000c0 ebx=00007530 ecx=001b1e30 edx=00000020 esi=00000000 edi=0374ff50
eip=7c91e514 esp=0374ff20 ebp=0374ff78 iopl=0         nv up ei pl nz na pe nc
cs=001b  ss=0023  ds=0023  es=0023  fs=003b  gs=0000             efl=00000206
ntdll!KiFastSystemCallRet:
7c91e514 c3              ret

a German version of this question inclusive sample code can found here: http://www.delphipraxis.net/166199-com-interop-mit-einer-c-library-mit-wpf-und-com.html


Solution

  • delphi doesn't mask the exception from the wpf which are raised in the fpu.

    by setting a exception mask, the fpu ignores these exceptions.

    SetExceptionMask(
        [exInvalidOp, exDenormalized, exZeroDivide, 
        exOverflow, exUnderflow, exPrecision]
    );
    

    this function is located in the math unit.

    http://docwiki.embarcadero.com/VCL/en/Math.SetExceptionMask

    thanks goes to Bernhard Geyer, who had this idea.