Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. error in an example of loading dll dynamically

error in an example of loading dll dynamically

Scheduled Pinned Locked Moved COM
c++helptutorialquestionlearning
2 Posts 2 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    virusx1984
    wrote on last edited by
    #1

    In the "Dynamic Selection of a Component" section of the book , there is an example teach me to load dll dynamically. Here is my code: It couldn't run. I guess error in the file "tv.cpp". This code couldn't return a function pointer(I print the value of proc, and computer shows that proc = 0): "CREATEVCRPROC proc = reinterpret_cast(GetProcAddress(h, "CreateVcr"));" :confused: PS: I use Microsoft C++ Compiler to compile this files > cl -c tv.cpp > cl -c vcr.cpp > link -dll vcr.obj > link tv.obj Could anyone help me? Thank you very much!!

    // file: vcr.h

    #include "video.h"

    class CVcr : public IVideo{

    public:
    CVcr(void);
    long _stdcall GetSignalValue();
    void _stdcall Delete();

    private:
    long m_lCurValue;
    int m_nCurCount;
    };

    // file: vcr.cpp

    #include "vcr.h"

    CVcr::CVcr(){
    m_lCurValue = 5;
    m_nCurCount = 0;
    }

    long CVcr::GetSignalValue(){

    m\_nCurCount++;
    if(5 == m\_nCurCount){
    	m\_lCurValue = 5;
    	m\_nCurCount = 1;
    }
    
    long lReturnValue = m\_lCurValue;
    m\_lCurValue += 10;
    return lReturnValue;
    

    }

    IVideo * _stdcall CreateVcr(void){

    return new CVcr;
    

    }

    void CVcr::Delete(){

    delete this;
    

    }

    // file: video.h - Definition of interface IVideo

    class IVideo{

    public:

    virtual long \_stdcall GetSignalValue() = 0;
    virtual void \_stdcall Delete() = 0;
    

    };

    extern "C" IVideo * _stdcall CreateVcr();

    // file: tv.cpp

    #include "video.h"
    #include #include using namespace std;

    IVideo * CreateInstance(char * pszDll){

    // Define a pointer to prototype of CreateVcr function
    typedef IVideo \* (\_stdcall \* CREATEVCRPROC)(void);
    
    // Load the specified library
    HINSTANCE h = LoadLibrary(pszDll);
    
    // Obtain the procedure entry point for CreateVcr
    CREATEVCRPROC proc = reinterpret\_cast(GetProcAddress(h, "CreateVcr"));
    
    printf("proc = %d", proc);
    
    // Execute "CreateVcr" indirectly
    return (\*proc)();
    

    }

    int main(int argc, char* argv[])
    {
    int i;
    IVideo * pVideo = CreateInstance("vcr.dll");

    for(i=0; i<10; i++) {
    	long val = pVideo->GetSignalValue(); 
    	cout << "Round: " << i << " - Value: " << val << endl; 
    } 
    
    pVideo->Delete();
    
    return 0; 
    

    }

    L 1 Reply Last reply
    0
    • V virusx1984

      In the "Dynamic Selection of a Component" section of the book , there is an example teach me to load dll dynamically. Here is my code: It couldn't run. I guess error in the file "tv.cpp". This code couldn't return a function pointer(I print the value of proc, and computer shows that proc = 0): "CREATEVCRPROC proc = reinterpret_cast(GetProcAddress(h, "CreateVcr"));" :confused: PS: I use Microsoft C++ Compiler to compile this files > cl -c tv.cpp > cl -c vcr.cpp > link -dll vcr.obj > link tv.obj Could anyone help me? Thank you very much!!

      // file: vcr.h

      #include "video.h"

      class CVcr : public IVideo{

      public:
      CVcr(void);
      long _stdcall GetSignalValue();
      void _stdcall Delete();

      private:
      long m_lCurValue;
      int m_nCurCount;
      };

      // file: vcr.cpp

      #include "vcr.h"

      CVcr::CVcr(){
      m_lCurValue = 5;
      m_nCurCount = 0;
      }

      long CVcr::GetSignalValue(){

      m\_nCurCount++;
      if(5 == m\_nCurCount){
      	m\_lCurValue = 5;
      	m\_nCurCount = 1;
      }
      
      long lReturnValue = m\_lCurValue;
      m\_lCurValue += 10;
      return lReturnValue;
      

      }

      IVideo * _stdcall CreateVcr(void){

      return new CVcr;
      

      }

      void CVcr::Delete(){

      delete this;
      

      }

      // file: video.h - Definition of interface IVideo

      class IVideo{

      public:

      virtual long \_stdcall GetSignalValue() = 0;
      virtual void \_stdcall Delete() = 0;
      

      };

      extern "C" IVideo * _stdcall CreateVcr();

      // file: tv.cpp

      #include "video.h"
      #include #include using namespace std;

      IVideo * CreateInstance(char * pszDll){

      // Define a pointer to prototype of CreateVcr function
      typedef IVideo \* (\_stdcall \* CREATEVCRPROC)(void);
      
      // Load the specified library
      HINSTANCE h = LoadLibrary(pszDll);
      
      // Obtain the procedure entry point for CreateVcr
      CREATEVCRPROC proc = reinterpret\_cast(GetProcAddress(h, "CreateVcr"));
      
      printf("proc = %d", proc);
      
      // Execute "CreateVcr" indirectly
      return (\*proc)();
      

      }

      int main(int argc, char* argv[])
      {
      int i;
      IVideo * pVideo = CreateInstance("vcr.dll");

      for(i=0; i<10; i++) {
      	long val = pVideo->GetSignalValue(); 
      	cout << "Round: " << i << " - Value: " << val << endl; 
      } 
      
      pVideo->Delete();
      
      return 0; 
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to export your entry points so they are visible outside the dll; see http://msdn.microsoft.com/en-us//library/a90k134d.aspx[^].

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups