Bogey
Bogey

Reputation: 5724

Debugging a C++ DLL loaded into Excel via LoadLibrary()

I'm writing a C++-based DLL which exports a few functions, let's say in a simple case

__declspec(dllexport) int __stdcall Test()
{
  return 123;
}

Then, I load this DLL into a running Excel process via VBA, and invoke its Test function:

Private Declare PtrSafe Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As LongPtr
Private Declare Function Test Lib "MyLibrary.dll" () As Long

Sub executeTest()
  LoadLibrary "Path\To\MyLibrary.dll"
  Dim result As Long: result = Test()
End Sub

This works fine. But I don't seem to be able to Debug the C++ part of it.

I've tried firing up Excel, and before executing any code (i.e. befoer the DLL is loaded into Excel), attaching Visual Studio to the running Excel process. When I set a breakpoint, it's "white" - aka "This breakpoint will not currently be hit. No symbols have been loaded for this document". Makes sense as my DLL hasn't been laoded into Excel yet.

Then, I execute the VBA code, which will load up the DLL and invoke the Test function. But the debugger's status doesn't change, the breakpoint remains in this state; at no point does it seem to realize the library has been loaded / is able to debug it.

Am I doing anything wrong there? Is there a way to debug calls to my DLL in this scenario?

Thanks!

Upvotes: 2

Views: 507

Answers (1)

sandwood
sandwood

Reputation: 2167

You might try to add a __debugbreak directive. If the dll is build in debug, it should open the Windows Just in Time debugger.

__declspec(dllexport) int __stdcall Test()
{
  __debugbreak(); 
  return 123;
}

see https://msdn.microsoft.com/en-us//library/f408b4et.aspx

Upvotes: 1

Related Questions