CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Jan 2006
    Posts
    384

    Rules for DLL Function Signatures

    Hi,

    Are there any specific rules as regards parameters that make up a DLL function signature which is exposed to the outside world ?

    In addition, Is it an accepted practice to have DLL function signatures which
    1.Take class objects as parameters (object parameters, pointer to object or reference to object)
    2. Return class objects as parameters (object parameter, pointer to object parameter, or reference object)

    I suppose it is acceptable to return an object pointer from a DLL, because that is one of the ways in which plug-ins are implemented (where the return type is an Interface pointer)

    Please do let me know.

    The DLL is expected to be consumed by C or C++ clients.

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Rules for DLL Function Signatures

    1. Yes.
    2. Yes.
    3. For any object allocated in Dll, supply Release function which should delete it. Client should not try to release objects allocated in another Dll.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Rules for DLL Function Signatures

    Quote Originally Posted by Alex F View Post
    1. Yes.
    2. Yes.
    3. For any object allocated in Dll, supply Release function which should delete it. Client should not try to release objects allocated in another Dll.
    Yes, this is the correct guideline. There are some exceptions to rule 3), and that is if The DLL uses the Windows API to allocate the memory (i.e. GlobalAlloc and those funtions).

    For example, various third-party DLL's that deal with imaging will allocate the memory using GlobalAlloc, HeapAlloc, etc. and it's the responsibiltiy of the app to release the image memory. Of course, this needs to be documented to the user of the DLL.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Rules for DLL Function Signatures

    1.Take class objects as parameters (object parameters, pointer to object or reference to object)
    2. Return class objects as parameters (object parameter, pointer to object parameter, or reference object)
    I would say no to both. What if the class definition changes and function is still assuming the old version' class has been passed, or vice versa. The same goes for returning a class object - doesn't matter if you return the object or the pointer to object.

    Would you ever try to return std::vector from a DLL function? Or would you expect a function taking map<string,list<int>::iterator> & ? These classes would definitely change! The client might be using other version of STL, and your DLL might be working in other version of STL. STL doesn't have DLL, no runtime library, thus size/definition would definitely differ!

    The only case, where I can expect taking/returning the class, when the same DLL is exporting the classes. Even in that case, I may check at runtime - using ASSERT to check it in debug, and performing sizeof checks in release. Though, for performance reasons, I may omit runtime check at release version.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Rules for DLL Function Signatures

    Quote Originally Posted by Ajay Vijay View Post
    IWould you ever try to return std::vector from a DLL function? Or would you expect a function taking map<string,list<int>::iterator> & ?
    Yes, STL objects should never be passed between Dll and its client. This is well-known problem. Interesting, what is general rule for all templates, not only STL.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Rules for DLL Function Signatures

    Quote Originally Posted by Alex F View Post
    Yes, STL objects should never be passed between Dll and its client. This is well-known problem. Interesting, what is general rule for all templates, not only STL.
    I would not take the trouble of passing CDataExchange or CWordArray either.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Rules for DLL Function Signatures

    I suppose it is acceptable to return an object pointer from a DLL, because that is one of the ways in which plug-ins are implemented (where the return type is an Interface pointer)
    1. Pointer to object is not the same thing as interface pointer
    2. Interface pointer is a standard way for dealing with/interchanging objects in plugin technology
    3. Interface declaration must follow __stdcall convention to guarantee real cross-compiler compatibility
    Last edited by Igor Vartanov; May 11th, 2010 at 11:50 AM.
    Best regards,
    Igor

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Rules for DLL Function Signatures

    Interface declaration must follow __stdcall convention to guarantee real cross-compiler compatibility
    Did you mean the object-oriented interface term, or the interface you are giving via your exported functions? Former is not possible since calling conventions apply to functions only. Latter doesn't fit the discussion.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Rules for DLL Function Signatures

    Quote Originally Posted by Ajay Vijay
    Former is not possible since calling conventions apply to functions only.
    Actually, this is what I meant (not sure what you did):
    Code:
    typedef struct {
        virtual int __stdcall foo0() = 0;
        virtual int __stdcall foo1(int a) = 0;
        virtual int __stdcall foo2(int a, int b) = 0;
    } MyFooInterface, *PMyFooInterface;
    Best regards,
    Igor

  10. #10
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Rules for DLL Function Signatures

    The discussion is about arguments and return types from a function.

    And typedefining a structure with virtual function??
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Rules for DLL Function Signatures

    The only time I would expose classes from a dll is if I only used the dll within exe projects I controlled. That way, I could always rebuild both the exe and the dll.

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Rules for DLL Function Signatures

    Quote Originally Posted by Ajay Vijay View Post
    The discussion is about arguments and return types from a function.
    Yep, exactly. Arguments and return types, as well as objects and interfaces, dlls and plug-ins.

    And typedefining a structure with virtual function??
    Man, it looks like you never delved into COM and plug-in technologies. Lucky guy.
    Best regards,
    Igor

  13. #13
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Rules for DLL Function Signatures

    Quote Originally Posted by Igor Vartanov View Post
    Yep, exactly. Arguments and return types, as well as objects and interfaces, dlls and plug-ins.
    The OP asked if it is okay to pass or return the class objects. Interfaces are different thing. Yes you can pass interfaces, with less issues than classes. The definition of interface, and importance of it are off beat, as per this thread.

    Man, it looks like you never delved into COM and plug-in technologies. Lucky guy.
    Hmm. You did not get me. A typedef struct {...}theStruct; definition reminds me of C, and code compatibility with C. Now, when you include a pure virtual function in it, it become obscure.

    COM Interaface? Well, that also uses IDL/ODL files, which are composed of classes/coclasses and not C-style struct typedefs.

    Interface declaration must follow __stdcall convention to guarantee real cross-compiler compatibility
    This seriously has nothing to do with DLL exported function signatures, taking class objects.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  14. #14
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Rules for DLL Function Signatures

    @Igor,
    I guess you are more concerned about this following statement.
    I suppose it is acceptable to return an object pointer from a DLL, because that is one of the ways in which plug-ins are implemented (where the return type is an Interface pointer)
    But, in my way of interpreting it, he is just giving an example on how Plug-ins are implemented. With that knowledge, he is quite sure that object/class-pointer may be returned from an exported DLL function. But again, I believe, he is not concerned about interfaces as such, and __stdcall convention in interfaces.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Rules for DLL Function Signatures

    Ajay, I really appreciate your input. But would you just slow down with guessing? and let the thread originator utter a couple of words about his real point of interest? Thank you.
    Best regards,
    Igor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured