Re: how to get the pointer variable address from a dll
On Mon, 18 Jun 2007 08:13:27 -0700, "karunyalakshmi@gmail.com"
<karunyalakshmi@gmail.com> wrote:
I have a DLL which has a output parameter LPVOID, A application
calls this DLL function, the Dll responsibility is to assign a valid
pointer to the lpBuffer,
....
Prototype,
__declspec(dllexport) BOOL TRSTMonGetStatus(LPVOID lpBuffer)
Please let me know what to do, I am stuck in this.
As others have said, the problem is that you may need a new level of
indirection (pointer to a pointer).
AliR or Joe seems to have suggested using reference (&), but if your
DLL has a C-interface, I would suggest using a real
pointer-to-pointer, i.e. void ** (or LPVOID *), as COM does e.g. for
QueryInterface:
__declspec(dllexport) BOOL TRSTMonGetStatus(LPVOID * ppBuffer);
In your function implementation:
// Check valid parameters
if ( ppBuffer == NULL )
return FALSE; // Error: bad parameter
// Clear output parameter (*ppBuffer is LPVOID)
*ppBuffer = NULL;
// Allocate/process what you need here...
// Use *ppBuffer, e.g.:
BYTE * pBytes = new BYTE[1000];
// ...or whatever...
// This value is returned to caller
*ppBuffer = (LPVOID)pBytes;
Note that you should also provide in your DLL a function to *release*
the buffer:
__declspec(dllexport) void TRSTMonReleaseBuffer(LPVOID * ppBuffer)
{
if ( ppBuffer == NULL )
return;
BYTE * pBytes = (BYTE*) (*ppBuffer);
if ( pBytes == NULL )
return;
// Release the allocated array
delete [] pBytes;
// Clear caller pointer
*ppBuffer = NULL;
}
You can use your code like so:
// The caller
LPVOID pBuffer = NULL;
if ( TRSTMonGetStatus( &pBuffer ) ... )
// ... Work ...
// Release
TRSTMonReleaseBuffer( &pBuffer );
(BTW: I've not tested the code with the compiler...)
MrAsm