SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 5 of 5
  1. #1
    Moderator AJ's Avatar
    Join Date
    Jun 1999
    Location
    San Jose, CA
    Posts
    1,691
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Passing a structure to a DLL?

    How do you pass or return a structure from a DLL function?
    AJ Alhait
    BetaSoft Inc.

  2. #2
    Senior Member
    Join Date
    Dec 2000
    Location
    Pune, Maharashtra, India
    Posts
    1,037
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Passing a structure to a DLL?

    Pointer to a void does the trick. The caller should know how to "unpack" it. As an example, consider how thread functions are passed parameters.

    void main()
    {
    // We are passing an integer as an example
    // This could be a struct too
    int i_g_Param=10;

    ::CreateThread( NULL, 0, SureshThreadFunc, (LPVOID) g_i_Param, 0, NULL);

    .
    .
    .
    }

    DWORD WINAPI SureshThreadFunc(LPVOID lpThreadParam)
    {
    // unpack into local variable
    // I know that the value I'm getting here is an
    // integer - hence the cast
    int iParam = (int) lpThreadParameter;
    .
    .
    .
    }
    Suresh Nageswaran, CQA, CSTE
    Lead Consultant
    Kanbay Inc.
    Pune, India.
    sureshnageswaran@yahoo.com

  3. #3
    Moderator AJ's Avatar
    Join Date
    Jun 1999
    Location
    San Jose, CA
    Posts
    1,691
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Passing a structure to a DLL?

    Suresh, but what if you are passed back a structure? How do you handle that?
    AJ Alhait
    BetaSoft Inc.

  4. #4
    Senior Member
    Join Date
    Dec 2000
    Location
    Pune, Maharashtra, India
    Posts
    1,037
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Passing a structure to a DLL?

    Similar fashion - and like I said, both caller and callee would need to know how to pack/unpack the struct.

    Consider this:
    <<Definition Header - will be included>>
    typedef struct
    {
    int i;
    float k;
    }TIMEPASS;

    << The DLL header - definition>>
    _declspec dllexport int ProcessStruct(LPVOID);

    << DLL C file - implementation>>
    BOOL APIENTRY DllMain( HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved
    )
    {
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
    break;
    }
    return TRUE;
    }
    _declspec dllexport int ProcessStruct(LPVOID lpParam);
    {

    return (iA+iB);
    }
    Suresh Nageswaran, CQA, CSTE
    Lead Consultant
    Kanbay Inc.
    Pune, India.
    sureshnageswaran@yahoo.com

  5. #5
    Senior Member
    Join Date
    Dec 2000
    Location
    Pune, Maharashtra, India
    Posts
    1,037
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Passing a structure to a DLL?

    If you (the caller) are getting a DLL function(the callee) to give you a struct, then you need to have access(include) to the definition of the struct. As an alternative, if you are able to tinker with the DLL code itself, you could simply pass back a pointer to a void.

    // << Struct defined - call this timepass.h>>
    // << This is included by both caller and callee
    typdef struct t{
    int i;
    float j;
    } TIMEPASS;

    // << This is the implementation
    // - inside the DLL>>
    #include <timepass.h>
    _declspec dllexport LPVOID StructPassingDLLFunction()
    {
    // Under no circumstances must you actually
    // return a pointer to a local variable
    // That ranks among the all time "duh" mistakes.

    // This is just an example so I'm taking a
    // liberty

    TIMEPASS tp;
    tp.i = 10;
    tp.j = 11;
    return ((LPVOID) tp);
    }

    // <<Here's the caller>>
    #include <timepass.h>

    main()
    {
    // Either ::LoadLibrary() or static linking
    // -whatever makes you happier will preceed
    // the call
    TIMEPASS returnedTP = (TIMEPASS)StructDLLPassingFunction();
    .
    .
    .
    }


    Alternatively, you could simply create a new struct within the DLL function (malloc) and pass it back. Freeing it will also be your responsibility.
    Suresh Nageswaran, CQA, CSTE
    Lead Consultant
    Kanbay Inc.
    Pune, India.
    sureshnageswaran@yahoo.com

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 12.50%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 06:33 AM.

Copyright BetaSoft Inc.