-1

I got a function :

SAFEARRAY FAR* pArray = NULL;

and I get that function : pServer1->GetDirectMemory(dwAddrBegin, dwAddrEnd, wDisplayWidth, &pArray);

I want to get information from pArray, if i look the structure of it, I have PVOID pvData; that must be contains my information.

How could I get it in a int ?

Old question :

I want to get the data of a PVOID to a int value

I get a SAFEARRAY FAR* pArray and I have only one element so I get ir with a PVOID type like that :

PVOID myData = pArray[0].pvData;

And I try to get the data with the function PtrToInt :

int myNbr = PtrToInt(myData);

But my int (myNbr) doens't get the same value that i can see with my debugger. So my question is how can i get datas from that SAFEARRAY FAR* pArray or PVOID without using MFC function like SafeArrayAccessData or else.

Thanks

Fred37b
  • 822
  • 2
  • 10
  • 29
  • Are you just trying to cast a pointer to an int, eg `int p=reinterpret_cast(ptr)` ? – Sean Feb 12 '14 at 15:49
  • 1
    `SafeArrayAccessData` is in the `OleAuto.h` header you shouldn't need to include MFC to access it. – Mgetz Feb 12 '14 at 15:54
  • I get an adress in my PVOID and I want to put the value where is a that adress, in a int variable – Fred37b Feb 12 '14 at 16:05
  • Your latest comment is unclear. A `PVOID` is a pointer, right? (I'm guessing it's a typedef for `void*`.) Do you want the pointer value converted to `int`, or do you want the value of the `int` object that it points to? If the former, that's rarely a useful thing to do, and depending on the system it can lose information. If the latter, why wasn't the pointer defined as an `int*` in the first place? (There can be good reasons to use `void*` to point to `int` data, but usually using an `int*` is the best approach.) – Keith Thompson Feb 12 '14 at 16:11
  • I use a PVOID because the structure `SAFEARRAY` was defined in using PVOID. I got a int in the `SAFEARRAY FAR* pArray` and I cannot get the value to put it in a int – Fred37b Feb 12 '14 at 16:16
  • @Fred37b this question is **HIGHLY** unclear as to the exact circumstances of the problem you're facing. Can you update your question to include a minimum viable example that demonstrates the problem? – Mgetz Feb 12 '14 at 16:18

1 Answers1

1

Assuming your array contains ints (use SafeArrayGetVartype to verify), is 1-dimensional (use SafeArrayGetDim) and 0-based (use SafeArrayGetLBound), the correct way to access it is this:

int value;
LONG indices[] = { 0 };
if (FAILED(SafeArrayGetElement(pArray, indices, &value))) {
  // getting element failed - probably bad index
}
// value now contains the correct value

You can use SafeArrayAccessData too, but unless you have identified a performance problem, it's better not to.

In general when dealing with OLE structures (VARIANT, SAFEARRAY, etc.) you should always use the provided utility functions. They are part of Windows, not MFC. Here's the reference for arrays:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms221145(v=vs.85).aspx

However, if you can, I strongly recommend you use ATL's wrapper for SAFEARRAY, CComSafeArray. See the documentation and a short blog article showing its usage:

http://msdn.microsoft.com/en-us/library/3xzbsee8.aspx http://msmvps.com/blogs/gdicanio/archive/2011/02/04/simplifying-safearray-programming-with-ccomsafearray.aspx

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • Thank for your answer. Maybe it's not int because int does not contain the correct value. – Fred37b Feb 12 '14 at 16:32
  • I work with a µController which is on 16 bits when my computer is on 32 bits, so the µP send me a 16 bits int and my system don't see a int because for it, it must be on 32 bits. So i change the type of value from int to char and now i receive my variable. Thanks for helping – Fred37b Feb 12 '14 at 16:58