Problems using IPersistFile with VB.NET

  • Thread starter Thread starter Robin Tucker
  • Start date Start date
R

Robin Tucker

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
 
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
 
Ahhhh ok I hadn't realised we had a wrapper already with .net. thanks will
give it a go.
 
Back
Top