My own written DLL not accessible in dialog based app; [modified]
-
hllo guys...I tried to write my own DLL with simple functions which so far im successful. It lookd like this
MyDll.h
namespace MyDll
{
class MyOwnDll
{
//function here
static _declspec(dllexport) void PrintMsg();
};
}MyDll.cpp
namespace MyDll
{
void MyOwnDll::PrintMsg()
{
::MessageBox(0,"","",0);
}
}Now I added this DLL to my dialog based application (MyDll.h in "stdafx.h" of MyProj) and build the solution. Everything is fine and it builds. But when I try to use this, it show errors stating Undeclared Identifier. Here is how I use it
MyProjDlg.cpp
{
...............
...............void MyProjDlg::MyMsg() //function added to a button { MyOwnDll mod; //myTapi, mod ==> Undeclared Identifier mod.PrintMsg(); //mod ==> Undeclared Identifier }
}
All the paths and libraries are included, everything looks fine. But why it is not able to find this?? :confused: thnx
modified on Thursday, December 2, 2010 2:47 PM
-
hllo guys...I tried to write my own DLL with simple functions which so far im successful. It lookd like this
MyDll.h
namespace MyDll
{
class MyOwnDll
{
//function here
static _declspec(dllexport) void PrintMsg();
};
}MyDll.cpp
namespace MyDll
{
void MyOwnDll::PrintMsg()
{
::MessageBox(0,"","",0);
}
}Now I added this DLL to my dialog based application (MyDll.h in "stdafx.h" of MyProj) and build the solution. Everything is fine and it builds. But when I try to use this, it show errors stating Undeclared Identifier. Here is how I use it
MyProjDlg.cpp
{
...............
...............void MyProjDlg::MyMsg() //function added to a button { MyOwnDll mod; //myTapi, mod ==> Undeclared Identifier mod.PrintMsg(); //mod ==> Undeclared Identifier }
}
All the paths and libraries are included, everything looks fine. But why it is not able to find this?? :confused: thnx
modified on Thursday, December 2, 2010 2:47 PM
You haven't qualified the class name with the namespace it's in, eg:
MyDll::MyOwnDll mod;
No idea if that's the source of your problem but it's a good place to start. Another thing to watch out for is the way you export the function from the DLL. Ideally you want to export the name in the DLL and import it in the EXE file. It might be worth having a look at some sample code to see how you're supposed to do that. Cheers, Ash
-
hllo guys...I tried to write my own DLL with simple functions which so far im successful. It lookd like this
MyDll.h
namespace MyDll
{
class MyOwnDll
{
//function here
static _declspec(dllexport) void PrintMsg();
};
}MyDll.cpp
namespace MyDll
{
void MyOwnDll::PrintMsg()
{
::MessageBox(0,"","",0);
}
}Now I added this DLL to my dialog based application (MyDll.h in "stdafx.h" of MyProj) and build the solution. Everything is fine and it builds. But when I try to use this, it show errors stating Undeclared Identifier. Here is how I use it
MyProjDlg.cpp
{
...............
...............void MyProjDlg::MyMsg() //function added to a button { MyOwnDll mod; //myTapi, mod ==> Undeclared Identifier mod.PrintMsg(); //mod ==> Undeclared Identifier }
}
All the paths and libraries are included, everything looks fine. But why it is not able to find this?? :confused: thnx
modified on Thursday, December 2, 2010 2:47 PM
On your header file you have declared your export as
__declspec(dllexport)
: this is right as it instruct the compiler that the methodMyOwnDll::PrintMsg()
is implemented in your code and should be exported from the library. On the other hand, when you use the dll, you should include the header file, but you need theMyOwnDll::PrintMsg()
method to be imported, then it should be declared as__declspec(dllimport)
. A typical approach, is to add a pre-processor definition in your dll project, for exampleMYOWNDLLPRJ
, and modify the header file as follow:#ifdef MYOWNDLLPRJ
#define MYOWNDLLAPI __declspec(dllexport)
#else
#define MYOWNDLLAPI __declspec(dllimport)
#endifnamespace MyDll
{
class MyOwnDll
{
//function here
static MYOWNDLLAPI void PrintMsg();
};
} -
On your header file you have declared your export as
__declspec(dllexport)
: this is right as it instruct the compiler that the methodMyOwnDll::PrintMsg()
is implemented in your code and should be exported from the library. On the other hand, when you use the dll, you should include the header file, but you need theMyOwnDll::PrintMsg()
method to be imported, then it should be declared as__declspec(dllimport)
. A typical approach, is to add a pre-processor definition in your dll project, for exampleMYOWNDLLPRJ
, and modify the header file as follow:#ifdef MYOWNDLLPRJ
#define MYOWNDLLAPI __declspec(dllexport)
#else
#define MYOWNDLLAPI __declspec(dllimport)
#endifnamespace MyDll
{
class MyOwnDll
{
//function here
static MYOWNDLLAPI void PrintMsg();
};
}Since im new to DLLs so plz dont mind. After this, how do I call this function in MyProjDlg.cpp? Like this?
void MyProjDlg::MyMsg()
{
MyDll::MyOwnDll mod;
mod.PrintMsg();
} -
Since im new to DLLs so plz dont mind. After this, how do I call this function in MyProjDlg.cpp? Like this?
void MyProjDlg::MyMsg()
{
MyDll::MyOwnDll mod;
mod.PrintMsg();
}That will work, but is not the right way: as the
MyDll::MyOwnDll.PrintMsg()
method isstatic
, you don't need to instantiate an object of your class to call it. The right way is the following:void MyProjDlg::MyMsg()
{
MyDll::MyOwnDll::PrintMsg();
}