PDA

View Full Version : to use a dll (.NET)



mickey
9th October 2006, 15:38
hi,
I write a class File (file.h and file.cpp) and compile it as file.dll.
TheN I'd like use it in other project myApp; so I add in myApp:


#include "file.h"
using namespace std;
int main (int argc, char** argv) {
File f;
f.setFileName("myfile.txt");
if (!f.open_file(0)) {
cerr << "unable to open that file\n";
return -1;
}
. ..........................................
return 0;

and I put file.dll in myApp directory;
the errorors are these:


file error LNK2019: unresolved external symbol "public: __thiscall File::~File(void)" (??1File@@QAE@XZ) referenced in function _main
file error LNK2019: unresolved external symbol "public: __thiscall File::File(void)" (??0File@@QAE@XZ) referenced in function _main
file error LNK2019: unresolved external symbol "public: class std::basic_fstream<char,struct std::char_traits<char> > & __thiscall File::open_file(int)" (?open_file@File@@QAEAAV?$basic_fstream@DU?$char_t raits@D@std@@@std@@H@Z) referenced in function _main
file fatal error LNK1120: 3 unresolved externals

I think this is simple, but this is my first time...anyone could suggest me? thanks

munna
10th October 2006, 08:49
With the .dll file you would have got .lib file also. Add the path to this lib file in the linker options of myApp.

project->properties->linker->Additional Library directories.

mickey
10th October 2006, 20:11
sorry I haven't .lib file; probabily I'm doing a wrong crating .dll; I did: new project-> win32consoleProject then I cjecked dll and 'empty project' in application settings; then I add file.h and file.cpp to the project; compiling is successful...but it create .dll .obj....the usual files of a .net application (eccept .exe)....How to costruct a .dll? What am I doing wrong? thanks

mickey
13th October 2006, 11:13
Hi, could anyone suggest me a link to read hoe create a .dll and use it step by step with .net2003 (I found for VC6 but I have a crisis...) thanks

mickey
17th October 2006, 23:22
hi, I'm trying to buil a dll from a class and its implementation....I've this problem (I already find on google but doens't help me...). Here below you can see the linking errors; I need to include iostream to declare string file_name (and for other things...) Any suggets? Thanks...


// mgfile.h
#pragma once
#include <use_ansi.h>
#include <iostream> // error LNK2020: unresolved token (0A000006) _CxxThrowException


//#include <fstream> // error LNK2020: unresolved token(0A000006) _CxxThrowException

//using namespace std; //it seems necessary includes <iostream>


using namespace System;

namespace mgfile {
public __gc class File {
private:
//string file_name;
//std::fstream file;
public:
File();
~File();
//inline void setFileName (std::string s) {file_name = s;}
//inline std::string getFileName () {return file_name;}
};
}

brcain
20th October 2006, 17:15
Make sure you are exporting the functions you want public. If there are non exported, then Visual Studio (or MinGW, etc.) won't create the associated .lib.

brcain
20th October 2006, 17:38
Here's a portable, common way to handle WIN32 exports. It will generate .lib on Windows using Visual Studio or other compiler (e.g. MinGW).

In the project that creates the library, define the preprocessor directive SOMECLASS_LIBRARY ... resulting in usage of the dllexport rule.

In the project that includes the library, it will automatically use the dllimport rule.

In header file someClassExport.h

#ifndef _SOMECLASS_EXPORT_
#define _SOMECLASS_EXPORT_

#if defined(WIN32) || defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__) || defined( __BCPLUSPLUS__) || defined( __MWERKS__)
#ifdef SOMECLASS_LIBRARY
#define SOMECLASS_EXPORT __declspec(dllexport)
#else
#define SOMECLASS_EXPORT __declspec(dllimport)
#endif
#else
#define SOMECLASS_EXPORT
#endif

#endif // _SOMECLASS_EXPORT_


In header file someClass.h

#ifndef _SOMECLASS_
#define _SOMECLASS_

#include "someClassExport.h"

// If wanting to export whole class ...
class SOMECLASS_EXPORT cSomeClass
{
public:
int foo(void);
}

// Or, if wanting to export only specified methods of class ...

class cSomeClass
{
public:
SOMECLASS_EXPORT int foo(void);
};

#endif // _SOMECLASS_

stevey
27th October 2006, 08:52
You don't need the .lib
You do need to specify an additional include path to where the header file of your library is located, and #include that file.

The trick with Visual Studio .NET 2k3 and 2k5 is to add a 'Reference' to the dll.
Go to the 'Solution Explorer' and right click the client project (the one that needs access to the dll), then select 'Add Reference'.
A dialog will be shown listing all the .NET, .COM and Project references.

The dialog allows you to select the dll you need.
If you have a solution open and you dll and client project within that solution, then click on the 'Projects' tab. Your dll project will be listed here. Select it from the list and click the 'Select' button (to the right of the tab control), then click 'Ok' (bottom of the dialog).

If you are referring to a dll that isn't from a project in your solution, then you can 'Browse...' to the dll explicitly.

Each project in the solution explorer has a 'References' item in the TreeView.
Your new dll reference will be listed there. Now Visual Studio knows where to find the body of the classes you're calling and you shouldn't get the 'unresolved synbol' errors.


I hope this helps,

Steve York

brcain
27th October 2006, 16:05
So does visual studio no longer need the dllexport and dllimport rules. That would be convenient!

stevey
29th October 2006, 10:45
You do still need that, but Qt wraps them up into a <libname>_global.h and you include that in each class you want to export / import.

Here's an example:



#ifndef IMAGECACHE_GLOBAL_H
#define IMAGECACHE_GLOBAL_H

#include <Qt/qglobal.h>


#ifdef IMAGECACHE_LIB
# define IMAGECACHE_EXPORT Q_DECL_EXPORT
#else
# define IMAGECACHE_EXPORT Q_DECL_IMPORT
#endif


#endif // IMAGECACHE_GLOBAL_H



You basically need to stick IMAGECACHE_EXPORT in front of the class name:


#ifndef IMAGECACHE_H
#define IMAGECACHE_H

#include "imagecache_global.h"
#include <QObject>

class IMAGECACHE_EXPORT ImageCache : public QObject
{
Q_OBJECT

public:
ImageCache();
~ImageCache();


You could write a dll without these Export and Import macros but you'd have runtime linking issues and would have to define explicit load instructions for each function you want access to. You also can't add a reference to a dll from Visual Studio unless these macros are defined.

But, as I said originally, to allow dynamic linking in the above example, I'd stick #include "imagecache.h" in my client application and add a project reference to imagecache.dll
Then the client application will be able to compile without having to worry about having a .lib or .exp file in your build path.



Steve York

mickey
28th November 2006, 20:06
You don't need the .lib
The trick with Visual Studio .NET 2k3 and 2k5 is to add a 'Reference' to the dll.
Go to the 'Solution Explorer' and right click the client project (the one that needs access to the dll), then select 'Add Reference'.
A dialog will be shown listing all the .NET, .COM and Project references.

The dialog allows you to select the dll you need.
If you have a solution open and you dll and client project within that solution, then click on the 'Projects' tab. Your dll project will be listed here. Select it from the list and click the 'Select' button (to the right of the tab control), then click 'Ok' (bottom of the dialog).

If you are referring to a dll that isn't from a project in your solution, then you can 'Browse...' to the dll explicitly.
Steve York

sorry,
I'm trying to follow your instructions but this errror occured; I've got a dll create from project A; after I created projetc B and I try to import dll within B project; I try to add this reference from all tabs (.net, com, project); the error is the same; why that?
thanks.