Solved

Iofflinescache implementation in c#

Posted on 2011-09-23
2
1,010 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 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

21 Experts available now in Live!

Get 1:1 Help Now