473,657 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C3395: __declspec(dlle xport) 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(dlle xport) (AFX_EXT_CLASS) I get
the following compiler error:

Error 2 error C3395: 'CWPFControlPro xy::OnWPFButton Click' :
__declspec(dlle xport) cannot be applied to a function with the __clrcall
calling convention g:\robert_d\vis ual studio
2005\projects\w pfcontrols\wpfc ontrols\WPFCont rolProxy.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

// CWPFControlProx y
#include <msclr\event. h>
#include <msclr\auto_gcr oot.h>
using namespace System;
using namespace System::Windows ;
using namespace System::Windows ::Controls;
using namespace System::Windows ::Media;

class AFX_EXT_CLASS CWPFControlProx y : public CStatic
{
DECLARE_DYNAMIC (CWPFControlPro xy)
public:
CWPFControlProx y();
virtual ~CWPFControlPro xy();
CRect m_borders;
private:
HWND m_WPFHwnd;
CString m_WPFText;
Visual^ CreateRootWPFVi sual();
msclr::auto_gcr oot<TextBox^> m_wpfTextBox;
void OnWPFButtonClic k (Object^ sender, RoutedEventArgs ^ eargs);
public:
BEGIN_DELEGATE_ MAP(CWPFControl Proxy)
EVENT_DELEGATE_ ENTRY(OnWPFButt onClick,Object^ , RoutedEventArgs ^)
END_DELEGATE_MA P()
void SetText (LPCTSTR text);
protected:
DECLARE_MESSAGE _MAP()
public:
afx_msg int OnCreate(LPCREA TESTRUCT lpCreateStruct) ;
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnMouseMove(UIN T nFlags, CPoint point);
afx_msg LRESULT OnSetText( WPARAM, LPARAM );
protected:
virtual BOOL PreCreateWindow (CREATESTRUCT& cs);
};

Nov 17 '05 #1
8 12453
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(dlle xport) (AFX_EXT_CLASS) I get
the following compiler error:

Error 2 error C3395: 'CWPFControlPro xy::OnWPFButton Click' :
__declspec(dlle xport) cannot be applied to a function with the __clrcall
calling convention g:\robert_d\vis ual studio
2005\projects\w pfcontrols\wpfc ontrols\WPFCont rolProxy.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******** ******@TK2MSFTN GP12.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(dlle xport) (AFX_EXT_CLASS) I get
the following compiler error:

Error 2 error C3395: 'CWPFControlPro xy::OnWPFButton Click' :
__declspec(dlle xport) cannot be applied to a function with the __clrcall
calling convention g:\robert_d\vis ual studio
2005\projects\w pfcontrols\wpfc ontrols\WPFCont rolProxy.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******** ******@TK2MSFTN GP15.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(dlle xport) (AFX_EXT_CLASS) I get
the following compiler error:

Error 2 error C3395: 'CWPFControlPro xy::OnWPFButton Click' :
__declspec(dlle xport) cannot be applied to a function with the __clrcall
calling convention g:\robert_d\vis ual studio
2005\projects\w pfcontrols\wpfc ontrols\WPFCont rolProxy.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

// CWPFControlProx y
#include <msclr\event. h>
#include <msclr\auto_gcr oot.h>
using namespace System;
using namespace System::Windows ;
using namespace System::Windows ::Controls;
using namespace System::Windows ::Media;

class AFX_EXT_CLASS CWPFControlProx y : public CStatic
{
DECLARE_DYNAMIC (CWPFControlPro xy)
public:
CWPFControlProx y();
virtual ~CWPFControlPro xy();
CRect m_borders;
private:
HWND m_WPFHwnd;
CString m_WPFText;
Visual^ CreateRootWPFVi sual();
msclr::auto_gcr oot<TextBox^> m_wpfTextBox;
void OnWPFButtonClic k (Object^ sender, RoutedEventArgs ^ eargs);
public:
BEGIN_DELEGATE_ MAP(CWPFControl Proxy)
EVENT_DELEGATE_ ENTRY(OnWPFButt onClick,Object^ , RoutedEventArgs ^)
END_DELEGATE_MA P()
void SetText (LPCTSTR text);
protected:
DECLARE_MESSAGE _MAP()
public:
afx_msg int OnCreate(LPCREA TESTRUCT lpCreateStruct) ;
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnMouseMove(UIN T 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(dlle xport)
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
2601
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 it as follows: class __declspec(dllexport) Terrain { public: void foo(){} };
1
1303
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 CLASSDECLARE( c ) class __declspec(dllexport) c #else #define CLASSDECLARE( c ) class c //note the absence of dllimport #endif #endif
5
2048
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 some investigating, I narrowed this down to a very odd behavior (bug?) of the VC++.NET 2003 compiler: If a class that is declared as __declspec(dllimport) derives from a template, that template's methods are never inlined, even if declared with...
5
2218
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 could not be "read"". Can any one explain what I am doing wrong? This is my Code.
7
1910
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 comment myself! Following the conversion I have numerous errors, which following some digging around turns out to be because _export is obsolete, and
3
12436
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 : __declspec(dllexport) & __declspec(dllimport) I do export non-static and static members from my c++ code
2
5120
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 function. */ __declspec(dllexport) int fnDLLSAMPLE(void) {
2
1753
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 &ms) However (perhaps unsuprisngly), my function is not expoted in the DLL/lib - and when I try to build a project that uses the Dll, I get linkage error: demo error LNK2019: unresolved external symbol "class
1
6281
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 (dllexport). I get the following compiler errors: Error 224 error C3389: __declspec(dllexport) cannot be used with /clr:pure or /clr:safe Error 225 error C4394: 'ErrorHandler::instance' : per-appdomain symbol should not be marked with...
0
8397
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6167
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2731
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.