CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    52

    Question calling dll functions..?

    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

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: calling dll functions..?

    Quote Originally Posted by swapnil_paranjape
    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

    Code:
    if you are using win32 applicaqtion
    use LoadLibrary()
    & GetProcAddress()
    Code:
    Using MFC use
    dllexport & dllimport method
    Code:
    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

  3. #3
    Join Date
    Jul 2005
    Posts
    52

    Question Re: calling dll functions..?

    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()

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: calling dll functions..?

    Quote Originally Posted by swapnil_paranjape
    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
    Code:
    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
    Last edited by humptydumpty; July 15th, 2005 at 05:05 AM.

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: calling dll functions..?

    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:

    Code:
    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:
    Code:
    #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:
    Code:
    #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:

    Code:
    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:

    Code:
    extern "C" MyABC * createABC( /*params*/ );
    extern "C" void deleteABC( /*params*/ );
    Yes, you could define
    Code:
    #ifdef USING_DEF
    #define MYPROJ_EXPORTS "C"
    #endif
    Now if you do use a .def file, it should look something like this:
    Code:
    LIBRARY MyProj
    
    EXPORTS
    
    createMyABC @ 1
    deleteMyABC @ 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured