2

I have a DLL containing multiple functions that can fastly perform arithmetic operations on extremely large integers. My test program runs smoothly in my Visual Studio 2019, as follows.

int main()
 {
    HINSTANCE myDDL = LoadLibrary(L".\\BigIntDLL.dll"); 
    typedef string (*func)(string a, string b); 
    func expBigInt = (func)GetProcAddress(myDDL, "expBigInt"); 
    string y= expBigInt("2", "10000");//it can calculate 2^10000 and return it as a string
    cout << y;
}

So, I moved the code directly into my Qt project as a part in widget.cpp, and also placed the BigIntDLL.dll and .lib in the same directory of the project. The compilation was successful, but when debugging my interface, the program broke with a Segmentation fault error due to a call to the expBigInt function.

void Widget::on_expButton_clicked()
{
    getTextEditNum();
    Output=expBigInt(Input1,Input2);//crashed here
    writeResult(Output);
}

I am not really sure where the real problem is, but I now suspect that I have not successfully called the functions in this DLL, causing some memory issues.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • 2
    If it crashes you have to use a debugger to see where it exactly crashes. Your code is not compilable nor full functional - where within Widget class do you resolve the function? – chehrlic Dec 23 '22 at 17:19
  • 1
    The `func` type alias is missing a [calling convention](https://learn.microsoft.com/en-us/cpp/cpp/calling-conventions) specifier. Caller and callee need to agree, and there's nothing in the code that ensures this. – IInspectable Dec 23 '22 at 17:51
  • 1
    Besides, passing a `std::string` across a module boundary is only going to work for a minuscule subset of configurations. Both client and module need to have been compiled using the same compiler, same version, exact same settings, same standard library implementation. Not something you can reasonably expect to control over time. Use a type with a well-defined binary layout and ownership semantics instead (e.g. an [`HSTRING`](https://learn.microsoft.com/en-us/windows/win32/winrt/hstring)). – IInspectable Dec 23 '22 at 17:57

0 Answers0