0

I'm trying to convert the functionality of our old code to a DLL.

One of the function wants to export from our old code use a dynamically linked DLL (that I'm gonna call other)

std::wstring otherDLL = L"C:\\PATH\\TO\\OTHER\\DLL"
if (!(libOther = LoadLibraryW(otherDLL.c_str()))){
     return;
}

In our old code, this portion of code works just fine.

So I added in my new DLL the following code:

extern "C++" std::wstring test_load(void){
    std::wstring otherDLL = L"C:\\PATH\\TO\\OTHER\\DLL"
    if (!(libOther = LoadLibraryW(otherDLL.c_str()))){
         return L"Not Loaded";
    }
    return L"Loaded";
}

(this function is not launched during DLLMain). If I try to launch test_load from my DLL in another exe

#include "MyNewDLL.h"
int main()
{
    std::wcout << test_load();
}

I get following error

Unhandled exception at 0x7BDE20f3 (ucrtbased.dll) in Test.exe: 0xC00000005: Access violation reading location 0x00000000

What's strange is that if I try to load the OtherDLL from the main that is not inside MyNewDLL, there is no error and I can use it just fine.

Any Idea where my problem could come from ?

MK Patel
  • 1,354
  • 1
  • 7
  • 18
  • 1
    You made the bad decision of passing / returning an object such as `std::wstring` by value. Memory is allocated when creating `std::wstring`, so which module is responsible for handling the memory allocation and deallocation? – PaulMcKenzie Mar 04 '20 at 13:51
  • 1
    [Possible duplicate](https://stackoverflow.com/questions/22797418/how-do-i-safely-pass-objects-especially-stl-objects-to-and-from-a-dll) – PaulMcKenzie Mar 04 '20 at 13:56
  • Wow, that was not smart indeed. I need still some work as loading still act different when loading from MyNewDLL. However I don't have an exception anymore – ChristineA Mar 04 '20 at 14:16

0 Answers0