[comp.windows.ms.programmer] Dynamic Run-Time import with C++

iam@waikato.ac.nz (Ian McDonald) (05/27/91)

I'm trying to call a DLL routine dynamically (ie using a LoadLibrary) but I am
having difficulty because of C++ type checking.  In C you can go :

  (*lpFunc) ((LPSTR) Buffer,512);

for example if lpFunc is of type FARPROC.  (This is taken from page 20-33 of
the SDK Guide to Programming).  With C++ it will not let me do this because of
the type checking.  I have tried (and failed) to make a typedef to do this
although I am not too experienced with typedef.  Could somebody help me out by
showing me a way to have a typedef equivalent to FARPROC but allows parameters
(defined in advannce) or do a typecast to use FARPROC properly.

-- 
-------------------------------------------------------------------------
Ian McDonald  |                                           
52 Cook Street|                                             /  /\  /\
Hamilton      |   This space free for advertising !!       /  /  \/  \
New Zealand   |                                           /  /        \
+64-71-563438 |   I can receive mail but can't reply to it.  

cadsi@ccad.uiowa.edu (CADSI) (05/28/91)

From article <1991May27.123911.3813@waikato.ac.nz>, by iam@waikato.ac.nz (Ian McDonald):
> I'm trying to call a DLL routine dynamically (ie using a LoadLibrary) but I am
> having difficulty because of C++ type checking.  In C you can go :
> 
>   (*lpFunc) ((LPSTR) Buffer,512);
> 
> for example if lpFunc is of type FARPROC.  (This is taken from page 20-33 of
> the SDK Guide to Programming).  With C++ it will not let me do this because of
> the type checking.  I have tried (and failed) to make a typedef to do this
> although I am not too experienced with typedef.  Could somebody help me out by
> showing me a way to have a typedef equivalent to FARPROC but allows parameters
> (defined in advannce) or do a typecast to use FARPROC properly.

CHEAP Example:

	{
		FARPROC lpfn = (FARPROC)lpFunc;
		(*lpfn)((LPSTR)Buffer, 512);
	}

Otherwise, just typedef the call inline:

	(*((FARPROC)lpFunc))((LPSTR)Buffer, 512);

Both should work.

|----------------------------------------------------------------------------|
|Tom Hite					|  The views expressed by me |
|Manager, Product development			|  are mine, not necessarily |
|CADSI (Computer Aided Design Software Inc.	|  the views of CADSI.       |
|----------------------------------------------------------------------------|

CADSI@sunbrk.FidoNet.Org (CADSI) (05/28/91)

From article <1991May27.123911.3813@waikato.ac.nz>, by iam@waikato.ac.nz (Ian McDonald):
> I'm trying to call a DLL routine dynamically (ie using a LoadLibrary) but I am
> having difficulty because of C++ type checking.  In C you can go :
> 
>   (*lpFunc) ((LPSTR) Buffer,512);
> 
> for example if lpFunc is of type FARPROC.  (This is taken from page 20-33 of
> the SDK Guide to Programming).  With C++ it will not let me do this because of
> the type checking.  I have tried (and failed) to make a typedef to do this
> although I am not too experienced with typedef.  Could somebody help me out by
> showing me a way to have a typedef equivalent to FARPROC but allows parameters
> (defined in advannce) or do a typecast to use FARPROC properly.

CHEAP Example:

	{
		FARPROC lpfn = (FARPROC)lpFunc;
		(*lpfn)((LPSTR)Buffer, 512);
	}

Otherwise, just typedef the call inline:

	(*((FARPROC)lpFunc))((LPSTR)Buffer, 512);

Both should work.

|----------------------------------------------------------------------------|
|Tom Hite					|  The views expressed by me |
|Manager, Product development			|  are mine, not necessarily |
|CADSI (Computer Aided Design Software Inc.	|  the views of CADSI.       |
|----------------------------------------------------------------------------|

 * Origin: Seaeast - Fidonet<->Usenet Gateway - sunbrk (1:343/15.0)