FIX: Platform SDK Causes MIDL to Throw MIDL2311 Error

Symptoms

When you use the Microsoft Interface Definition Language (MIDL) with the /mktypelib203 switch to compile an .odl file, MIDL reports the following error:

MIDL2311 : statements outside library block are illegal in mktyplib compatibility mode.
This happens when you use SDK headers that are included in the July 1999 Platform SDK. When the INCLUDE path points to Visual C++ 6.0 headers first, this problem does not occur and the application builds.

Cause

This problem may occur if all of the following conditions are met:
  • There is a bug in the MIDL code specific to /mktyplib203 processing. The

    #pragma once
    in the SDK header files, Olectl.h, is not recognized by MIDL when the /mktyplib203 switch is on.
  • The Tool wizard defaults to /mktyplib203, which is unnecessary. (Do not use /mktyplib203 unless you are dealing with code dating back to mktyplib version 2.03, which does not compile with a newer MIDL compiler.)
  • A bug exists in the SDK Olectl.h file that does not hide the section #pragma once properly.

Resolution

There are four possible solutions for this problem:

  • Install the latest Platform SDK available at:
  • Do not use the /mktyplib203 switch unless you have to deal with legacy code dating back to mktyplib ver.2.03.
  • In the .odl file, put the following inside the library block:
     #include <olectl.h> 
  • In the SDK Olectl.h file, move the section with #pragma once inside the part of the file invisible to MIDL or mktyplib.

    The SDK olectl.h file looks like this:
    #ifndef _OLECTL_H_
    #define _OLECTL_H_

    #if _MSC_VER > 1000
    #pragma once
    #endif

    #if !defined(__MKTYPLIB__) && !defined(__midl)
    // most of the file
    #endif

    // some #defines
    #endif

    Change it to:
    #ifndef _OLECTL_H_
    #define _OLECTL_H_

    #if !defined(__MKTYPLIB__) && !defined(__midl)

    #if _MSC_VER > 1000
    #pragma once
    #endif

    // most of the file
    #endif

    // some #defines
    #endif

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in the January 2000 Platform SDK.

More Information

Steps to Reproduce Behavior

  1. Install Visual C++ 6.0 Service Pack 3.
  2. Install the Platform SDK, July 1999, from MSDN.
  3. Start Visual C++.
  4. From the File menu, select New.
  5. On the Projects tab, select MFC ActiveX ControlWizard. In the Project Name edit box, type the name of the project, for example:
    MFCTest
  6. Click OK.
  7. Click Finish.
  8. Finally, from the Build menu, select Build <projectname> to build the project.
The following error message appears in the .odl file:

file.odl(1) : error MIDL2311 : statements outside library block are illegal in mktyplib compatibility mode : [ ]

References

For additional information on searching the Knowledge Base, click the article number below to view the article in the Microsoft Knowledge Base:

242971 HOWTO: Use the Developer Support Microsoft Knowledge Base
Svojstva

ID članka: 243219 - posljednja izmjena: 24. ožu 2009. - verzija: 1

Povratne informacije