当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - How to convert PVOID to int?

问题描述:

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

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: