By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Problems using IPersistFile with VB.NET

P: n/a
I'm having problems with the IPersistFile interface. We have in house
objects that support IPersistFile (and have done for years). I would like
to load an object using this interface with VB.NET (note: I'm already
successfully using our IPersistStorage interface for structured storage with
other of our file types, but these particular file types do not support
IPersistStorage). Anyway, the result I get from IPersistStorage::Load
is -2147467259, or "Unspecified Error". The software that generates, writes
and reads these files (MFC C++ program) has no problem with them. Is there
something wrong with my interface definition or usage here?

Any pointers would be great, thanks.
******************** MY WRAPPER FOR IPersistFile ***********************

<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown ),
Guid("0000010b-0000-0000-C000-000000000046")> _
Public Interface IPersistFile

<PreserveSig()> _
Function GetClassID(ByRef pClassID As IntPtr) As Integer

<PreserveSig()> _
Function IsDirty() As Integer

<PreserveSig()> _
Function Load(<MarshalAs(UnmanagedType.LPStr)> ByVal pszFilename As
String, ByVal dwMode As Integer) As Integer

<PreserveSig()> _
Function Save(<MarshalAs(UnmanagedType.LPStr)> ByVal pszFilename As
String, <MarshalAs(UnmanagedType.Bool)> ByVal fRemember As Boolean) As
Integer

<PreserveSig()> _
Function SaveCompleted(<MarshalAs(UnmanagedType.LPStr)> ByVal
pszFilename As String) As Integer

<PreserveSig()> _
Function GetCurFile(ByRef theFile As String) As Integer
End Interface

******************** THE FUNCTION USING IPersistFile ***********************

Dim Result As Boolean
Dim theData() As Byte

Dim theIPersistFile As COM.IPersistFile

Try
Dim theImage As TherMonitor.Image

' Get type ID

Dim theType As Type = Type.GetTypeFromProgID("TherMonitor.Image")

' Create a blank instance.

theImage = CType(Activator.CreateInstance(theType), TherMonitor.Image)

' Cast to IPersistFile

theIPersistFile = CType(theImage, COM.IPersistFile)

' Now atempt to open it

Dim hResult As Integer = theIPersistFile.Load(theFile, 0)

' Save from document into theData

If hResult = 0 Then

..............
..............

' Success

Result = True
Else

' Failed

Result = False
End If

Catch ex As Exception

' Failed

Result = False

Finally

End Try

' Return the result...

Return Result
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I'm not sure if the definitions the problem, but if it is, it would be
easier for you to use System.Runtime.InteropServices.UCOMIPersistFile... It
should be what you want. :)

Dan

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cd*******************@news.demon.co.uk...
I'm having problems with the IPersistFile interface. We have in house
objects that support IPersistFile (and have done for years). I would like
to load an object using this interface with VB.NET (note: I'm already
successfully using our IPersistStorage interface for structured storage with other of our file types, but these particular file types do not support
IPersistStorage). Anyway, the result I get from IPersistStorage::Load
is -2147467259, or "Unspecified Error". The software that generates, writes and reads these files (MFC C++ program) has no problem with them. Is there something wrong with my interface definition or usage here?

Any pointers would be great, thanks.
******************** MY WRAPPER FOR IPersistFile ***********************

<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown ),
Guid("0000010b-0000-0000-C000-000000000046")> _
Public Interface IPersistFile

<PreserveSig()> _
Function GetClassID(ByRef pClassID As IntPtr) As Integer

<PreserveSig()> _
Function IsDirty() As Integer

<PreserveSig()> _
Function Load(<MarshalAs(UnmanagedType.LPStr)> ByVal pszFilename As
String, ByVal dwMode As Integer) As Integer

<PreserveSig()> _
Function Save(<MarshalAs(UnmanagedType.LPStr)> ByVal pszFilename As
String, <MarshalAs(UnmanagedType.Bool)> ByVal fRemember As Boolean) As
Integer

