CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Threaded View

  1. #1
    Join Date
    May 2002
    Posts
    1,798

    How to call a C++ dll function at runtime ?

    Despite an enormous amount of information available on runtime dynamic linking of DLLs, I cannot get things to work using Win 7 / VS 2010. While there are many pitfalls, my current problem is how to find the 'decorated' names. Here is a snippet of a header file for my dll (lap.dll):
    Code:
    #ifdef LAP_EXPORTS
    #define LAP_API __declspec(dllexport)
    #else
    #define LAP_API __declspec(dllimport)
    #endif
    
    #include "Parser.h"
    
    
    // This class is exported from the LAP.dll
    class LAP_API Clap {
    public:
    	Clap(void);
    	~Clap(void);
    
    // Attributes
    protected:
    	char * m_pList;
    	double ** m_fMatrix;
    	CParser * m_pParser;
    
    // Operations
    public:
    	void PutMatrix(double ** pMatrix);
    	double ** GetMatrix();
    	void DumpMatrix(double ** pMatrix);
    	double **CreateMatrix(int nRows, int nCols);
    	void FreeMatrix(double ** pMatrix);
    	double ** Parse(char * expr);
    	int GetOpMode();
    	int SaveVariableMap(char * sfile);
    	int LoadVariableMap(char * sfile);
    	char * GetKeyList();
    	char * GetVarList();
    	char * GetFunList();
    	void AddVariable(TNT::Matrix<double> Mc, char * svarnam);
    	int EraseAllVariables();
    	int RunScript(char * sfile);
    	int ProcessScript(char* sscript);
    	
    };
    and here is the lap.def file:
    Code:
    LIBRARY
    
    EXPORT
    
    	PutMatrix
    	GetMatrix;
    	DumpMatrix
    	CreateMatrix
    	FreeMatrix
    	Parse
    	GetOpMode
    	SaveVariableMap
    	LoadVariableMap
    	GetKeyList
    	GetVarList
    	GetFunList
    	AddVariable
    	EraseAllVariables
    	RunScript
    	ProcessScript
    Both lap.dll and the test app compile, but I cannot figure out how to define the correct function prototypes in the test file.

    Here's a fragment of the test file. The handle comes back as valid, but I cannot get the proc address correctly:
    Code:
    #include "stdafx.h"
    
    #include <windows.h> 
    #include <stdio.h> 
    
    typedef double ** PARSEPROC(LPSTR);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	cout << "Normal Beginning" << endl << endl;
    
        HINSTANCE hinstLib; 
        PARSEPROC ProcAdd; 
        BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 
     
        // Get a handle to the DLL module.
     
        hinstLib = LoadLibrary(TEXT("lap.dll")); 
     
        if (hinstLib != NULL) 
        { 
    		printf("\nHandle is valid\n");
    
    		ProcAdd = (PARSEPROC) GetProcAddress(hinstLib, "Parse"); 
            // If the function address is valid, call the function.
     
            if (NULL != ProcAdd) 
            {
    			printf("\nProcAdd is not NULL\n\n");
    
                fRunTimeLinkSuccess = TRUE;
            }
            // Free the DLL module.
     
            fFreeResult = FreeLibrary(hinstLib); 
        } 
    
        // If unable to call the DLL function, use an alternative.
        if (! fRunTimeLinkSuccess) 
            //printf("Message printed from executable\n"); 
    		printf("Unable to call the DLL function\n");
    
    	printf("\n\nNormal Termination\n\n");
    
    
    	getchar(); 
    
    	return 0;
    }
    All my efforts have failed to capture the procedure address. In fact, the way the test app is written here, it will not compile as it will not allow assignment (or casting) of the GetProcAddress, even though GetProcAddress(hinstLib, "Parse"); by itself will compile and run. I am uncertain whether this problem is due to name decoration, test app syntax, def file syntax, or some other problem.

    There used to be a way to set the compiler to generate docorated names file when compiling a dll, but I can not find such a tool in VS 2010. The DumpBin.exe tool needed to have the path to mspdb100.dll set. When run against lap.dll, it DumpBin produces the following :
    Code:
    File Type: DLL
    
      Summary
    
            2000 .data
            3000 .idata
           18000 .rdata
            7000 .reloc
            1000 .rsrc
           BA000 .text
           59000 .textbss
    which I have no clue as to what it means.

    Any help greatly appreciated. But I've looked at a number of tutorials and most are either outdated or deal only with C declared function. It is worth noting that lap.dll has performed flawlessly when used as a staically bound dll.
    Last edited by Mike Pliam; July 30th, 2011 at 10:02 PM.
    mpliam

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