validating an object pointer returned by an exported function

S

srilalpr

Hi,

I have a dll and one of its exported function is suppossed to return a
pointer to an object.
How can i check whether the exported function is of the same prototype
i want.


Please comment


Thanks & Regards
Srilal
 
B

benben

Hi,

I have a dll and one of its exported function is suppossed to return a
pointer to an object.
How can i check whether the exported function is of the same prototype
i want.

Please be noted that the concept of DLL may be implemented differently
across platforms and such a concept is not a concern of the C++ standard
and is generally off-topic in this newsgroup. You may therefore consult
platform specific documentation on DLL's or ask in a more suitable
newsgroup.

That said, I think the C++ name mangling would do the trick, although in
some systems it is mandated exported functions must be extern "C" and
therefore cannot be mangled.

Regards,
Ben
 
S

srilalpr

hi,
actually what i meant was, if i am executing the exported function
then how can i test if the returned values is a valid pointer to the
expected object.

Thanks & Regards,
Srilal
 
T

TB

(e-mail address removed) skrev:
hi,
actually what i meant was, if i am executing the exported function
then how can i test if the returned values is a valid pointer to the
expected object.

Thanks & Regards,
Srilal

If the function does not guarantee that a returned non-null pointer
is valid, then don't use the function.
 
S

srilalpr

Hi,

here is the code snippent of my application

HINSTANCE hHandle = LoadLibrary ("UserDLL.dll");

//If the dll handle returns is null then trow exception.
if (!hHandle)
{ throw Exception; }

//Else get call the exported function pointer for creating the
object
typedef MyClass* (*pMyClass) ();

pMyClass pMyClassCreate= (pMyClass) GetProcAddress(hHandle,
"ExportedFunction");

if (!pMyClassCreate)
{
if (hHandle)
{
FreeLibrary (hHandle);
}

throw Exception
}

MyClass * pMyClassobj;
try
{
pMyClassobj = pMyClassCreate();
}
catch(...)
{
throw Exception;
}

try
{
pMyClass->MyFunction();
}
catch(...)
{
}

now my question is what if the ExportedFunction does not return the
pointer to MyClass
then executing pMyClass->MyFunction is giving access violations.

Thanks & Regards,
Srilal
 
T

TB

(e-mail address removed) skrev:
Hi,

here is the code snippent of my application

HINSTANCE hHandle = LoadLibrary ("UserDLL.dll");

//If the dll handle returns is null then trow exception.
if (!hHandle)
{ throw Exception; }

//Else get call the exported function pointer for creating the
object
typedef MyClass* (*pMyClass) ();

pMyClass pMyClassCreate= (pMyClass) GetProcAddress(hHandle,
"ExportedFunction");

if (!pMyClassCreate)
{
if (hHandle)
{
FreeLibrary (hHandle);
}

throw Exception
}

MyClass * pMyClassobj;
try
{
pMyClassobj = pMyClassCreate();
}
catch(...)
{
throw Exception;
}

try
{
pMyClass->MyFunction();
}
catch(...)
{
}

now my question is what if the ExportedFunction does not return the
pointer to MyClass
then executing pMyClass->MyFunction is giving access violations.

if(pMyClassobj) {
pMyClassobj->MyFunction();
}
 
S

srilalpr

hi,

ok.
for me when i execute pMyClassCreate();
i get an invalid pointer to MyClass object ie. pobjMyClassobj is
invalid.

e.g if the ExportedFunction has the prototype int Function(int,int)
then executing it will give me int instead of MyClass*

so is there a way to check whether the returned pointer is of a valid
object because else executing MyFunction will give access violation.

Thanks & Regards,
Srilal
 
T

TB

(e-mail address removed) skrev:
hi,

ok.
for me when i execute pMyClassCreate();
i get an invalid pointer to MyClass object ie. pobjMyClassobj is
invalid.

pobjMyClassobj? Can't find that name in the source code you posted.
e.g if the ExportedFunction has the prototype int Function(int,int)
then executing it will give me int instead of MyClass*

Yes, of course it does.
so is there a way to check whether the returned pointer is of a valid
object because else executing MyFunction will give access violation.

Which pointer, it apparently returns an 'int', not a pointer
to an object of MyClass.

int != MyClass*
 
R

Rolf Magnus

hi,

ok.
for me when i execute pMyClassCreate();
i get an invalid pointer to MyClass object ie. pobjMyClassobj is
invalid.

e.g if the ExportedFunction has the prototype int Function(int,int)
then executing it will give me int instead of MyClass*

so is there a way to check whether the returned pointer is of a valid
object because else executing MyFunction will give access violation.

I would simply do that like you do it with every other library function.
Make a header that belongs to the library and declare the prototype in that
header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top