validating an object pointer returned by an exported function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • srilalpr@gmail.com

    #1

    validating an object pointer returned by an exported function

    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

  • benben

    #2
    Re: validating an object pointer returned by an exported function

    srilalpr@gmail. com wrote:[color=blue]
    > 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.[/color]

    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

    Comment

    • srilalpr@gmail.com

      #3
      Re: validating an object pointer returned by an exported function

      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

      Comment

      • TB

        #4
        Re: validating an object pointer returned by an exported function

        srilalpr@gmail. com skrev:[color=blue]
        > 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
        >[/color]

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

        --
        TB @ SWEDEN

        Comment

        • srilalpr@gmail.com

          #5
          Re: validating an object pointer returned by an exported function

          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,
          "ExportedFuncti on");

          if (!pMyClassCreat e)
          {
          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 ExportedFunctio n does not return the
          pointer to MyClass
          then executing pMyClass->MyFunction is giving access violations.

          Thanks & Regards,
          Srilal

          Comment

          • TB

            #6
            Re: validating an object pointer returned by an exported function

            srilalpr@gmail. com skrev:[color=blue]
            > 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,
            > "ExportedFuncti on");
            >
            > if (!pMyClassCreat e)
            > {
            > 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 ExportedFunctio n does not return the
            > pointer to MyClass
            > then executing pMyClass->MyFunction is giving access violations.
            >[/color]

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

            --
            TB @ SWEDEN

            Comment

            • srilalpr@gmail.com

              #7
              Re: validating an object pointer returned by an exported function

              hi,

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

              e.g if the ExportedFunctio n has the prototype int Function(int,in t)
              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

              TB wrote:
              [color=blue]
              > srilalpr@gmail. com skrev:[color=green]
              > > 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,
              > > "ExportedFuncti on");
              > >
              > > if (!pMyClassCreat e)
              > > {
              > > 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 ExportedFunctio n does not return the
              > > pointer to MyClass
              > > then executing pMyClass->MyFunction is giving access violations.
              > >[/color]
              >
              > if(pMyClassobj) {
              > pMyClassobj->MyFunction() ;
              > }
              >
              > --
              > TB @ SWEDEN[/color]

              Comment

              • TB

                #8
                Re: validating an object pointer returned by an exported function

                srilalpr@gmail. com skrev:[color=blue]
                > hi,
                >
                > ok.
                > for me when i execute pMyClassCreate( );
                > i get an invalid pointer to MyClass object ie. pobjMyClassobj is
                > invalid.[/color]

                pobjMyClassobj? Can't find that name in the source code you posted.
                [color=blue]
                >
                > e.g if the ExportedFunctio n has the prototype int Function(int,in t)
                > then executing it will give me int instead of MyClass*[/color]

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

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

                int != MyClass*
                [color=blue]
                > Thanks & Regards,
                > Srilal
                >
                > TB wrote:
                >[color=green]
                >> srilalpr@gmail. com skrev:[color=darkred]
                >>> 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,
                >>> "ExportedFuncti on");
                >>>
                >>> if (!pMyClassCreat e)
                >>> {
                >>> 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 ExportedFunctio n does not return the
                >>> pointer to MyClass
                >>> then executing pMyClass->MyFunction is giving access violations.
                >>>[/color]
                >> if(pMyClassobj) {
                >> pMyClassobj->MyFunction() ;
                >> }
                >>
                >> --
                >> TB @ SWEDEN[/color]
                >[/color]

                --
                TB @ SWEDEN

                Comment

                • Rolf Magnus

                  #9
                  Re: validating an object pointer returned by an exported function

                  srilalpr@gmail. com wrote:
                  [color=blue]
                  > hi,
                  >
                  > ok.
                  > for me when i execute pMyClassCreate( );
                  > i get an invalid pointer to MyClass object ie. pobjMyClassobj is
                  > invalid.
                  >
                  > e.g if the ExportedFunctio n has the prototype int Function(int,in t)
                  > 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.[/color]

                  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.

                  Comment

                  Working...