Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
guilloryt
in reply to: adam.nagy

Afternoon Adam,

 

In my testing with iStorage in .net the data was not committed to the file unless I made set the dirty flag and did doc.Save.

 

The c++ code really is only a mistery to me... But see below the code I converted from your c# example to vb...

 

 

Is there another method I could use with VB to commit?

 

 

 

 

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports Inventor
Imports System.Diagnostics
Imports MVOI = Microsoft.VisualStudio.OLE.Interop
Imports Newtonsoft.Json
Imports Cameron.FOP.ModelCF

Public Class InvDoc_Storage
Public Enum STGM As Integer
DIRECT = &H0
TRANSACTED = &H10000
SIMPLE = &H8000000
READ = &H0
WRITE = &H1
READWRITE = &H2
SHARE_DENY_NONE = &H40
SHARE_DENY_READ = &H30
SHARE_DENY_WRITE = &H20
SHARE_EXCLUSIVE = &H10
PRIORITY = &H40000
DELETEONRELEASE = &H4000000
NOSCRATCH = &H100000
CREATE = &H1000
CONVERT = &H20000
FAILIFTHERE = &H0
NOSNAPSHOT = &H200000
DIRECT_SWMR = &H400000
End Enum

' create private storage and stream
Private Function CreatePrivateStorageAndStream(pDoc As Inventor.Document, StorageName As String, StreamName As String, data As String) As Boolean
Try
' create/get storage. "true" means if create
'if it does not exists.
Dim pStg As MVOI.IStorage = DirectCast(pDoc.GetPrivateStorage(StorageName, True), MVOI.IStorage)
If pStg Is Nothing Then
Return False
End If

' create stream in the storage
Dim pStream As MVOI.IStream = Nothing
pStg.CreateStream(StreamName, CUInt(STGM.DIRECT Or STGM.CREATE Or STGM.READWRITE Or STGM.SHARE_EXCLUSIVE), 0, 0, pStream)

If pStream Is Nothing Then
Return False
End If

Dim byteVsize As Byte() = System.BitConverter.GetBytes(data.Length)
Dim byteVData As Byte() = Encoding.[Default].GetBytes(data)
Dim dummy As UInteger

' convert string to byte and store it to the stream
pStream.Write(byteVsize, CUInt(4), dummy)
pStream.Write(byteVData, CUInt(byteVData.Length), dummy)

' Save the data
pStream.Commit(CUInt(MVOI.STGC.STGC_OVERWRITE Or MVOI.STGC.STGC_DEFAULT))

'Don't forget to commit changes also in storage
pStg.Commit(CUInt(MVOI.STGC.STGC_DEFAULT Or MVOI.STGC.STGC_OVERWRITE))

' force document to be dirty thus
' the change can be saved when document
'is saved.
pDoc.Dirty = True
pDoc.Save()

'Don't forget to release the object!!
Marshal.ReleaseComObject(pStg)

Return True
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return False
End Try
End Function
' read the storge and stream
Private Function ReadPrivateStorageAndStream(pDoc As Inventor.Document, StorageName As String, StreamName As String, ByRef outDataStr As String) As Boolean
outDataStr = ""
Try
'get the storge. "false" means do not create
'if it does not exist
Dim pStg As MVOI.IStorage = DirectCast(pDoc.GetPrivateStorage(StorageName, False), MVOI.IStorage)
If pStg Is Nothing Then
Return False
End If

' open stream to read
Dim pStream As MVOI.IStream = Nothing
pStg.OpenStream(StreamName, IntPtr.Zero, CUInt(STGM.DIRECT Or STGM.READWRITE Or STGM.SHARE_EXCLUSIVE), 0, pStream)

If pStream Is Nothing Then
Return False
End If

Dim byteVsize As Byte() = New Byte(15) {}
Dim intSize As UInteger = 4

' read the stream
Dim dummy As UInteger
pStream.Read(byteVsize, CUInt(intSize), dummy)
Dim lSize As Integer = System.BitConverter.ToInt16(byteVsize, 0)

Dim outDataByte As Byte() = New Byte(8191) {}
pStream.Read(outDataByte, CUInt(lSize), dummy)

' convert byte to string
outDataStr = Encoding.[Default].GetString(outDataByte, 0, lSize)

'Don't forget to release the object!!
Marshal.ReleaseComObject(pStg)
Return True
Catch ex As Exception
MessageBox.Show(ex.ToString())
Return False
End Try

End Function