exporting classes in a DLL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfonso Morra

    #1

    exporting classes in a DLL

    I am familiar with creating lean and mean WIN32 DLLS and exporting C
    functions via a DEF file (or cconv decorators), but I am wndering how I
    can export my C++ objects from my DLLs?

    Ideally, I would be able to use the classes (and their methods) by
    calling the objects and invoking their methods - is this possible, given
    the fact that the C++ compiler decorates names ?

    looking forward to an informed answer -tkx

  • Rodrigo Corral [MVP]

    #2
    Re: exporting classes in a DLL

    // in your header...

    class __declspec(dlle xport) CDllTest
    {
    public:
    CDllTest(){}
    ~CDllTest(){}

    public:
    void SayHello();
    };

    // in your cpp...

    void CDllTest::SayHe llo()
    {
    printf(_T("Hell o World!!"));
    }

    Another option: if you create a new dll project using VS it will create an
    example dummy exported class.


    --
    Un saludo
    Rodrigo Corral González [MVP]

    FAQ de microsoft.publi c.es.vc++



    Comment

    • Alfonso Morra

      #3
      Re: exporting classes in a DLL

      Gracias mucho por su ayuda :-)
      (Thanks for your help)

      Rodrigo Corral [MVP] wrote:
      [color=blue]
      > // in your header...
      >
      > class __declspec(dlle xport) CDllTest
      > {
      > public:
      > CDllTest(){}
      > ~CDllTest(){}
      >
      > public:
      > void SayHello();
      > };
      >
      > // in your cpp...
      >
      > void CDllTest::SayHe llo()
      > {
      > printf(_T("Hell o World!!"));
      > }
      >
      > Another option: if you create a new dll project using VS it will create an
      > example dummy exported class.
      >
      >[/color]

      Comment

      • Alfonso Morra

        #4
        Re: exporting classes in a DLL



        Rodrigo Corral [MVP] wrote:
        [color=blue]
        > // in your header...
        >
        > class __declspec(dlle xport) CDllTest
        > {
        > public:
        > CDllTest(){}
        > ~CDllTest(){}
        >
        > public:
        > void SayHello();
        > };
        >
        > // in your cpp...
        >
        > void CDllTest::SayHe llo()
        > {
        > printf(_T("Hell o World!!"));
        > }
        >
        > Another option: if you create a new dll project using VS it will create an
        > example dummy exported class.
        >
        >[/color]

        Thanks - but that's on the exporting side of things. I wanted to know
        how to use the exported classes in a seperate project that imports the
        classes and uses them. The code you gave above, only shows how to export
        the classes (which I already know how to do).

        mtia

        Comment

        • Nishant Sivakumar

          #5
          Re: exporting classes in a DLL

          The class declaration header file should look something like this :-

          #ifdef EXPORTSCLASS
          #define EXPORTCLASS_API __declspec(dlle xport)
          #else
          #define EXPORTCLASS_API __declspec(dlli mport)
          #endif

          class EXPORTCLASS_API CMyClass
          {
          public:
          //...
          };

          When you use this in the DLL project, define EXPORTSCLASS, but refrain from
          doing so in the EXE project where you call this dll.

          --
          Regards,
          Nish [VC++ MVP]
          Nish’s thoughts on technology leadership, cloud architecture, and APIs, among other things




          "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
          news:dbij8j$km8 $1@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
          >
          >
          > Rodrigo Corral [MVP] wrote:
          >[color=green]
          >> // in your header...
          >>
          >> class __declspec(dlle xport) CDllTest
          >> {
          >> public:
          >> CDllTest(){}
          >> ~CDllTest(){}
          >>
          >> public:
          >> void SayHello();
          >> };
          >>
          >> // in your cpp...
          >>
          >> void CDllTest::SayHe llo()
          >> {
          >> printf(_T("Hell o World!!"));
          >> }
          >>
          >> Another option: if you create a new dll project using VS it will create
          >> an example dummy exported class.
          >>
          >>[/color]
          >
          > Thanks - but that's on the exporting side of things. I wanted to know how
          > to use the exported classes in a seperate project that imports the classes
          > and uses them. The code you gave above, only shows how to export the
          > classes (which I already know how to do).
          >
          > mtia
          >[/color]


          Comment

          • Alfonso Morra

            #6
            Re: exporting classes in a DLL



            Nishant Sivakumar wrote:
            [color=blue]
            > The class declaration header file should look something like this :-
            >
            > #ifdef EXPORTSCLASS
            > #define EXPORTCLASS_API __declspec(dlle xport)
            > #else
            > #define EXPORTCLASS_API __declspec(dlli mport)
            > #endif
            >
            > class EXPORTCLASS_API CMyClass
            > {
            > public:
            > //...
            > };
            >
            > When you use this in the DLL project, define EXPORTSCLASS, but refrain from
            > doing so in the EXE project where you call this dll.
            >[/color]
            Thanks. this is what I was looking for.

            Comment

            • Alfonso Morra

              #7
              Re: exporting classes in a DLL



              Nishant Sivakumar wrote:
              [color=blue]
              > The class declaration header file should look something like this :-
              >
              > #ifdef EXPORTSCLASS
              > #define EXPORTCLASS_API __declspec(dlle xport)
              > #else
              > #define EXPORTCLASS_API __declspec(dlli mport)
              > #endif
              >
              > class EXPORTCLASS_API CMyClass
              > {
              > public:
              > //...
              > };
              >
              > When you use this in the DLL project, define EXPORTSCLASS, but refrain from
              > doing so in the EXE project where you call this dll.
              >[/color]

              Nishant, I tried this (exported a single method) and then run dumbin
              against my DLL. I find that the name of the method is mangled (as I had
              suspected).

              Two questions:

              If I declare a FAR PASCAL calling convention (i.e. declspec(export )) at
              the class level, do I get all the public methods exported "for free" or
              do I have to manually export every public method in the classes public
              interface?

              2). If the names are mangled (ehich I suspect they will be because of
              the overloading etc I have in my code), can I still use the unmangled
              names of classes and methods in an application that links to the library?

              tks

              Comment

              • Alfonso Morra

                #8
                Re: exporting classes in a DLL



                Alfonso Morra wrote:
                [color=blue]
                >
                >
                > Nishant Sivakumar wrote:
                >[color=green]
                >> The class declaration header file should look something like this :-
                >>
                >> #ifdef EXPORTSCLASS
                >> #define EXPORTCLASS_API __declspec(dlle xport)
                >> #else
                >> #define EXPORTCLASS_API __declspec(dlli mport)
                >> #endif
                >>
                >> class EXPORTCLASS_API CMyClass
                >> {
                >> public:
                >> //...
                >> };
                >>
                >> When you use this in the DLL project, define EXPORTSCLASS, but refrain
                >> from doing so in the EXE project where you call this dll.
                >>[/color]
                >
                > Nishant, I tried this (exported a single method) and then run dumbin
                > against my DLL. I find that the name of the method is mangled (as I had
                > suspected).
                >
                > Two questions:
                >
                > If I declare a FAR PASCAL calling convention (i.e. declspec(export )) at
                > the class level, do I get all the public methods exported "for free" or
                > do I have to manually export every public method in the classes public
                > interface?[/color]

                OK I found the answer to do this. I just did this and the answer is yes.[color=blue]
                >
                > 2). If the names are mangled (ehich I suspect they will be because of
                > the overloading etc I have in my code), can I still use the unmangled
                > names of classes and methods in an application that links to the library?
                >[/color]
                The names are mangled as I had expected. I will try using the library
                from another project and see if I can access the classses andntheir
                methods using the undecorated names - unless you reply to this before
                I'm through with my test ...[color=blue]
                > tks
                >[/color]

                Comment

                • Nishant Sivakumar

                  #9
                  Re: exporting classes in a DLL

                  The names are mangled, but in the calling exe, if you include the header
                  file and add the lib (a lib is generated along with the dll) to the linker
                  list, you will be alright. Note that the lib does not contain the real
                  functions (it just contains definitions that the linker can use, so you need
                  to have the dll for the exe to run).

                  --
                  Regards,
                  Nish [VC++ MVP]
                  Nish’s thoughts on technology leadership, cloud architecture, and APIs, among other things




                  Comment

                  Working...