How do I use the MS DIA SDK from C#?
Asked Answered
Y

4

8

I'm trying to use the Microsoft Debug Interface Access SDK from C#. This is installed with Visual Studio, but the docs don't seem to mention how you use this from C#.

I've found example code on interweb but no information on how to link to the DIA SDK. I.e. I can't import it as an assembly. I don't think I have to include it into a managed C++ application and use it as COM (that would be hell).

There is an IDL file, is this the correct way? If so, how?


Edit: The following will create the type library for use as a referenced assembly. Paste into a batch file.

call "%VS80COMNTOOLS%\vsvars32.bat"
midl /I "%VSINSTALLDIR%\DIA SDK\include" "%VSINSTALLDIR%\DIA SDK\idl\dia2.idl" /tlb dia2.tlb
tlbimp dia2.tlb
Yuen answered 30/3, 2009 at 14:52 Comment(0)
U
16

You need to convert the IDL to a typelib first:

Something like:

midl /I "%VSINSTALLDIR%\DIA SDK\include" dia2.idl /tlb dia2.tlb
tlbimp dia2.tlb

Then you can import the tlb.

I've never used the DIA SDK this way, so don't know how friendly it would be. You could also consider using it directly from a managed C++ assembly and presenting a managed interface to the functionality you need.

Unthankful answered 30/3, 2009 at 15:9 Comment(4)
Thanks. Would that be using the DllImport PInvoke stuff?Yuen
You said you've "never used the DIA SDK this way". If you don't use it that way, how do you use it?Yuen
Once you've run tlbimp on the generated tlb you should get an assembly you can reference from your managed code. The assembly will do the COM interop from you and there is no need to dllimport or PInvoke anything. I've only used it from unmanaged C++Unthankful
Great, thanks. I'll paste my batch file in above. I have a DLL now. I did a lot of this quite a while ago and forgot the details.Yuen
P
4

The previous instructions worked, but needed some updating. VSINSTALLDIR doesn't exist anymore (and is ambiguous when you have multiple VS versions installed) so I generalized and corrected the instructions. Here is a VS 2015 version:

"%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" amd64
set DIASDK=%VS140COMNTOOLS%..\..\DIA SDK
midl /I "%DIASDK%\include" "%DIASDK%\idl\dia2.idl" /tlb dia2.tlb
tlbimp dia2.tlb

Change VS140 to match whatever version you are trying to use.

This created dia2lib.dll which I added as a reference - right-click References, Add Reference, Browse, find the file. It works and I can now build and run symbolsort.

Pneumococcus answered 9/1, 2017 at 1:0 Comment(0)
M
4

In case somebody has issues with the path, here is what worked for me for VS 2017.

  1. Open x86_x64 Cross Tools Command Prompt (from start/programs/Visual Studio 2017 in Administrator mode)

  2. cd C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional

  3. midl /I "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\DIA SDK\idl";"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\DIA SDK\include" dia2.idl /tlb dia2.tlb

  4. tlbimp dia2.tlb

The Dia2Lib.dll is now in the C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional folder.

Using it in C# code I got unregistered dll exception ! I had to run

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\DIA SDK\bin>regsvr32 msdia140.dll

to get this resolved

Misguidance answered 25/7, 2017 at 19:31 Comment(0)
A
2

I have had success using DIA from C# without having to manually build the type library or import library from source using Visual Studio 2019. If the COM Server(s) are registered, its possible to simply add a COM reference to the csproj, like this:

<ItemGroup>
  <COMReference Include="Dia2Lib.dll">
    <WrapperTool>tlbimp</WrapperTool>
    <VersionMinor>0</VersionMinor>
    <VersionMajor>2</VersionMajor>
    <Guid>106173a0-0173-4e5c-84e7-e915422be997</Guid>
    <Lcid>0</Lcid>
    <Isolated>false</Isolated>
  </COMReference>
</ItemGroup>

This works because the COM server dll actually has the type library embedded in it as a resource.

The reference can alternatively be added using the Visual Studio UI by opening your project, right clicking on Dependencies->Add COM Reference..., then search for "dia". An entry named "dia 2.0 Type Library" should be available if the COM server is registered.

Register the two required COM servers as follows, assuming your architecture is AMD64 and you have Visual Studio 2019 Enterprise installed. The path can be changed to match the your specific installation and architecture. This must be done from an elevated command prompt (right click and run as administrator).

regsvr32 /s "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\DIA SDK\bin\msdia140.dll"
regsvr32 /s "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\DIA SDK\bin\amd64\msdia140.dll"

The DIA SDK must of course be installed, which can be done by running Visual Studio Installer, selecting "Modify" for your installation, then choosing "Desktop development with C++" and installing.

Apparatus answered 17/12, 2020 at 3:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.