How do I debug DLL code in Visual Studio after using loadlibrary in MATLAB?

68 次查看(过去 30 天)
I have a C++ code that I have compiled in Visual Studio 2022 as a DLL. I use MATLAB to call the DLL using the function loadlibrary. The code executes no problem. However, I want to enter the Visual Studio debugger to step through the code once MATLAB calls the DLL. Years ago in a previous version of Visual Studio and MATLAB, a friend showed me how to connect Visual Studio to MATLAB. In Visual Studio, I would select "Build->Attach to process" and pick the MATLAB process that would appear in the window. Then, I could add a breakpoint in the C code, and when MATLAB called the DLL, the Visual Studio debugger would stop code execution at the break point and let me step through the code. I'm unable to do that now for some reason. When I attach MATLAB to Visual Studio, the breakpoint turns to an empty red outline circle with a caution triangle on it. When I hover over it, the tooltip says "The breakpoint will not currently be hit. No symbols have been loaded for this document." The Symbol Status column of the Debug->Windows->Modules dialog says "Cannot find or open the PDB file." I'm unsure what to do here. I'm sure I'm just forgetting a step that I was shown years back. Any help would be appreciated. Note Visual Studio did create a pdb file along with the DLL and both are in the same directory. Also, I compiled in debug mode, not release mode.
  2 个评论
Umar
Umar 2024-6-22
Hi Joel, To debug a MATLAB DLL called from Visual Studio, ensure that the PDB file is correctly loaded. Check that the PDB file is in the same directory as the DLL and that Visual Studio can access it. You may need to set the symbol file location in Visual Studio to point to the directory containing the PDB file. Additionally, make sure that the DLL and MATLAB processes are running in the same architecture (32-bit or 64-bit) to enable debugging. Finally, rebuild the solution in Visual Studio to ensure that the latest symbols are generated and loaded correctly. By following these steps, you should be able to debug your MATLAB DLL code effectively in Visual Studio. Hope this will help resolve your issue.
Joel Williams
Joel Williams 2024-6-23,19:14
Hi Umar, would you be willing to provide more specific details on the steps you describe by chance? I'm not a C programmer; I use Visual Studio to compile this one code every couple of years when I receive a source update from a vendor. I'm otherwise very unfamiliar with Visual Studio. I just have an occasion with the latest version to make sure what's being passed into a subfunction in the C code is correct, so I want to enable debugging. I can confirm that the PDB file is in the same directory as the DLL MATLAB is calling and has the same name (just a different extension - .pdb not .dll). How can I confirm according to your instruction that Visual Studio can access it? Can you also explain how to change the symbol file location? Do I have the option to add a path to a series of existing paths, or is only one folder allowed for the symbol directory? I do believe I am running in the correct architecture. I am attempting to debug the DLL compiled in debug mode x64, and I'm calling it from a 64-bit version of MATLAB. I have also rebuilt the solution several times over the last couple of days trying different things to make it work, so I don't think that is the issue.

请先登录,再进行评论。

回答(1 个)

aditi bagora
aditi bagora 2024-6-25,9:16
Hi Joel,
I understand that you are trying to debug a DLL using Visual Studio after loading and calling it from MATLAB. According to the issue, probably the Visual Studio is unable to find your pdb file.
It seems that Visual Studio is unable to load the pdb file. The issue is not related to MATLAB. However, in order to resolve the issue please refer to the following steps:
In the Modules Window in the Debugger:
  1. Go to Debug->Windows->Modules in the menu bar.
  2. Search for your dll file in the list. In the Symbol Status column it should read "Cannot find or open the PDB file".
  3. Right click the dll and choose Load Symbols from the context menu.
  4. Point it to the correct pdb file.
  5. The Symbol Status should now change to "Symbols Loaded".
The Symbol Status change shows that the symbols from pdb file are loaded and you can now debug the DLL using breakpoints
Hope this helps!
  1 个评论
Joel Williams
Joel Williams 2024-6-25,13:02
Hi Aditi,
I loaded the DLL using loadlibrary and then attached Visual Studio to MATLAB. I then opened the Modules window in Visual Studio and sorted through all the DLLs in the list. My DLL doesn't show up in the list. Any idea why not?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by