error in an example of loading dll dynamically
-
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;
}
-
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;
}