?
Solved

Iofflinescache implementation in c#

Posted on 2011-09-23
2
Medium Priority
?
1,051 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

770 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