Search code examples
c#delphivisual-studio-debuggingdebug-symbolsdllexport

C# DLL (using 3F DLLExport for exporting functions) not debuggable in Delphi application


I'm trying to export functions from a C# Class Library (.Net Framework 4.8) to later use it in a Delphi project.

C# part:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ClassLibrary1
{
    public class Class1
    {
        [DllExport (CallingConvention = CallingConvention.StdCall)]
        public static void Init()
        {
            MessageBox.Show("Hallo von testprogramm c#");
        }
    }
}

The C# project is set to X86 only, and I used 3F/DLLExport:

DllExport Setup Screen

In Visual Studio, I enabled "Debug of native Code" in the Project Settings.

I compile in "Debug" Mode, therefore Debug symbols get generated (I see a .pdb file next to the DLL).

Delphi part:

procedure TestDLL();
var
  dllHandle : THandle;
  dllInit: PInit;
begin
  dllHandle := SafeLoadLibrary('path\to\ClassLibrary1\bin\x86\Debug\ClassLibrary1.dll');
  dllInit := GetProcAddress(dllHandle, 'Init');
  dllInit();
end;

When I call the Delphi procedure, the DLL gets correctly attached and the Init function get called. The MessageBox appears on the screen.

Problem

When I attach Visual Studio to the running Delphi program, Visual Studio claims that there are no symbols loaded for the DLL, and therefore I'm not able to debug the C# code.

Does anyone have any idea how to fix this? I did this sort of thing in the past and never had this kind of problem.


Solution

  • I found the solution: In the .csproj of the C# Project I need to specifiy the PDB to Full

    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
    

    If you start the Host application through Visual Studio (Startprofile) check the checkbox mixed debugging. And specify the host application. Then you can debug into the DLL. If you want to attach to an already running host application you need to specify the correct debugger type in the "Attach to process" window. Default it is set to "automatic (native code)" this is in this case wrong as we don't want to debug native code. Manually selection "Managed (.Net 4.x)" is the solution. manually selection debugger type