From: Gib Bogle on
I am able to invoke a subroutine in my DLL successfully, and all seems OK until
the execution is complete. On return from the DLL subroutine my C++/Qt
application crashes silently.

My subroutine expects an integer and four character arrays:

subroutine execute(ncpu,infile,outfile,resfile,runfile)
!DEC$ ATTRIBUTES DLLEXPORT :: EXECUTE
!DEC$ ATTRIBUTES STDCALL, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE" :: execute
integer :: ncpu
character*(*) :: infile,outfile,resfile,runfile

I am calling the DLL from within a thread, but I don't think that is
significant. Here is the C++:

void ExecThread::run()
{
....
QLibrary myLib(dll_path);

typedef int (*MyPrototype)(int *, char *, int, char *, int, char *, int, char
*, int);
MyPrototype execute = (MyPrototype) myLib.resolve("EXECUTE");
if (execute) {
execute(...)
}
myLib.unload();
}

If I comment out the call to execute() the program doesn't crash. I suspect the
problem is stack related. This same invocation works fine in Python (using
windll.LoadLibrary()), and apparently almost works here, since the subroutine
executes correctly. I know it's a long shot asking for help on this, since I'm
using not only C++ but also Qt.
From: Gib Bogle on
Gib Bogle wrote:

> !DEC$ ATTRIBUTES STDCALL, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE"

Replacing STDCALL by C makes this work. C++ apparently has a different calling
convention than Python, which needs STDCALL.
From: Arjen Markus on
On 29 mrt, 10:33, Gib Bogle <g.bo...(a)auckland.no.spam.ac.nz> wrote:
> Gib Bogle wrote:
> > !DEC$ ATTRIBUTES STDCALL, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE"
>
> Replacing STDCALL by C makes this work.  C++ apparently has a different calling
> convention than Python, which needs STDCALL.

This really depends on the compiler used.

STDCALL ordinarily does more than just change the calling
convention - it also adds a suffix "@nn" to the link name of
the routines (where nn is the number of bytes in the interface).
Via aliases that may become invisible.

Regards,

Arjen
From: Jugoslav Dujic on
Gib Bogle wrote:
> Gib Bogle wrote:
>
>> !DEC$ ATTRIBUTES STDCALL, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE"
>
> Replacing STDCALL by C makes this work. C++ apparently has a different
> calling convention than Python, which needs STDCALL.

I suppose you've just found the reason why that calling convention is
called "C" :-).

--
Jugoslav
www.xeffort.com
Please reply to the newsgroup.
You can find my real e-mail on my home page above.
From: Craig Powers on
Gib Bogle wrote:
> Gib Bogle wrote:
>
>> !DEC$ ATTRIBUTES STDCALL, REFERENCE, MIXED_STR_LEN_ARG, ALIAS:"EXECUTE"
>
> Replacing STDCALL by C makes this work. C++ apparently has a different
> calling convention than Python, which needs STDCALL.

Alternatively, slap a "__stdcall" on your function pointer type---the
default, as you discovered, is __cdecl. It may also need to be 'extern
"C"'; I don't recall offhand whether that has any impact beyond name
mangling to where it would be needed here.