Solved

Iofflinescache implementation in c#

Posted on 2011-09-23
2
1,014 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ramywatts
2 Comments
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 36592475
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
 

Author Comment

by:ramywatts
ID: 36592643
Thanks alot egl!

With your help I finally got the point... and the pointer :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now