Solved

Iofflinescache implementation in c#

Posted on 2011-09-23
2
1,038 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

688 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