• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Build and Debug Excel add-in using XLW Project and Visual Studio 2022 C++

Dear All,

I am creating an Excel add-in using the XLW project on GitHub in Visual Studio 2022 C++.

I have done the following:

Create a solution MyFunction which contains two projects MyFunction and XLW.

In MyFunction, Configuration Properties -> General -> Configuration Type is set to Static Library.

In MyFunction,

my_header.h:
#pragma once

double EchoDouble(double x);

my_source.cpp:
#include "my_header.h"

double EchoDouble(double x)
{
    return x;
}

In XLW, the XLW package is installed via NuGet Package Manager, and
  • Configuration Properties -> General -> Configuration Type is set to Dynamic Library.
  • Configuration Properties -> C/C++ -> Additional Include Directories is set to the folder which contains the solution.
  • Configuration Properties -> Linker -> General -> Additional Library Directiories is set to the folder which contains MyFunction.lib.
  • Configuration Properties -> Linker -> Input -> Additional Dependencies is set to MyFunction.lib.
In XLW,
cppinterface.h:
#ifndef TEST_H
#define TEST_H

#include <xlw/MyContainers.h>
#include <xlw/CellMatrix.h>
#include <xlw/DoubleOrNothing.h>
#include <xlw/ArgList.h> 
    
using namespace xlw;

//<xlw:libraryname=MyTestLibrary


double MyEchoDouble(double x);

#endif

source.cpp:
#include "cppinterface.h"
#include "MyFunction/my_header.h"

#pragma warning (disable : 4996)

double MyEchoDouble(double x)
{
    return EchoDouble(x);
}

MyFunction is built first and then XLW. Load the generated add-in XLW.xll in Excel and MyEchoDouble works.

To debug, I can do Debug -> Attach to Process -> Attach to Native code and select the Excel instance. It works fine.

Alternatively, I do
  • Configuration Properties -> Debugging -> Command is set to the path for EXCEL.EXE
  • Configuration Properties -> Debugging -> Command Arguments is set to $(TargetPath)
  • Set XLW as startup project.
Press F5, I get the error message:

Exception Thrown​

Exception thrown at 0x00007FFE8496B74B in EXCEL.EXE: 0xC0000005: Access violation reading location 0x0000000000000000.

Frame not in module​

The current stack frame was not found in a loaded module. Source cannot be shown for this location.

My questions are
  1. Is the way I set up the projects correct? What I want to achieve is that all functions are implemented in MyFunction project. Then corresponding Excel functions are done in XLW project, and the add-in is generated by the XLW project.
  2. What went wrong with the alternative debugging method?
Really appreciate any advice and suggestion. Thank you all.

FYI, this is a repost of the same question on StackOverflow here.
 
Back
Top