title: How can I use a User Defined Type (UDT) in a COM server?
body: <p>I have a COM server with a method currently returning an integer:</p>
  
  <pre><code>[
      object,
      uuid("..."),
      dual,
      helpstring("IMyCOMServer Interface"),
      pointer_default(unique)
  ]
  __interface IMyCOMServer : IDispatch
  {
      [id(1), helpstring("method MyQuery")]
      HRESULT MyQuery([in] BSTR instr, [out,retval] int* outint);
  };
  </code></pre>
  
  <p>This compiles fine, but I'd rather return an enum: <em>(this code is actually above the interface definition)</em></p>
  
  <pre><code>typedef
  [
      uuid("..."),
      v1_enum,
      helpstring("Enum")
  ]
  enum {
      value_a,
      value_b,
      value_c
  } MyEnum;
  </code></pre>
  
  <p>Which again compile fine of its own right, but as soon as I change the <code>int*</code> to <code>MyEnum*</code> in the interface and implementation, I get linker errors:</p>
  
  <pre><code>[id(1), helpstring("method MyQuery")]
  HRESULT MyQuery([in] BSTR instr, [out,retval] MyEnum* outint);
  
  error MIDL2025 : syntax error : expecting a type specification near "MyEnum"
  </code></pre>
  
  <p>Whichever way I do it, I can't get it to compile.</p>
  
  <hr>
  
  <p>Thanks to <a href="https://stackoverflow.com/users/2230/euro-micelli">Euro Micelli</a> it turns out that the real problem is that my User Defined Type (the enum) wasn't making it into the generated .IDL file. Judging by forum queries online, this seems to be a common problem.</p>
  
  <p>A blog article <a href="http://startech-journey.blogspot.com/2007/06/udt-user-defined-types-and-com.html" rel="nofollow noreferrer">Star Tech: UDT (User Defined Types) and COM</a> guided me down the right path. It seems that a workaround is needed when using attributed ATL.</p>
  
  <p>In summary, I made the following changes:</p>
  
  <p>Created <code>udt.idl</code>:</p>
  
  <pre><code>import "oaidl.idl";
  import "ocidl.idl";
  
  [
      uuid("..."),
      v1_enum,
      helpstring("Enum")
  ]
  typedef enum MyEnum {
      value_a,
      value_b,
      value_c
  } MyEnum_t;
  
  [
      version(1.0),
      uuid(...),
      helpstring(...)
  ]
  library MyLibrary
  {
      enum MyEnum;
  }
  </code></pre>
  
  <p>Added the following line prior to the module attribute in the main <code>.cpp</code> file so that the above IDL gets imported into the generated file:</p>
  
  <pre><code>[importidl("udt.idl")];
  </code></pre>