Iofflinescache implementation in c#

Hey guys, I've been trying to implement IofflinefilesCache interface in c# but for some reason it keeps giving me the error "Value does not fall within the expected range." when I try to use the method GetLocation (for example)
other methods do the same

this is the website for the Iofflinefilecache http://msdn.microsoft.com/en-us/library/bb530486(VS.85).aspx

I attached my implementation in the code section and keep in mind I tried with all sorts of variables of intptr and pointers and strings :P

So, if anyone could help this noob, I'd appreciate it

 
[ComImport,Guid("48C6BE7C-3871-43cc-B46F-1449A1BB2FF3")]
    class testclass
    {
    }

    [ComImport]
    [Guid("855d6203-7914-48b9-8d40-4c56f5acffc5")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOfflineFilesCache
    {
            int GetLocation([MarshalAs(UnmanagedType.LPWStr)] out string ppszPath);  
    }

        private void button1_Click(object sender, EventArgs e)
        {
            testclass tc = new testclass();
            IOfflineFilesCache test = (IOfflineFilesCache)new testclass();
            string ppszPath;
            test.GetLocation(out ppszPath);
        }

Open in new window

ramywattsAsked:
Who is Participating?
 
nffvrxqgrcfqvvcConnect With a Mentor Commented:
Your on the right track however when you build the Interface you have to make sure the members are in the correct order. In your above example GetLocation() isn't the first entry in the v-table for that interface. You can't trust msdn to display the Interface in the correct order as it's just a help source so what you have to do is open the header file (Cscobj.h) and inspect how the members are aligned on the v-table. This ensures the method you implement in your .NET code maps to the same method in the interface. You also must create and instance OfflineFilesCache before you can request the interface. The example below shows the basic layout of the interface and how to call the GetLocation() method.
Imports System.Runtime.InteropServices
 
Public Class Form1

    Private Const CLSID_OfflineFilesCache As String = "48C6BE7C-3871-43cc-B46F-1449A1BB2FF3"

    Private ofc As IOfflineFilesCache = Nothing

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim hr As Integer
        Dim pLocation As IntPtr
        '// Request Interface
        ofc = DirectCast(Activator.CreateInstance(Type.GetTypeFromCLSID(New Guid(CLSID_OfflineFilesCache)), True), IOfflineFilesCache)
        '// Call GetLocation on Interface
        hr = ofc.GetLocation(pLocation)
        If hr = 0 Then
            Debug.Print(Marshal.PtrToStringUni(pLocation)) ' output path
            Marshal.FreeCoTaskMem(pLocation) 'free memory(according to msdn)
        End If

    End Sub
End Class



<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _
Guid("855d6203-7914-48b9-8d40-4c56f5acffc5")> _
Public Interface IOfflineFilesCache

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

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

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

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

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

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

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

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

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

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

    <PreserveSig()> _
    Function GetLocation(ByRef ppszPath As IntPtr) As Integer

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

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

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

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

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

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

End Interface

Open in new window

0
 
ramywattsAuthor Commented:
Thanks alot egl!

With your help I finally got the point... and the pointer :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.