Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to create a sample VC++ DLL , using VS .Net 2005 platform

The below content is my ExoDLL.h file

// function that called it.
extern "C" __declspec(dllexport)void BoxProperties(double Length, double Height,
double Width, double& Area, double& Volume);


class Test
{


// This function is used to calculate the total area of a parallelepid rectangle
double BoxArea(double L, double H, double W);
// This function is used to calculate the volume of a parallelepid rectangle


double BoxVolume(double L, double H, double W);


}

extern Test testobj;
**********************************************************************
And my ExoDll.cpp file is having below content

// ExoDLL.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "ExoDLL.h"



#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
double Test::BoxArea(double L, double H, double W)
{
return 2 * ((L*H) + (L*W) + (H*W));
}

double Test::BoxVolume(double L, double H, double W)
{
return L * H * W;

}
void BoxProperties(double L, double H, double W, double A, double V)
{
testobj = new Test();
A = testobj.BoxArea(L, H, W);
V = testobj.BoxVolume(L, H, W);

}
#ifdef _MANAGED
#pragma managed(pop)
#endif

*********************************************************************

When I am trying to build the solution getting the below error message

"error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Test *' (or there is no acceptable conversion)"

**********************************************************************

Since I am novice to VC++ , I am bit confused on where I am making mistakes.

Please help me to rsolve the issue.

Thanks,
Pattabi

******************************************************************

Hi,

Your suggestion helped me lot.But Now I am getting Linker errors as below.

1.Error 1 error LNK2020: unresolved token (0A00000D) "class Test * test" (?test@@3PAVTest@@A) ExoDLL.obj

2.Error 2 error LNK2001: unresolved external symbol "class Test * test" (?test@@3PAVTest@@A) ExoDLL.obj

3.Error 3 fatal error LNK1120: 2 unresolved externals C:\Documents and Settings\WiproCC\My Documents\Visual Studio 2005\Projects\ExoDLL\Debug\ExoDLL.dll

Please suggest.

Surely I will go through some C++ book
Posted
Updated 18-Jan-10 0:42am
v3

If you know this little about C++, writing a dll is not a great starting point. DLLs are a little complex in C++. Also, when you ask for help, if you could tell us what line has the error, it would help.


wrote:
extern Test testobj;


This is a Test object

wrote:
testobj = new Test();


This is a call to new. New in C++ is used to create a new object to assign to a pointer. Your variable is not a pointer, so it's constructor has already been called, and the statement is trying to return a Test * and assign it to a Test, which is not possible, hence the error.
 
Share this answer
 
Hi,

Thanks for your reply. Even I have tried to create the Pointer obect like below

Test* testobj;

But I am getting the same error on the below line

A = testobj.BoxArea(L, H, W);

Could you please let me know how do I need to change the code ?

Note: The above code is compiling properly when there isn't any class statement.
 
Share this answer
 
When you want to add new content, please modify the original post, i.e. don't post additional questions as 'Answer'.

As about:

A = testobj.BoxArea(L, H, W);


You should change it according to pointer notation (since, now, testobj is a pointer):

A = testobj->BoxArea(L, H, W);



You really need a good C++ book, though.
 
Share this answer
 
v2
Hi,

The problem got resolove.

I removed the keyword "extern" from header file line

extern Test testobj; & the linker error gone.

Is it right solution ? Please let me know for any clarification.

Thanks,
Pattabi.
 
Share this answer
 
I agree a DLL project is very ambitious as a learning exercise. There are all sorts of issues with testing, getting functions to export correctly etc, you don't want to be distracted with too soon!

There's a lot of things which would improve the structure of this project, but here are one or two to start with...

(1) Get rid of
extern Test testObj;
i.e. don't just remove "extern", remove the whole line.
Globals are not a good idea if you can avoid them.
You should just declare a local variable inside BoxProperties(); see (3).

(2) The definition of BoxProperties() in ExoDLL.cpp must agree with the declaration in ExoDLL.h ... you had "double A" which should be "double& A" in the .cpp file.

(3) Don't use "testobj = new Test();" - this creates a fresh object each time this function is called ... not only is this slow because you're allocating memory, you're not releasing it, so if this function gets called a lot, you might even run out of memory!!
Instead use "Test testobj;" which allocates the object on the stack and removes it automatically when the function returns.

C#
void BoxProperties(double L, double H, double W, double& A, double& V)
{
Test testobj;
A = testobj.BoxArea(L, H, W);
V = testobj.BoxVolume(L, H, W);
}


(3) BTW, there is no need for a class like Test at all. Just pull BoxArea() and BoxVolume() out of the class and call them direct. It will be quicker as you're not constructing the Test object with each call to BoxProperties(). Eg

C#
void BoxProperties(double L, double H, double W, double& A, double& V)
{
A = BoxArea(L, H, W);
V = BoxVolume(L, H, W);
}


(4) If you must use a class, at least make use of it.
Set up L, H, W as data members, and leave BoxArea/BoxVolume in the class. Add a constructor for Test to feed in L,H,W.
Then you don't need to pass in L,H,W to BoxArea() and BoxVolume() because they are members of the class and thus already know L,H,W.
To save space I've put the function definitions inside the class declaration.

Notice `public` & `private` keywords. `public` means visible to any user of the class. `private` means visible only within the class.
Normally you hide (i.e. make private) class data members so users of the class can only access them indirectly via methods.

C#
class Test
{
public:
Test( double Lin, double Hin, double Win)
{
  L=Lin; H=Hin; W=Win;
}
double BoxArea() { return 2 * ((L*H) + (L*W) + (H*W)); }
double BoxVolume() { return L * H * W; }

private:
double L;
double H;
double W;
};

void BoxProperties(double L, double H, double W, double& A, double& V)
{
Test testobj(L,H,W);
A = testobj.BoxArea();
V = testobj.BoxVolume();
}


P.S. When defining a class don't forget to put a semicolon after the closing curly braces. If you forget, you can get some weird looking error messages.
 
Share this answer
 
v2
Your answers just indicate again that you are indeed a beginner. Nothign wrong with that. Don't set your self up to be discouraged by trying something too complex to start with. Forget about dlls, and just buy a basic book and work through it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900