Click to See Complete Forum and Search --> : calling dll functions..?


swapnil_paranjape
July 15th, 2005, 02:56 AM
I have written the class and made the dll file .

Now i want call the member functions of the class from my application.

so i am writing wrapper functions which my application will use.These wrapper functions will call these dll functions.

how should i do this?how to call a dll functions from function defined in my application.?

i am setting some variable using my wrapper functons which are set by constructor of the class on dll side.
i have defined a global object of the class.

thanx

humptydumpty
July 15th, 2005, 03:06 AM
I have written the class and made the dll file .

Now i want call the member functions of the class from my application.

so i am writing wrapper functions which my application will use.These wrapper functions will call these dll functions.

how should i do this?how to call a dll functions from function defined in my application.?

i am setting some variable using my wrapper functons which are set by constructor of the class on dll side.
i have defined a global object of the class.

thanx
First THing Tell Properly your question like what r u trying and type of your application

any way try this


if you are using win32 applicaqtion
use LoadLibrary()
& GetProcAddress()



Using MFC use
dllexport & dllimport method



if you are using
use
object creation
like ISimple *ptr;
::CocreateInstance(CLSID,NULL,CLSCTX_INPROC_SERVER,IID,(void**)&ptr);
ptr->Functionname()


try this if still you are getting problem let me know
and i think there is a lot of stuff in MSDN for you just go with function Name

swapnil_paranjape
July 15th, 2005, 03:59 AM
please explain me this....


if you are using
use
object creation
like ISimple *ptr;
::CocreateInstance(CLSID,NULL,CLSCTX_INPROC_SERVER,IID,(void**)&ptr);
ptr->Functionname()

humptydumpty
July 15th, 2005, 05:03 AM
please explain me this....


if you are using
use
object creation
like ISimple *ptr;
::CocreateInstance(CLSID,NULL,CLSCTX_INPROC_SERVER,IID,(void**)&ptr);
ptr->Functionname()

See Swapnil
this means you are using ATL for your application
.Simply after Loading your dll in your program you can use all the function Present in it.like Normal Function

when you make a ATL dll and insert your Object .after performing your all work when you build your application
it,will generate two file which is useful for you
assume your object name is Simple
so it's going to generate File for your workspace
Workspace.h
and WorkSpace_i.c
copy these two files and your EXE side go and use add to project option and put the file there
then in your Exe
assume name is he
so at he.cpp side
just include the name of the two file

now your work is create a Object of your CSimple Class

if you don't know how to do
just open your WorkSpace_i.c file.
copy name of CLSID_SIMPLE; //Object Name
now at your he.cpp side
drag app
upto msg structure

Create a Object
use Simple put i infront of it
like ISimple *ptr;

now use CoCreateinstance function for Creating an instance of an object in a type-safe manner like
::CoCreateInstance();takes 5 Parameter
1)CLSID of Object
2) If NULL, indicates that the object is not being created as part of an aggregate. If non-NULL, pointer to the aggregate object's IUnknown interface, the controlling IUnknown.
3) your server type for local use CLSCTX_INPROC_SERVER
4) IID of OBject in this case IID_ISimple
5) void pointer Address of pointer variable that receives the interface pointer

and use HRESULT for checking the return value
so syntax is
::CocreateInstance

HRESULT hr = ::CoCreateInstance(CLSID_ISimple,NULL,CLSCTX_INPROC_SERVER,IID_ISimple,(void**)&ptr);

if(SUCCEEDED(hr))
{
call your dll function with the help of ptr;
like ptr->Functionname();
}

and
after that it's upto you where to use and how


for more details either check MSDN or let me know what's your requirement and what you really want

NMTop40
July 15th, 2005, 05:29 AM
dllimport and dllexport are not specific to MFC. It is a way of specifying in your code (without the use of .def files) what symbols you are exporting.

Your best way to export your class is with an abstract base class. There is no need to put any decorative symbols around this ABC at all. Thus:


class MyABC
{
public:
virtual MyMethod1() = 0;
};

basically all the methods that your client will use. (You can use the "template pattern" method instead of using protected virtual methods and public non-virtual methods to call them by proxy. That is really a separate topic though).

Within your DLL you have a class that implements this.

Finally you need to export a "factory" method to create an instance of your class. This should return a pointer to the abstract base class. You will also need a method to destruct the class. (It will probably call delete internally). You should do this rather than request users to call delete themselves. (You could return a smart-pointer, but beware if you use a standard one like boost, the users of the DLL may have a different version of boost to you and it might not be fully portable).

Now how will users link with your DLL? If "directly" then you use __declspec( dllexport ) or declare the factory methods in a .def file. (Note also that the factory methods can be an object rather than a function). The .def file is probably simpler, but you will need extern "C" linkage in this case.

Using __declspec you should put this in your header:

#ifndef MYPROJ_EXPORTS
#ifdef _WIN32
#define MYPROJ_EXPORTS __declspec(dllimport)
#else // you can also #elif for other platforms
#define MYPROJ_EXPORTS
#endif
#endif

And somewhere in your .cxx module where you have the actual symbol you put:

#ifdef _WIN32
#define MYPROJ_EXPORTS __declspec(dllexport)


Now for the symbols you export, put MYPROJ_EXPORTS in front. (change MYPROJ to the name of your DLL).

Thus:


extern MYPROJ_EXPORTS MyABC * createABC( /*params*/ );
extern MYPROJ_EXPORTS void deleteABC( /*params*/ );


There is no need for these symbols to be extern "C" and they can be in a namespace too if you like.

Using a .def file though they cannot be in a namespace and must be extern "C". You do not need the other decoration though. Thus:


extern "C" MyABC * createABC( /*params*/ );
extern "C" void deleteABC( /*params*/ );

Yes, you could define

#ifdef USING_DEF
#define MYPROJ_EXPORTS "C"
#endif

Now if you do use a .def file, it should look something like this:

LIBRARY MyProj

EXPORTS

createMyABC @ 1
deleteMyABC @ 2