<PreserveSig()> _
Function SaveCompleted(<MarshalAs(UnmanagedType.LPStr)> ByVal
pszFilename As String) As Integer

<PreserveSig()> _
Function GetCurFile(ByRef theFile As String) As Integer
End Interface

******************** THE FUNCTION USING IPersistFile ***********************
Dim Result As Boolean
Dim theData() As Byte

Dim theIPersistFile As COM.IPersistFile

Try
Dim theImage As TherMonitor.Image

' Get type ID

Dim theType As Type = Type.GetTypeFromProgID("TherMonitor.Image")

' Create a blank instance.

theImage = CType(Activator.CreateInstance(theType), TherMonitor.Image)

' Cast to IPersistFile

theIPersistFile = CType(theImage, COM.IPersistFile)

' Now atempt to open it

Dim hResult As Integer = theIPersistFile.Load(theFile, 0)

' Save from document into theData

If hResult = 0 Then

..............
..............

' Success

Result = True
Else

' Failed

Result = False
End If

Catch ex As Exception

' Failed

Result = False

Finally

End Try

' Return the result...

Return Result

Nov 20 '05 #2

P: n/a
Ahhhh ok I hadn't realised we had a wrapper already with .net. thanks will
give it a go.

"Daniel" <da******@hotmail.com> wrote in message
news:Oe**************@TK2MSFTNGP11.phx.gbl...
I'm not sure if the definitions the problem, but if it is, it would be
easier for you to use System.Runtime.InteropServices.UCOMIPersistFile... It should be what you want. :)

Dan

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cd*******************@news.demon.co.uk...
I'm having problems with the IPersistFile interface. We have in house
objects that support IPersistFile (and have done for years). I would like to load an object using this interface with VB.NET (note: I'm already
successfully using our IPersistStorage interface for structured storage

with
other of our file types, but these particular file types do not support
IPersistStorage). Anyway, the result I get from IPersistStorage::Load
is -2147467259, or "Unspecified Error". The software that generates,

writes
and reads these files (MFC C++ program) has no problem with them. Is

there
something wrong with my interface definition or usage here?

Any pointers would be great, thanks.
******************** MY WRAPPER FOR IPersistFile ***********************

<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown ),
Guid("0000010b-0000-0000-C000-000000000046")> _
Public Interface IPersistFile

<PreserveSig()> _
Function GetClassID(ByRef pClassID As IntPtr) As Integer

<PreserveSig()> _
Function IsDirty() As Integer

<PreserveSig()> _
Function Load(<MarshalAs(UnmanagedType.LPStr)> ByVal pszFilename As
String, ByVal dwMode As Integer) As Integer

<PreserveSig()> _
Function Save(<MarshalAs(UnmanagedType.LPStr)> ByVal pszFilename As
String, <MarshalAs(UnmanagedType.Bool)> ByVal fRemember As Boolean) As
Integer

<PreserveSig()> _
Function SaveCompleted(<MarshalAs(UnmanagedType.LPStr)> ByVal
pszFilename As String) As Integer

<PreserveSig()> _
Function GetCurFile(ByRef theFile As String) As Integer
End Interface

******************** THE FUNCTION USING IPersistFile

***********************

Dim Result As Boolean
Dim theData() As Byte

Dim theIPersistFile As COM.IPersistFile

Try
Dim theImage As TherMonitor.Image

' Get type ID

Dim theType As Type = Type.GetTypeFromProgID("TherMonitor.Image")

' Create a blank instance.

theImage = CType(Activator.CreateInstance(theType), TherMonitor.Image)
' Cast to IPersistFile

theIPersistFile = CType(theImage, COM.IPersistFile)

' Now atempt to open it

Dim hResult As Integer = theIPersistFile.Load(theFile, 0)

' Save from document into theData

If hResult = 0 Then

..............
..............

' Success

Result = True
Else

' Failed

Result = False
End If

Catch ex As Exception

' Failed

Result = False

Finally

End Try

' Return the result...

Return Result


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.