tags:

views:

154

answers:

2

I have a callback function which has the parameter const unsigned char *pData. everytime I hit the callback function I need to store the pData value into my local unsigned char* variable. Is there any function to copy the data?


Edit: Here is a code sample:

void Callbackfun(int x, const unsigned char* pData, const UINT cbData) {
    switch(x) {
    case 0:
        // ptr is a global variable of structure containg name and number
        ptr.name = (unsigned char*)pData;
        break;
    case 1:
        ptr.number = (unsigned char*)pData;
        break;
    }
}

now every time this function is called i want to store the pData values in my local structure (as shown by ptr.name).

+1  A: 

In your callback function, you will have to allocate local memory for your "data". That way you can retain it when the calling function leaves scope. If you know the length of the data, and the length is consistent you have two options. Dynamic allocation, or allocate on the stack. Code example is untested.

Here's the dynamic allocation and copy.

 unsigned char* localData = new unsigned char[42];
 memcpy( localData, pData, 42 );

Here's the stack allocated version

 unsigned char localData[42];
 memcpy( &localData, pData, 42 );

I'm assuming you're not passing string data since you're using unsigned char. Since you're only dealing with a pointer you'll have to know the size of the buffer for either allocation.

If you do not have a constant data length, then you'll need to pass that as a second parameter to your callback function.

nathan
@sbi I wrote the example assuming variable length string data, then realized it may not be string data. I'll remove the memory initialization.
nathan
char *a=NULL; char *s=NULL; a=(char *)calloc(1,(sizeof(char))); s=(char *)calloc(1,(sizeof(char))); a="DATA"; memcpy(s,a,(strlen(a))); printf("%s",s);Can you plz tell me why its printing DATA½½½½½½½½■ε■????How to print only DATA??Thanks
SPB
@SPB I suggest adding the code you have in your comment to the question. That will allow other people to better answer your question. The short answer is your string isn't null terminated. When you call memcpy your truncating your null character so printf keeps on printing.
nathan
@SPB: you're also not allocating enough memory for `s` (you're allocating one char, when you need 5), and leaking the memory allocated for `a` (since `a="DATA"` changes the pointer `a`, not the memory it's pointing at). Something like `char* a="DATA"; char* s = calloc(strlen(a),sizeof(char)); strncpy(s,a,strlen(a));` would be more correct (in a C program, and remembering to call `free(s)` once you've finished with it), but in C++ you should be using classes like `std::string` to manage memory allocation for you.
Mike Seymour
Good catch Mike, I looked over that part.
nathan
@nathan: Even for string data, what's needed is _one zero_ at the right place, not zeroing everything.
sbi
@sbi I agree, but not knowing how the array was going to be used I opted to set the data to a known value.
nathan
+1  A: 

If you can alter the struct that ptr seems to be an instance of, I would suggest this:

#include <string>

struct ptr_type {
    std::string name;
    std::string number;
};

ptr_type ptr;

And for the callback, assuming cbData tells us the size of the buffer pointed to by pData:

void Callbackfun(int x, const unsigned char* pData, const UINT cbData) {

    // The following line will copy the buffer pointed to by
    // pData into an std::string object:
    std::string data(pData, cbData);

    switch(x) {
    case 0:
        // Simple assignment of std::string objects copies the
        // entire buffer:
        ptr.name = data;
        break;
    case 1:
        ptr.number = data;
        break;
    }
}
Magnus Hoff