thiscall calling convention
Don Clugston
dac at nospam.com.au
Tue Oct 24 05:07:45 PDT 2006
Max Samuha wrote:
> On Mon, 23 Oct 2006 09:59:56 -0400, "Jarrett Billingsley"
> <kb3ctd2 at yahoo.com> wrote:
>
>> "Max Samuha" <maxter at i.com.ua> wrote in message
>> news:o14pj2teu31k82a6rnqfc7jbk422lrm05l at 4ax.com...
>>> I have a COM object which uses __thiscall linkage for its methods
>>> (guys who created the object have perfect sense of humor). Would you
>>> advice me of a good way to call those methods from D?
>> To use any COM interface from D, you can just create an interface which
>> derives from IUnknown. D will make the interface COM-compatible.
>>
>>
>> import std.c.windows.com;
>>
>> interface SomeInterface : IUnknown
>> {
>> HRESULT method1();
>> }
>>
>> extern(Windows) export CreateSomeInterface();
>>
>> ...
>>
>> SomeInterface i = CreateSomeInterface();
>> i.method1();
>>
>>
>> Of course, if the library you're using (what are you using anyway?) doesn't
>> have a function which creates an instance of the COM object for you, you'll
>> have to use QueryInterface from a base COM object, but..
>>
>
> Thank you for the reply. It's not that i can't obtain the COM
> interface. I just can't call methods on that interface because the COM
> object implementation uses thiscall (not stdcall or cdecl) calling
> convention. This is an ASIO driver. The interface declaration does not
> specify the calling convention explicitly in their SDK (in C++):
>
> interface IASIO : public IUnknown
> {
> virtual ASIOBool init(void *sysHandle) = 0;
> virtual void getDriverName(char *name) = 0;
> virtual long getDriverVersion() = 0;
> ...
> }
>
> So MSVC++ which is used de facto to compile ASIO drivers for windows
> defaults to thiscall, effectively making the drivers crash when they
> are called by clients compiled in languages that don't support
> thiscall.
thiscall isn't standard in C++. It's an MSVC-only thing (but see below).
> I think, Walter has intended to add extern(C++): "C++ is reserved for
> future use". Will it be added?
If it is, it still won't solve the problem. There's no standard for
extern(C++). Borland, MSVC, GCC, Watcom, DMC, all use different calling
conventions. Failing to standardise the ABI was one of the appallingly
bad C++ mistakes.
HOWEVER... *** IUnknown is treated specially by DMD ***
It is magical, and it makes all of the member functions use the MSVC
'thiscall' calling convention. (There's no other way to specify 'thiscall').
It should just work. Have you actually tried it?
> A wrapper could be used to fix that
> (http://www.martinfay.com/openasio.htm) but someone may have a better
> idea?
More information about the Digitalmars-d-learn
mailing list