WCF Com Interop GetRecordInfoFromGuids Returns Old Format Or Invalid Type Library

0

I have a struct in a WCF Service defined in C# as

   [DataContract]
   [StructLayout(LayoutKind.Sequential), Serializable]
   [ComVisible(true)]
   public struct MyData
   {
      [DataMember]
      public int data1;

      [DataMember]
      public string data2;
   }

From a MFC app I am trying to create a SafeArray of this struct. When calling GetRecordInfoFromGuids like this

hr = GetRecordInfoFromGuids(LIBID_MyLib, 1, 0, LOCALE_USER_DEFAULT, __uuidof(MyData), &pRI);

I am getting a return value of

0x80028019 Old format or invalid type library. 

What is wrong with this?

c#
c++
com-interop
safearray
asked on Stack Overflow Jul 13, 2015 by TimR75

1 Answer

2

I managed to fix this and as I found 3 or 4 identical questions searching Google and none of them had an answer, I thought I would update mine so it was the one that did actually have a solution.

It turned out that the string data member was being marshalled by default as LPSTR. It never occurred to me that this might be a problem and in fact there is no documentation to state that it might be. When passed as a single object there is no problem. However, it turns out that when passing an array of these objects the string member must be marshalled as BSTR. Otherwise you get the error in GetRecordInfoFromGuids that the type library is not valid. There is really no documentation that hints that this is the reason why this call returns type library not valid. It was merely by trial and error that I found the problem.

So the above code just needed to be altered to this

   [DataContract]
   [Guid("xxx")]
   [StructLayout(LayoutKind.Sequential), Serializable]
   [ComVisible(true)]
   public struct MyData
   {
      [DataMember]
      public int data1;

      [DataMember]
      [MarshalAs(UnmanagedType.BStr)]
      public string data2;
   }

Now GetRecordInfoFromTypeInfo succeeds and I can create and pass the data successfully.

answered on Stack Overflow Jul 14, 2015 by TimR75 • edited Jul 14, 2015 by TimR75

User contributions licensed under CC BY-SA 3.0