473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C3395: __declspec(dllexport) and __clrcall incompatible

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\WPFControlPr oxy.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 ...

---- WPFControlProxy.h ------------------

#pragma once

// CWPFControlProxy
#include <msclr\event.h>
#include <msclr\auto_gcroot.h>
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Media;

class AFX_EXT_CLASS CWPFControlProxy : public CStatic
{
DECLARE_DYNAMIC(CWPFControlProxy)
public:
CWPFControlProxy();
virtual ~CWPFControlProxy();
CRect m_borders;
private:
HWND m_WPFHwnd;
CString m_WPFText;
Visual^ CreateRootWPFVisual();
msclr::auto_gcroot<TextBox^> m_wpfTextBox;
void OnWPFButtonClick (Object^ sender, RoutedEventArgs^ eargs);
public:
BEGIN_DELEGATE_MAP(CWPFControlProxy)
EVENT_DELEGATE_ENTRY(OnWPFButtonClick,Object^, RoutedEventArgs^)
END_DELEGATE_MAP()
void SetText (LPCTSTR text);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnSetText( WPARAM, LPARAM );
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
};

Nov 17 '05 #1
8 12407
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\WPFControlPr oxy.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.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
Jochen Kalmbach [MVP] wrote:
So please add the

#pragma unmanaged

to the region of code, you want to export.

Does that mean if I want to export a class it is impossible for that
class to have methods that return managed types or take managed types as
parameters ? Even if the method is private? What about (private) member
types, can they be managed ?
Nov 17 '05 #3
Hi bonk!
#pragma unmanaged

to the region of code, you want to export.

Does that mean if I want to export a class it is impossible for that
class to have methods that return managed types or take managed types as
parameters ? Even if the method is private? What about (private) member
types, can they be managed ?


If your class will be used by unamanged apps, then you cannot export
managed methods (even if the method is private, because the whole
v-table will be different)!

If you want to export managed classes, then you simply need to make it
public (no need of exporting it).

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #4
Jochen Kalmbach [MVP] schrieb:
Hi bonk!
#pragma unmanaged

to the region of code, you want to export.

Does that mean if I want to export a class it is impossible for that
class to have methods that return managed types or take managed types
as parameters ? Even if the method is private? What about (private)
member types, can they be managed ?

If your class will be used by unamanged apps, then you cannot export
managed methods (even if the method is private, because the whole
v-table will be different)!

If you want to export managed classes, then you simply need to make it
public (no need of exporting it).

Is it ok to have managed types as members of a class I wanto dllexport?
And are there any common pitfalls I should watch out for? I am trying to
create a dll that internally works with managed types but exports on
unmanaged "Interfaces". So that a unmanaged (one without the /clr) MFC
project can use that dll.
Nov 17 '05 #5
Hi bonk!
If your class will be used by unamanged apps, then you cannot export
managed methods (even if the method is private, because the whole
v-table will be different)!
Is it ok to have managed types as members of a class I wanto dllexport?


You can´t have them directly; only via gcroot-templates... (but then it
is possible).
And are there any common pitfalls I should watch out for? I am trying to
create a dll that internally works with managed types but exports on
unmanaged "Interfaces". So that a unmanaged (one without the /clr) MFC
project can use that dll.


I don´t know if this will work out of the box... (AFAIK: No, but I have
never tested it...).

You need some kind of CLR_Hosting in the main app...
See: CLR Hosting
http://www.gotdotnet.com/team/clr/wh...r_hosting.aspx
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #6
Hi Jochen,
"Jochen Kalmbach [MVP]" <no********************@holzma.de> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
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\WPFControlPr oxy.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,...a8c55058d.aspx
Nov 17 '05 #7
Hi Bonk,

"bonk" <sc******************@gmx.de> wrote in message
news:Oy**************@TK2MSFTNGP15.phx.gbl...
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\WPFControlPr oxy.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 ...

---- WPFControlProxy.h ------------------

#pragma once

// CWPFControlProxy
#include <msclr\event.h>
#include <msclr\auto_gcroot.h>
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Media;

class AFX_EXT_CLASS CWPFControlProxy : public CStatic
{
DECLARE_DYNAMIC(CWPFControlProxy)
public:
CWPFControlProxy();
virtual ~CWPFControlProxy();
CRect m_borders;
private:
HWND m_WPFHwnd;
CString m_WPFText;
Visual^ CreateRootWPFVisual();
msclr::auto_gcroot<TextBox^> m_wpfTextBox;
void OnWPFButtonClick (Object^ sender, RoutedEventArgs^ eargs);
public:
BEGIN_DELEGATE_MAP(CWPFControlProxy)
EVENT_DELEGATE_ENTRY(OnWPFButtonClick,Object^, RoutedEventArgs^)
END_DELEGATE_MAP()
void SetText (LPCTSTR text);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnSetText( WPARAM, LPARAM );
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
};


Here is what went wrong with your code:

1) You export a native class via __declspec(dllexport)
2) This exports the functions of the native class
3) Although managed functions, functions have a native calling convention -
these functions can easily be exported
4) Some managed functions have the calling convention __clrcall (not because
you explicitly wrote that, but because functions with managed types in their
signature can only have this calling convention.
5) __clrcall calling conventions can not be exported by DLLs - trying to
export them causes the problems you see here.

__clrcall tells the compiler that it should not create unmanaged->managed
thunks, but to export managed functions, unmanaged->managed thunks are
needed.
Nov 17 '05 #8
Hi Marcus!
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.


Yes, you are right... sorry for the confusion...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gawel | last post by:
Hajo, I have two dlls, both of them are compiled with /clr switch. In first dll project I have managed and unmanaged classes. One of the unmanaged I would like to use outside therefore I marked...
1
by: Geoffrey | last post by:
We write lots of cross platform code at my company. To facilitate this, class declarations are handled like this (simplification): #if defined(_WIN32) #if defined(BUILDLIB) #define...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
5
by: Jason W | last post by:
I have a C# class that I wan't to be able to use in VB6 and VBA applications. To do this I was trying to use a mixed managed VC++ dll and export a function. Doing this I get an error "The memory...
7
by: Martin Pritchard | last post by:
Hi, Sorry for my ignorance, but I'm a bit new to C++. I've been handed over a C++ app written in VS2002 which I have to convert to VS2005. Apparently it's been written in a C style, but cannot...
3
by: majestik666 | last post by:
Hi, i'm bulding a multi platform app under windows/linux/osx an i have a bit of trouble exporting c++ symbols from a dynamic library... Under windows, i compile a dll exporting symbols using :...
2
by: Luis | last post by:
Hello... How can I do an exported function in Visual Basic.Net? In Visual C++ the source is the next... but I need the same in VB.Net Thank you /* This is an example of an exported...
2
by: Bit Byte | last post by:
I have written a Dll which contains a class. I overload the "<<" opertor in global functions to perform Ostream operations - a follows: ostream &operator << (ostream &os, const struct _mystruct_t...
1
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I want to give multiple native classes (in different mixed mode dlls) access to a managed output window (for error messages). Therefore I wrote a native singleton with __declspec...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.