I have created a simple DLL in C++ which is to be called from VBA, a couple of issues are happening which need addressing:
People who I send the DLL to cannot run it (where I can), I have a feeling this is due to the necessary dependancies being missing from their computer (the error is that the dll path can't be found, not anything to do with the entry point).
If I compile libstdc++ as static, I start getting segmentation issues when I try and create an atomic, strangely, when I leave this statement (-static-libstdc++) I get no segmentation issues.
I would like to make this as standalone as possible eg. my colleague gets sent it, references the DLL and can start using the methods.
Due to issues with my work giving out admin privlages, I cannot compile using the MSVC compiler - I am forced to use G++
Here is my VS Code build task JSON ->
tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "g++.exe",
"args": [
"-g",
"${file}",
"-shared",
"excelparallel.cpp",
"C:\\Windows\\System32\\oleaut32.dll",
"C:\\Windows\\System32\\ole32.dll",
"C:\\Windows\\System32\\oleacc.dll",
"C:\\Windows\\System32\\kernel32.dll",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.dll"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: g++.exe"
}
]
Here is the function which is cauing a segmentation issue, please refer to the new atomic line, this is causing the seg fault.
// Runs a target macro asynchronously
__declspec(dllexport) LONG __stdcall PRunAsync(BSTR sMacroName, BSTR sLocation)
{
CoInitialize(NULL);
nInstanceCounter++;
vecInstances.push_back(EXCELPARALLEL_INSTANCES()); // Add instance to the vector
vecInstances[nInstanceCounter].Attributes.IsFinished = new std::atomic<bool>(std::memory_order_relaxed);
vecInstances[nInstanceCounter].Attributes.IsFinished->store(false, std::memory_order_relaxed);
vecInstances[nInstanceCounter].Attributes.Proc = new std::thread(ThreadProcRunAsync, sMacroName, sLocation, nInstanceCounter);
vecInstances[nInstanceCounter].Attributes.Proc->detach();
CoUninitialize();
return nInstanceCounter; // Return new instance number
}
This code block should be enough for everyone to test - The new line is the one which is throwing the error at runtime.
__declspec(dllexport) void __stdcall Example()
{
std::atomic<bool> *Example;
Example = new std::atomic<bool>(std::memory_order_relaxed);
}
GCC Version is 8.1.0, the issue was only found after connecting gdb to excel, this highlighted the new std::atomic line as cauing the segmentation error (only when compiling libstdc++ as static).