views:

26

answers:

1

Hi there,

I used WinAPI function to create OLE file in C#. The WinAPI function is:

   [DllImport("ole32.dll")]
    public static extern int OleCreateFromFile(
        [In] ref Guid rclsid,
        [MarshalAs(UnmanagedType.LPWStr)] string lpszFileName,
        [In] ref Guid riid, uint renderopt,
        [In] IntPtr pFormatEtc,
        IOleClientSite pClientSite,
        IStorage pStg,
        [MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);

I used this function in C# application as below:

     hResult = OleAPI.OleCreateFromFile(ref clsid,
                                            sourceFileFullPath,
                                            ref IID_IOleObject,
                                            (uint)OLERENDER.OLERENDER_DRAW,
                                            IntPtr.Zero,
                                            pOleClientSite,
                                            pStorage,
                                            out pOleObjectOut);

The function works perfectly. But when the length of filename of the file is 140, it failed to create OLE file. The return value of hResult is -2147467259.

The value of sourceFileFullPath="C:\Users\Administrator\AppData\Local\Temp\TmpTzStore1\pju3wswj.jmq\000008F6\CalendarRepeddsfsfdsfsfat344Issue5sdfdfsfsfdsfdsfsdsdfdsfsf00kbCalendarRepeatIssue500kbCalendarRepeatIssue500kbCalendarRepeatIssue500kb.xlsx"

The length of filename (CalendarRepeddsfsfdsfsfat344Issue5sdfdfsfsfdsfdsfsdsdfdsfsf00kbCalendarRepeatIssue500kbCalendarRepeatIssue500kbCalendarRepeatIssue500kb.xlsx) is 140.

But when I trimmed the file name to 120, it worked.

I wanted to know if there is any limitation in the filename length for OLE file. Since I tried it for different application files( doc,docx,txt,pdf...). The results varied for different files.

I don't know what is the exact the file name length that this API supports. Or it is dependent on the OS? I am using Windows Server 2008.

Thanks Prakash

A: 

The maximum file name length of the complete filename length, including the path, is (about) 259 characters. The length of the path you pasted is 216 characters, so this should be under the limit.

That the MSDN pages do not describe a specific maximum length implies that 259 characters should be the maximum.

I could not find any documentation on this discrepancy. Maybe one or more of your paths are softlinks to path names that are in reality longer.

Pieter
Amazing thing happened. When I used the file name which have only alphabetic characters but having the length 140, it worked.
Prakash
You're kidding! You mean only A through Z?
Pieter
Yes. I used only A, B and C. Its file name is ABCABCABCABCABABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC.xlsx
Prakash
The variable thing is path. Its is temp folder generated from the code. For example, C:\\Users\\Administrator\\AppData\\Local\\Temp\\TmpTzStore0\\jhqqln14.v2o\\000008F6\. In this path, the only variable is "jhqqln14.v2o".
Prakash