Home > COM > MIDL “string” attribute

MIDL “string” attribute

Sometime back I was wondering about whether there was a way to transfer our good ol’ NULL terminated strings using COM apart from using BSTR’s.

That’s how I bumped into “string” attribute provided by MIDL.

Here is how we use it…

typedef [string] char NormalString[1024];

[id(1), helpstring( "Displays null terminated UNICODE strings" )]
HRESULT ShowMsgUnicodeString( [in,string] wchar_t* wCharString_i );

[id(2), helpstring( "Displays null terminated ANSI strings" )]
HRESULT ShowMsgAnsiString( [in,string] char* cString_i );

Well if we don’t specify “string” then MIDL treats the parameter as an address of a wchar/char.

Callers can invoke the call as they would normally do with a function.

  1. ashish
    April 22, 2010 at 4:54 pm


    With regard to
    typedef [string] char NormalString[1024];
    Can you please give an example, how we can use the NormalString as a method argument.


    • April 22, 2010 at 6:20 pm

      Hi Ashish,

      This used to work for me. I’ve got to recheck as to what’s going on. 🙂

  2. k62o
    June 17, 2009 at 9:09 pm

    Yes, but you have to be very carefull, this doesn’t work with cross-apartement calls

    • Ashish
      April 21, 2010 at 6:03 pm

      Hi Nibu

      I am facing problem using the string attribute. As you have mentioned, I used the string attribute in the typedef for the char array. Then used that typedef in the method as out parameter. Below is the snippet of the idl file:

      typedef [string] char NormalString[1024];
      HRESULT GetString([out, string] NormalString value);

      when I compile this idl using midl, the tlb being generated is having invalid method signature.

      Could you please let me know if I am doing something wrong. It will be great if you can help resolve this problem.

      Thanks a lot in advance.

      • April 22, 2010 at 6:22 pm

        I guess it should be

        typedef [string] char NormalString[1024];
        HRESULT GetString([out, string] char* value);

        That ‘typedef’ is just a demo as to how to typedef a char[] in an midl file. Also I’m not sure how it will behave as an out parameter. I’ve tested only with ‘in’ parameter.

        • Ashish
          April 23, 2010 at 12:01 am

          Thanks Nibu for your reply.
          I tried compiling as suggested but it failing giving errors.
          Although i am able to compile the idl if i use [in] attribute, but it looks like it is not considering the size specified in typedef in that case.

          Anyways, thanks once again. If you come to know how actually this sort of typedef works, do let me know.


          • April 23, 2010 at 11:03 am

            Forget the typedef ashish. That’s not related to the string attribute but a demo to show how ‘string’ can be used to typedef. I’ll let you know once I find out about this.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: