CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764

    How to use C++ class from a dll without lib file

    Hi,

    It might be simple but I've been breaking my head since some time.

    There is a 3rd party dll and I want to use a class in that dll as base class.

    I declare my class this way..
    Code:
    class CMyClass : public CThirdParty
    {
    };
    class __declspec(dllimport) CRfCommPort
    {
    // blah blah blah...
    };

    And I compile it and it gives me link errors as if the class is only defined in header but not implemented. And it is understandable as I didn't link it with its lib file.

    I have lib file but I dont want to link it with lib coz.. my app doesn't open in systems where the 3rd party's dll doesn;t exist and it says required dll is missing. (Unfortunately I'm not allowed to ship the 3rd party's dll with my application)

    Can anyone show me a way where I can make my app work for both systems with and without 3rd party's DLL?

    Thanks,

    John.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  2. #2
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    Oops i'm sorry, read
    Code:
    class __declspec(dllimport) CRfCommPort
    {
    // blah blah blah...
    };
    AS

    Code:
    class __declspec(dllimport) CThirdParty
    {
    // blah blah blah...
    };
    This CThirdParty class is defined as above in the header that I use.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  3. #3
    Join Date
    Jun 2002
    Posts
    395
    Only way I see is to aggregate the third party object and expose the functions of the third party object via functions in your object.

    Then you can dynamically load the third party dll and use GetProcAddress() to load all the functions you need.

    Code:
    class CMyClass
    {
    public:
        CMyClass() : pTPO(null) {
           hmod = LoadLibrary("thirdparty.dll");
    
           if (hmod) {
              typedef  CThirdParty *createobjectfunc();          
              createobjectfunc COfunc = (createobjectfunc )GetProcAddress(hmod, "CreateObject");
              if (COfunc)
                 pTPO = COfunc();
          }
       }
    
       DWORD ThirdPartyRotate(float degrees) { 
          if (pTPO == NULL) return 0;
          typedef DWORD *rotatefunc(float deg, CThirdPartyObject *pobj);
          rotatefunc rfunc = (rotatefunc)GetProcAddess(hmod, "RotateObject");
          if (rfunc) {
              rfunc(degrees, pTPO);
              return 1; 
          }
          else
              return 0;
       }
    
    private:
       MODULE hmod;
       CThirdParty * pTPO;
    
    };

  4. #4
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    Good point, but the main reason why I derive my class from the 3rd party's class is to implement the virtual functions of 3rd party class so that I can code for sifferent events.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    2 questions:
    1. You may not be allowed to ship the 3rd party dll, but are you at least allowed to use the 3rd party .lib ?
    2. At runtime, is it possible for you to know if you have to call the 3rd party class or not ? I mean, do you have sufficient knowledge to NOT call the class methods if the 3rd party dll doesn't exist ?

  6. #6
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    2 questions:
    1. You may not be allowed to ship the 3rd party dll, but are you at least allowed to use the 3rd party .lib ?
    2. At runtime, is it possible for you to know if you have to call the 3rd party class or not ? I mean, do you have sufficient knowledge to NOT call the class methods if the 3rd party dll doesn't exist ?
    1. I'm not sure if I can ship lib. But can lib be of any use when there is no dll?
    2. Yes, it is possible at runtime for me to know.

    I'm currently trying to keep the whole code (that uses 3rd party DLL) in a seperate DLL and planning to use that DLL dynamically as its my DLL and i can define the way I want it to be. So probably I can load this DLL dynamically.

    I'm open to any better ideas though!
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Ok. The lib is need for linking. No getting away from that. So, as far as developing your stuff, that hurdle is gone since I assume that you always have the lib available for linking.

    Coming to the second point, since you can decide at runtime if you have to call into the 3rd party stuff or not, you have an option to try. It's pretty cool and is rarely used. And it is well suited for exactly the situation like this.

    There is what is called as delayloading. You can specify in your linker options that a particular dll is to be delay loaded. When you say that, the loader actually patches the calls to this dll only when it is called first time or something like that. So, the loader won't bother to load the "delayloaded" dll until unless you really call into it. Hence, the app will load even if it links to that dll which may not even be there.

    In you case, the 3rd party dll is an ideal candidate to be delayloaded. Please read the documentation on DELAYLOAD linker option. You will find enough stuff to detail what needs to be done. it's a pretty simple thing.

    Hope this helps.

  8. #8
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    hi kirants,

    excellent!!! you're my hero.

    this is what exactly i need. i'm trying to implement it.

    thanks again,

    john.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

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