473,378 Members | 1,415 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,378 software developers and data experts.

How to dllimport a dllexported class

hi,

I have exported a class which i have written in win32 static lib app.
Now i have written a wrapper class for the same. But when i am trying
to compile it is giving me some linker errors. Here is the class
information

//This is MyClass.h file

#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )

class DllExport CMyClass
{
public:
CMyClass();
int GetSum(int a, int b);
};

//This is MyClass.cpp file

#include "MyClass.h"

CMyClass::CMyClass()
{
}
int CMyClass::GetSum(int a, int b)
{
return a+b;
}

After this i have created a .net class library in which i want to use
this class

// WrapperClasseApp.h
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
#include "D:\New Projects\MyStaticLib\MyClass.h"

namespace WrapperClasseApp
{
public __gc class Class1
{
// TODO: Add your methods for this class here.
private:
CMyClass *m_pClass;

public:
Class1::Class1(void)
{
m_pClass = new CMyClass();
}
Class1::~Class1(void)
{
delete m_pClass;
}

int WrapperForGetSum(System::Int32 a, System::Int32 b)
{
int iResult = m_pClass->GetSum(a,b);
return iResult ;
}
};
}

When i am trying to complie the above app, there are two linker errors

WrapperClasseApp.obj : error LNK2001: unresolved external symbol "void
* __cdecl operator new(unsigned int)" (??2@$$FYAPAXI@Z)
WrapperClasseApp.obj : error LNK2001: unresolved external symbol "void
__cdecl operator delete(void *)" (??3@$$FYAXPAX@Z)

can any one please help me how i can use the exported class and its
members
Aug 5 '08 #1
6 2603
On Mon, 4 Aug 2008 23:11:17 -0700 (PDT), vi*****************@gmail.com
wrote:
>hi,

I have exported a class which i have written in win32 static lib app.
There's no concept of exporting a class from a static library. Also,
"static lib app" is sort of a contradiction; it's either a library or
application, not both.
>Now i have written a wrapper class for the same. But when i am trying
to compile it is giving me some linker errors. Here is the class
information

//This is MyClass.h file

#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
class DllExport CMyClass
This won't work. You need to do something like the following, replacing X
with a suitably unique identifier, which can often just be the name of your
DLL:

#ifdef COMPILING_X_DLL
#define X_EXPORT __declspec(dllexport)
#else
#define X_EXPORT __declspec(dllimport)
#endif

Then you /D COMPILING_X_DLL in the DLL's project, and only there. You would
then write:

class X_EXPORT MyClass...

That said, it doesn't make any sense to do any of this in a static library.
You need to eliminate your existing DllExport/DllImport usage.
>{
public:
CMyClass();
int GetSum(int a, int b);
};

//This is MyClass.cpp file

#include "MyClass.h"

CMyClass::CMyClass()
{
}
int CMyClass::GetSum(int a, int b)
{
return a+b;
}

After this i have created a .net class library in which i want to use
this class

// WrapperClasseApp.h
#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
#include "D:\New Projects\MyStaticLib\MyClass.h"

namespace WrapperClasseApp
{
public __gc class Class1
{
// TODO: Add your methods for this class here.
private:
CMyClass *m_pClass;

public:
Class1::Class1(void)
{
m_pClass = new CMyClass();
}
Class1::~Class1(void)
{
delete m_pClass;
}
The "Class1::" bit isn't needed.
> int WrapperForGetSum(System::Int32 a, System::Int32 b)
{
int iResult = m_pClass->GetSum(a,b);
return iResult ;
}
};
}

When i am trying to complie the above app, there are two linker errors
I thought it was a library. :)
>WrapperClasseApp.obj : error LNK2001: unresolved external symbol "void
* __cdecl operator new(unsigned int)" (??2@$$FYAPAXI@Z)
WrapperClasseApp.obj : error LNK2001: unresolved external symbol "void
__cdecl operator delete(void *)" (??3@$$FYAXPAX@Z)

can any one please help me how i can use the exported class and its
members
You do need to link to the CRT, and all files in this library should have
the same CRT setting, e.g. /MD.

--
Doug Harrison
Visual C++ MVP
Aug 5 '08 #2
>can any one please help me how i can use the exported class and its
>members

You do need to link to the CRT, and all files in this library should
have the same CRT setting, e.g. /MD.
And many other compiler and runtime library options need to match, including
packing, alignment, use of checked iterators, the list goes on and on.

Please don't use dllexport on classes. Expose functions which take the
instance as a argument, like the Win32 API does, or use a factory function
to return a pointer to a pure interface class, like COM. These solutions
are well-known, tested, portable, and resilient to changes.
Aug 7 '08 #3
On Thu, 7 Aug 2008 09:51:31 -0500, "Ben Voigt [C++ MVP]"
<rb*@nospam.nospamwrote:
>>can any one please help me how i can use the exported class and its
members

You do need to link to the CRT, and all files in this library should
have the same CRT setting, e.g. /MD.

And many other compiler and runtime library options need to match, including
packing, alignment, use of checked iterators, the list goes on and on.
Right. Using DLLs is no different that using static libraries in that
respect.
>Please don't use dllexport on classes. Expose functions which take the
instance as a argument, like the Win32 API does, or use a factory function
to return a pointer to a pure interface class, like COM. These solutions
are well-known, tested, portable, and resilient to changes.
Alternatively, understand that when you share C++ classes across module
boundaries, you need to treat it like static linking WRT having identical
project settings for the things you mentioned. Given the goal of sharing
C++ objects across module boundaries, how could it be any different?

Anyone interested in a discussion of these points of view should refer to
these threads, as I'm not going to rehash it any further here:

http://groups.google.com/group/micro...n?d22e7c902105
http://groups.google.com/group/micro...18f517c973340f

P.S. Anyone who changes the default packing really is asking for trouble.
It's very unlikely to work with third party code, as few are as diligent as
MS at setting and restoring the packing. And MS gets (or use to get) it
wrong sometimes, as was the case in <winsock2.h>, TEXTMETRICW, and at least
one other I've forgotten. In contrast, exporting whole classes works
flawlessly for probably thousands of programs that use the CRT DLLs, MFC
extension DLLs, etc etc etc, and observe the similarity to static linking
and take care with template static data.

--
Doug Harrison
Visual C++ MVP
Aug 7 '08 #4
hi Ben,

" Expose functions which take the
instance as a argument, like the Win32 API does, or use a factory
function
to return a pointer to a pure interface class, like COM. "

Can you just expand the above......I couldnt understand what your
intention was. How can I return a pointer to a interface?
If you don't mind can you please take out an example and let me know
how to achieve that. The reply what I got was very helpful to me since
I am entirely new to this.

very very very thanks in advance
Aug 14 '08 #5
vi*****************@gmail.com wrote:
hi Ben,

" Expose functions which take the
instance as a argument, like the Win32 API does, or use a factory
function
to return a pointer to a pure interface class, like COM. "

Can you just expand the above......I couldnt understand what your
intention was. How can I return a pointer to a interface?
If you don't mind can you please take out an example and let me know
how to achieve that. The reply what I got was very helpful to me since
I am entirely new to this.
vivekanandaprasanth:

The DLL has a concrete class that derives from a pure interface. The exported
factory function returns a pointer to an instance of the concrete class, which
is also a valid pointer to the interface. The client application only knows
about the interface.

It is also a good idea to export a function (anti-factory) that destroys the
instance; this avoids problems with different heaps in the two modules.

This pattern allows mixed compiler version in the DLL and client, because all
compiler versions agree on the layout of a pure interface class.

--
David Wilkinson
Visual C++ MVP
Aug 14 '08 #6
vi*****************@gmail.com wrote:
hi Ben,

" Expose functions which take the
instance as a argument, like the Win32 API does, or use a factory
function
to return a pointer to a pure interface class, like COM. "

Can you just expand the above......I couldnt understand what your
intention was. How can I return a pointer to a interface?
If you don't mind can you please take out an example and let me know
how to achieve that. The reply what I got was very helpful to me since
I am entirely new to this.
If you search the msdn site for COM factory example, you might come up with
something like this:
http://blogs.msdn.com/ericlippert/ar...fault.aspx?p=2
http://blogs.msdn.com/ericlippert/pages/108025.aspx

Expecially look at the Create, AddRef, and Release functions, along with the
constructor and destructor.
>
very very very thanks in advance

Aug 25 '08 #7

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

Similar topics

0
by: .NET developer | last post by:
Hi, I am trying to compile my VC6 application on VC++7. It give following linker error. __declspec(dllimport) public: class ATL::CStringT<unsigned short,class StrTraitMFC_DLL<unsigned...
15
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the...
3
by: Mark Jerde | last post by:
I'm sill learning VS .NET 2003, not an expert yet. I'm calling an unmanaged C++ DLL from C# using . When the whole project is done I will be calling a total of 5 C++ DLLs from C#. All the DLLs...
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...
2
by: .NET developer | last post by:
Hi , While upgrading my application from vc6 to vc7 I got followin linking errors : __declspec(dllimport) public: class ATL::CStringT<unsigned short,class StrTraitMFC_DLL<unsigned short,class...
5
by: Frederiek | last post by:
Hi, I'm using MS Visual C++ 2005 (MSVC8.0). I am experiencing some kind of linker dilemma. While building a program I am -in a way- forced to use following construct. // aaa_ssf.h - some...
2
by: nospam | last post by:
Hello - I am getting a compile error "Expected class, delegate, enum". I am trying to create a Class Library in C# which wraps around an unmanaged DLL that manages a MIDI interface. In my...
9
by: jjones7947 | last post by:
Am doing a JNI wrap on a C++ API, am using VC7 and Eclipse. In preparation, I created a C++ executable which mimicked the flow of the JNI, i.e. a driver file which called methods in file with methods...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.