|
-
July 31st, 2011, 11:41 AM
#6
Re: How to call a C++ dll function at runtime ?
Igor asked:
what actually you're trying to do.
I want to give an application interface away for free, then charge for modular updates. I think the runtime dynamic linking dll is the way to go here. 1) Your client only needs to download and install a new dll and not the whole program interface 2) if the dll called by the client is not available (he didnt purchase it yet), the program can handle it and it will still run(unlike what happens if a static dll is missing). To accomplish this, the main app interface needs to be able to call the new dll function(s). Since there are only a few functions in the dll that serve as a gateways to the dll library, this shouldnt be much of a problem. I think that this is the method used by many programs to provide updates, add-ons, and plug-ins.
Paul, I downloaded and employed DependencyWalker, dragging lap.dll onto it's main window, and was quickly able to obtain a complete list of the decorated function names. THANKS! With that, I am able to obtain a Proc Address that is presumably valid. Now, how to use that address to execute the desired function in the test app?
Code:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
typedef double ** PARSEPROC(LPSTR);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hinstLib;
FARPROC ParseProcAddr;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("lap.dll"));
if (hinstLib != NULL)
{
printf("\nHandle is valid\n");
ParseProcAddr = GetProcAddress(hinstLib, "?Parse@Clap@@QAEPAPANPAD@Z"); // Tochiba
if (ParseProcAddr == NULL) { printf("ParseProcAddr is NULL\n"); }
// If the function address is valid, call the function.
if (NULL != ParseProcAddr)
{
printf("\nParseProcAddr is not NULL\n\n");
fRunTimeLinkSuccess = TRUE;
double ** pMatrix;
//pMatrix = (double **)(ParseProcAddr)("x = 3");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Unable to call the DLL function\n");
return 0;
}
Output:
Handle is valid
ParseProcAddr is not NULL
So the problem now becomes, given a function prototype definition, double ** PARSEPROC(LPSTR), and given a presumably valid dll procedure address, FARPROC ParseProcAddr, how do I use these to execute the procedure and retrieve the double ** ? That which I have commented out above will not compile for reasons of both assignment and casting errors.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|