In that blog I wrote, the comment just means you should not use MS APIs to get/edit the storage directly. i.e. you firstly need to use Inventor API to access the storage we provide for you: Document. GetPrivateStorage. But after you get it, the remaining jobs are all MS APIs. So what we need to do is to just convert them to .NET. Yes, there are some tricks to use the relevant APIs in .NET, though.
I have worked out a sample as below. I also googled something on internet. It works well with Creating. However it only works with Reading in one scenario . I have not figured it out why. If I get anything, I will update the post.
Anyway, hope the code is a bit helpful for you to get started with the relevant APIs in .NET.
Thanks to the links:
http://www.pinvoke.net/default.aspx/Enums/STGM.html
http://bytes.com/topic/c-sharp/answers/760093-accessing-com-c-istream-parameter
using MVOI = Microsoft.VisualStudio.OLE.Interop;
[Flags]
public enum STGM : int
{
DIRECT = 0x00000000,
TRANSACTED = 0x00010000,
SIMPLE = 0x08000000,
READ = 0x00000000,
WRITE = 0x00000001,
READWRITE = 0x00000002,
SHARE_DENY_NONE = 0x00000040,
SHARE_DENY_READ = 0x00000030,
SHARE_DENY_WRITE = 0x00000020,
SHARE_EXCLUSIVE = 0x00000010,
PRIORITY = 0x00040000,
DELETEONRELEASE = 0x04000000,
NOSCRATCH = 0x00100000,
CREATE = 0x00001000,
CONVERT = 0x00020000,
FAILIFTHERE = 0x00000000,
NOSNAPSHOT = 0x00200000,
DIRECT_SWMR = 0x00400000,
}
bool CreatePrivateStorageAndStream(Document pDoc,
string StorageName,
string StreamName,
string data)
{
try
{
MVOI.IStorage pStg = (MVOI.IStorage)pDoc.GetPrivateStorage(StorageName, true);
if (pStg == null)
returnfalse;
MVOI.IStream pStream = null;
pStg.CreateStream(StreamName, (uint)(STGM.DIRECT | STGM.CREATE | STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, 0, out pStream);
if (pStream == null)
returnfalse ;
byte[] byteVsize = System.BitConverter.GetBytes(data.Length);
byte[] byteVData = Encoding.Default.GetBytes(data);
uint dummy;
pStream.Write(byteVsize, (uint)(sizeof(int)), out dummy);
pStream.Write(byteVData, (uint)(byteVData.Length), out dummy);
// Save the data
pStream.Commit((uint)(MVOI.STGC.STGC_OVERWRITE | MVOI.STGC.STGC_DEFAULT));
//Don't forget to commit changes also in storage
pStg.Commit((uint)(MVOI.STGC.STGC_DEFAULT | MVOI.STGC.STGC_OVERWRITE));
//pDoc.Dirty = true;
//pDoc.Save();
returntrue;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
returnfalse;
}
}
bool ReadPrivateStorageAndStream(Document pDoc,
string StorageName,
string StreamName,
outstring outDataStr)
{
outDataStr = "";
try
{
MVOI.IStorage pStg = (MVOI.IStorage)pDoc.GetPrivateStorage(StorageName, false);
if (pStg == null)
returnfalse;
MVOI.IStream pStream = null;
pStg.OpenStream(StreamName,IntPtr.Zero, (uint)(STGM.DIRECT| STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, out pStream);
if (pStream == null)
returnfalse;
byte[] byteVsize = newbyte[16];
uint intSize = sizeof(int);
uint dummy;
pStream.Read(byteVsize, (uint)intSize, out dummy);
int lSize = System.BitConverter.ToInt16(byteVsize,0);
byte[] outDataByte = newbyte[8192];
pStream.Read(outDataByte, (uint)lSize, out dummy);
outDataStr = Encoding.Default.GetString(outDataByte, 0, lSize);
returntrue;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
returnfalse;
}
}
privatevoid test()
{
string progId = "Inventor.Application";
Inventor.Application m_inventorApp =
(Inventor.Application)Marshal.GetActiveObject(progId);
Document pDoc = m_inventorApp.ActiveDocument;
bool Result = CreatePrivateStorageAndStream(pDoc,
"MyPrvStorage1",
"MyStream1",
"Some private stored data");
if (Result)
{
pDoc = m_inventorApp.ActiveDocument;
string outPutStr = null;
bool readResult = ReadPrivateStorageAndStream(pDoc,
"MyPrvStorage1",
"MyStream1", out outPutStr);
MessageBox.Show(outPutStr);
}
}