how to transfer a DWORD from a function?

Discussion in 'C++' started by monkeydragon, Dec 12, 2005.

  1. monkeydragon

    monkeydragon Guest

    how to would you transfer a DWORD variable from inside of a function to
    the caller
    ex.
    InvokeProcessData(LPDWORD prtDW)
    {
    ...
    // we have created, initialized and processed
    // dword variable from
    DWORD data[32];

    // we need to transfer DWORD data to caller
    }


    so that, the calling function would look like:

    winmain(...)
    {

    DWORD new_data_buffer;
    InvokeProcessData(&new_data_buffer);

    //later 'new_data_buffer' will be used for example:

    WriteFile(hFile, &new_data_buffer, ...,)

    }
     
    monkeydragon, Dec 12, 2005
    #1
    1. Advertising

  2. monkeydragon wrote:
    > how to would you transfer a DWORD variable from inside of a function to
    > the caller
    > ex.
    > InvokeProcessData(LPDWORD prtDW)
    > {
    > ..
    > // we have created, initialized and processed
    > // dword variable from
    > DWORD data[32];
    >
    > // we need to transfer DWORD data to caller
    > }
    >
    >
    > so that, the calling function would look like:
    >
    > winmain(...)
    > {
    >
    > DWORD new_data_buffer;
    > InvokeProcessData(&new_data_buffer);
    >
    > //later 'new_data_buffer' will be used for example:
    >
    > WriteFile(hFile, &new_data_buffer, ...,)
    >
    > }
    >

    You can't return an array in C, so you have to use a pointer. Then
    there are several ways.

    You can make the function fill an array defined outside it directly.
    Just define DWORD new_data_buffer[32], and call
    InvokeProcessData(new_data_buffer). Then use prtDW inside the function,
    instead of 'data'.

    You can also have a static array inside of InvokeProcessData and just
    return a pointer to it:

    LPDWORD InvokeProcessData(void)
    {
    static DWORD data[32];
    /* fill data with data */

    /* this returns a pointer, NOT a copy of the array */
    return data;
    }
    Everytime you call InvokeProcessData it will then overwrite the array,
    so you need to copy it if you want to keep the previous version for use
    outside the function. But you'd probably want the first method instead
    of this.

    I'm assuming that LPDWORD is just a DWORD *. And DWORD might be 32 bit
    integer, not that it matters.

    And you might want to use

    #define PROCESS_DATA_SIZE 32

    or something like that, instead of using the number directly. It
    documents what the number is for, makes it easier to change it since you
    only need to change it in one spot, and also makes it unlikely that you
    type 23 somewhere instead of 32, etc.
     
    Tydr Schnubbis, Dec 12, 2005
    #2
    1. Advertising

  3. monkeydragon

    nSurgeon Guest

    int foo (LPDWORD prtDW, LPDWORD& prtResultDW)
    {
    // .....
    // Create required size array and store pointer to it in external
    // variavle, and size of it can be returned as a result of
    function.
    int res_size = XX;
    prtResultDW = new DWORD[res_size];

    /// ....

    return res_size;
    }

    or

    void foo (LPDWORD prtDW, LPDWORD prtResultDW)
    {
    // .....
    // prtResultDW = pointer to array where is enough space for store
    results
    // ....
    }
     
    nSurgeon, Dec 12, 2005
    #3
  4. "monkeydragon" <> wrote:

    > how to would you transfer a DWORD variable from inside of a function to
    > the caller
    > ex.
    > InvokeProcessData(LPDWORD prtDW)
    > {
    > ..
    > // we have created, initialized and processed
    > // dword variable from
    > DWORD data[32];
    >
    > // we need to transfer DWORD data to caller
    > }
    >
    >
    > so that, the calling function would look like:
    >
    > winmain(...)
    > {
    >
    > DWORD new_data_buffer;
    > InvokeProcessData(&new_data_buffer);
    >
    > //later 'new_data_buffer' will be used for example:
    >
    > WriteFile(hFile, &new_data_buffer, ...,)
    >
    > }


    You passed a pointer to InvokeProcessData; so why
    not just write to that location in that function,
    like so:

    (*prtDW) = 72;

    That will write 72 to your local variable in Winmain().

    If you need to pass more data than just one DWORD
    back to Winmain, then make an array in Winmain()
    and pass it to InvokeProcessData():

    // In Winmain():
    DWORD BigArray[500];
    InvokeProcessData(BigArray)

    // In InvokeProcessData():
    InvokeProcessData(DWORD* Blat)
    {
    for (int i=0; i<500; ++i)
    {
    Blat = whatever // writes to BigArray in Winmain()
    }
    }

    Or better, pass a std::list by non-const ref:

    // In Winmain():
    ....
    std::list<DWORD> MyList;
    ....
    InvokeProcessData(MyList);
    ....

    // In InvokeProcessData():
    void InvokeProcessData(std::list<DWORD> & Blat)
    {
    ...
    std::list<DWORD>::iterator
    for (i=Blat.begin(); i!=Blat.end; ++i)
    {
    (*i) = whatever // writes to MyList in Winmain()
    }
    ...
    }

    --
    Robbie Hatley
    Tustin, CA, USA
    lone wolf intj at pac bell dot net
    home dot pac bell dot net slant earnur slant
     
    Robbie Hatley, Dec 12, 2005
    #4
  5. monkeydragon

    monkeydragon Guest

    i did something like this:
    InvokeProcessData(UINT selOffset, LPDWORD pData)
    {
    DWORD byteStorage[32];
    // initializations and other things...
    // then...
    *pData = byteStorage[selOffset];
    }

    what do you think?
     
    monkeydragon, Dec 12, 2005
    #5
  6. monkeydragon

    nSurgeon Guest

    If I understand You correctly, You try return from function array of
    DWORD values, or not, and You need return only one ? If you want return
    only one You can use return operator.
     
    nSurgeon, Dec 13, 2005
    #6
  7. monkeydragon

    Geo Guest

    monkeydragon wrote:
    > i did something like this:
    > InvokeProcessData(UINT selOffset, LPDWORD pData)
    > {
    > DWORD byteStorage[32];
    > // initializations and other things...
    > // then...
    > *pData = byteStorage[selOffset];
    > }
    >
    > what do you think?


    I think, no I know, you don't want to do that (assuming LPDWORD is a
    DWORD*).

    Outside of InvokeProcessData, byteStorage doesn't exist, so pData will
    be a pointer into unallocated memory, very bad.
     
    Geo, Dec 13, 2005
    #7
  8. monkeydragon

    monkeydragon Guest

    I am sorry to cause you much trouble, but i have already resolved that
    problem.
    I use CopyMemory();
    Thank anyways,

    _May_the_force_be_with_you_
     
    monkeydragon, Dec 14, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lee K

    DWORD date value

    Lee K, Jul 17, 2003, in forum: C++
    Replies:
    6
    Views:
    5,725
    Victor Bazarov
    Jul 18, 2003
  2. Xanax
    Replies:
    20
    Views:
    3,617
    Corno
    Sep 10, 2003
  3. Denis Remezov
    Replies:
    7
    Views:
    979
    Phlip
    Jul 10, 2004
  4. Khuong Dinh Pham

    convert std::string to (byte*, DWORD)

    Khuong Dinh Pham, Aug 20, 2005, in forum: C++
    Replies:
    16
    Views:
    1,334
    Larry I Smith
    Aug 30, 2005
  5. Guest
    Replies:
    4
    Views:
    721
    Guest
    Aug 24, 2006
Loading...

Share This Page