3

I have programmed COM functions for many years, and today I noticed that many functions in fact have different signatures in C and C++. For example CoCreateInstance function:

HRESULT CoCreateInstance(
  _In_   REFCLSID rclsid,
  _In_   LPUNKNOWN pUnkOuter,
  _In_   DWORD dwClsContext,
  _In_   REFIID riid,
  _Out_  LPVOID *ppv
);

In C++, the signature expands to

unsigned __int32 CoCreateInstance(const CLSID& clsid, IUnknown*
pUnkOuter, unsigned __int32 dwClsContext, const IID& iid, void* ppv);

While in C, the signature becomes

unsigned __int32 CoCreateInstance(const CLSID* clsid, IUnknown*
pUnkOuter, unsigned __int32 dwClsContext, const IID* iid, void* ppv);

In Ole32.dll there is only one entry for CoCreateInstance, which means that the two declarations point to the same implementation.

Is this Microsoft extension or supported by C++ standard?

pnuts
  • 58,317
  • 11
  • 87
  • 139
cuteCAT
  • 2,251
  • 4
  • 25
  • 30
  • How are you checking that there is only one entry for CoCreateInstance in Ole32.dll? I'm thinking that the C++ one could have its name mangled. – Eric Finn Aug 13 '13 at 13:53
  • 4
    This is intentional, find the macro in the Guiddef.h SDK header file. There is no difference between a reference and a pointer at runtime. – Hans Passant Aug 13 '13 at 14:59
  • I looked at dissassmbly code that call both of version CoCreateInstance in one program, and in both case calling code at equals adresses (I declare second function in unnamed namespace to allow call it from C++ code), so there are the same function without any additional wrappers. – user1837009 Aug 13 '13 at 14:59
  • And because in runtime there aren't any difference between them (at least in this case) I can call CoCreateInstance with declaration, where one of parameter pass by reference and others will pass by pointers (e.g. variant that difference from C and C++ variant from header). – user1837009 Aug 13 '13 at 15:07
  • All above signatures are correct, there just macro definitions which points to the same things. – vasylz Sep 09 '13 at 20:46
  • Possible duplicate of [C++ by-reference argument and C linkage](http://stackoverflow.com/questions/1906000/c-by-reference-argument-and-c-linkage) – vitaut Oct 29 '15 at 02:17

2 Answers2

0

First of all understand that although very similar, C is a different language than C++. Now coming to the CoCreateinstance() function. The only difference I see is that the signature uses xxx& or pass by reference in C++ while it uses xxx* in C.

Theoretically, This is correct. In order to be able to alter value of the parameter passed C uses passing the memory location so if the caller function wants, it can alter the value of parameter. Although C++ can also pass a parameter by passing its memory location, it has a better mechanism for the same purpose. It passes the value by reference. so to me even though they look different, the functions are same.

Simple Fellow
  • 4,315
  • 2
  • 31
  • 44
0

The standard specifies this as implementation-defined, quoting Section 7.5 - Linkage specifications:

  1. Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved.
vitaut
  • 49,672
  • 25
  • 199
  • 336