您最初SAFEARRAYBOUND
可以像 C 数组一样创建并初始化它,例如SAFEARRAYBOUND sabdBounds[2] = { {10, 0}, {20, 0\} };
,然后使用SafeArrayCreate
(http://msdn.microsoft.com/en-us/library/windows/desktop/ms221234(v=vs.85).aspx)获得所需的适当类型和尺寸LPSAFEARRAY
。
更新:
这是一段显示如何创建的代码LPSAFEARRAY
,如您所见,我在创建数组之前找到了文件的大小,这样我就可以直接将数据读入其中,您还可以将文件内容存储在一些中间缓冲区,然后创建SAFEARRAYBOUND
后者:
#include <Windows.h>
#include <fstream>
#include <cstdlib>
int main(int argc, char** argv)
{
std::streampos fileSize = 0;
std::ifstream inputFile("file.bin", std::ios::binary);
fileSize = inputFile.tellg();
inputFile.seekg( 0, std::ios::end );
fileSize = inputFile.tellg() - fileSize;
SAFEARRAYBOUND arrayBounds[1] = { {fileSize, 0}}; // You have one dimension, with fileSize bytes
LPSAFEARRAY safeArray = SafeArrayCreate(VT_I1, 1, arrayBounds);
SafeArrayLock(safeArray);
char* pData = reinterpret_cast<char*>(safeArray->pvData); // This should be the pointer to the first element in the array, fill in the data as needed
// Do your stuff
SafeArrayUnlock(safeArray);
SafeArrayDestroy(safeArray);
inputFile.close();
}