0

I cannot find any documentation on how to open MS Project 2010 files using IStorage::OpenStorage. I do not know what to pass for the OLECHAR *pwcsName argument for msproject 2010.

The following code opens an MS Project 2007 file, but fails to open an MS Project 2010 file:

hr = pStorage->OpenStorage(L"   212",NULL,STGM_READ | STGM_SHARE_EXCLUSIVE,
                           NULL,0,&pAuxStorage);

What do I have to pass for the first parameter to open an MS Project 2010 file?

Jon Iles
  • 2,519
  • 1
  • 20
  • 30
Biryukov Pavel
  • 397
  • 4
  • 18

2 Answers2

0

L"foo" is NOT acceptable for passing to functions expecting OLECHAR.

You must pass a string that has been allocated by the SysAllocString family of functions.

These strings have a special memory layout and are allocated using the COM allocator. Although the data type is wchar_t in both cases, the use of the name OLECHAR indicates that the function being called is expecting one of these special strings. Link to related question

In C your code could be:

BSTR b = SysAllocString(L"   212");
hr = pStorage->OpenStorage(b, NULL, ......);
SysFreeString(b);

In C++ you could use an RAII wrapper to allocate and free the string, although this isn't standard across compilers. See here or here for discussion.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • I don't disagree but are you saying that's directly related to the stated problem? – Jonathan Potter Jul 07 '15 at 07:57
  • @JonathanPotter OP is not sure what to pass for the first parameter and showed an example that seemed to give unexpected results, so I have explained how to properly pass a string to the first parameter – M.M Jul 07 '15 at 08:36
  • Again I don't disagree, but given that the OP seems to have it working for MS Project 2007 and his question is specifically about Project 2010 I just wonder if your answer (while factually accurate) actually addresses the question. – Jonathan Potter Jul 07 '15 at 08:45
  • 1
    @JonathanPotter it may have been a coincidence (UB is UB) or perhaps Project 2007 does a zero-terminated wcslen but Project 2010 validates the length. Only OP can reveal whether this answers the question or not – M.M Jul 07 '15 at 12:22
  • Guys the example that i uploaded works! you can argue as much as you like the question was, did some one reverse engineered the 'MS Project 2010' file to give me the 'pwcsName' that will cause the 'OpenStorage' function to open the file and not fail it. – Biryukov Pavel Jul 07 '15 at 15:40
  • @user3374456 using a `L` literal as the argument is incorrect . I'm not sure what you are trying to say after that – M.M Jul 07 '15 at 15:47
  • @user: You might want to start by providing the error code you get. Other than that, if the code you posted does appear to work with MS Project 2007 files, it does so by **coincidence**, and coincidence alone. You are passing the wrong datatype for the first parameter. This is a bug. Arguing why erroneous code appears to work under certain circumstances is useless. You need to fix your code. – IInspectable Jul 07 '15 at 18:13
0

For an MPP file written by MS Project 2010 or later I think you'll need the following:

hr = pStorage->OpenStorage(L"   214",NULL,STGM_READ | STGM_SHARE_EXCLUSIVE,
                           NULL,0,&pAuxStorage);

Note the 212 changes to 214. The file format changed between Project 2007 and Project 2010.

Jon Iles
  • 2,519
  • 1
  • 20
  • 30