Re: C3395: __declspec(dllexport) and __clrcall incompatible

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Jochen,
"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@xxxxxxxxx> wrote in message
news:uHSH7cv0FHA.1032@xxxxxxxxxxxxxxxxxxxxxxx
> Hi bonk!
>> Hello, I created a MFC extension dll (using VS 2005 Beta 2) that is
>> supposed to export a class that uses .NET internally (See header below)
>> und later shall be used by a plain MFC Project (without the /CLR).
>>
>> As soon as I try to specify __declspec(dllexport) (AFX_EXT_CLASS) I get
>> the following compiler error:
>>
>> Error 2 error C3395: 'CWPFControlProxy::OnWPFButtonClick' :
>> __declspec(dllexport) cannot be applied to a function with the __clrcall
>> calling convention g:\robert_d\visual studio
>> 2005\projects\wpfcontrols\wpfcontrols\WPFControlProxy.h 28
>>
>> Could you explain what this error means and what I can do against it? I
>> don't see a __clrcall definition anywhere in my project ...
>
> If you compile with /clr, then all methods/functions are compiled into
> MSIL, therefor they will always use __clrcall as calling convention.
>
> If you want to have __stdcall or __cdecl then you need to compile into
> native code.
>
> So please add the
>
> #pragma unmanaged
>
> to the region of code, you want to export.

Sorry to correct you here, but your statement is wrong. Managed functions
can indeed have native calling conventions. In these cases special metadata
is generated that the CLR can use to generate unmanaged -> managed thunks.
The calling convention __clrcall exists only to avoid the generation of this
metadata and the managed -> unmanaged thunks in cases where they are not
necessary.

See [1] for the details.

Marcus Heege

[1]
http://www.heege.net/blog/PermaLink,guid,48daf2ff-41c8-4312-9d80-bdfa8c55058d.aspx


.



Relevant Pages

  • Re: x86-64 and calling conventions
    ... and there is no particular relation between the registers and the ... Beyond Just Putting A Big Damper On The Compiler Machinery, ... Win32 is my primary target, with Linux as secondary ... either to break the calling convention or violate threading). ...
    (comp.compilers)
  • Re: debug error
    ... > convention with a function pointer declared with a different calling ... called the 'calling convention' of a function. ... specify with prefixes to your function declarations or compiler settings. ... If you call one function with the compiler believing it was convention X ...
    (comp.lang.c)
  • Re: x86 Stack Confusion
    ... The "C" calling convention is defined ... > by the compiler that compiles the operating system, ... > integer and the floating point register file. ... or "cdecl", ...
    (comp.lang.c)
  • Re: gfortran iso c bindings - capabilities
    ... problems with the STDCALL calling convention that gcc hasn't worked out ... some progress in that area (perhaps a wrongheaded approach, ... for gfortran to interoperate in a 32-bit Windows environment. ... is a good compiler and all that, but it doesn't seem to me to be ...
    (comp.lang.fortran)