64,683
社区成员
发帖
与我相关
我的任务
分享
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif // _DEBUG
#define _CRTDBG_MAP_ALLOC
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include "oaidl.h"
#include "Psapi.h"
#pragma comment(lib, "Psapi.lib")
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif // _DEBUG
void displayMem(char * string)
{
HANDLE hProcess = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(hProcess, &pmc, sizeof(PROCESS_MEMORY_COUNTERS));
std::cout << string << "\t\tMemSize: " << pmc.WorkingSetSize << " B\n";
CloseHandle(hProcess);
}
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
//创建SAFEARRAY数组,每个元素为long型,该数组是一维数组
long nData[10]={1,2,3,4,5,6,7,8,9,10};
displayMem("before create SafeArray");
SAFEARRAY* pArray=NULL;
HRESULT hr=SafeArrayAllocDescriptor(1,&pArray);// 创建SAFEARRAY结构的对象
displayMem("after create SafeArray");
pArray->cbElements=sizeof(nData[0]);
pArray->rgsabound[0].cElements=10;
pArray->rgsabound[0].lLbound=0;
SafeArrayAllocData(pArray);
long* pData=NULL;
SafeArrayAccessData(pArray,(void**)&pData);
long l(0),h(0);
SafeArrayGetLBound(pArray,1,&l);
SafeArrayGetUBound(pArray,1,&h);
long Size=h-l+1;
SafeArrayAccessData(pArray,(void**)&pData);
for(long Idx=l;Idx<Size;++Idx)
{
pData[Idx]=nData[Idx];
std::cout << pData[Idx] << std::endl;
}
SafeArrayUnaccessData(pArray);
//访问SAFEARRAY数组
long* pValue=NULL;
SafeArrayAccessData(pArray,(void**)&pValue);
long Low(0),High(0);
hr=SafeArrayGetLBound(pArray,1,&Low);//维数索引从1开始
hr=SafeArrayGetUBound(pArray,1,&High);// 维数索引从1开始
SafeArrayUnaccessData(pArray);
displayMem("before destory SafeArray");
SafeArrayDestroy(pArray);
displayMem("after destory SafeArray");
getchar();
return 0;
}