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…

[sourcecode language=’cpp’]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 );[/sourcecode]

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.

7 thoughts on “MIDL “string” attribute

    • 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.

      • 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.

        • 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.

          Cheers
          Ashish

          • 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. Hi

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

    Thanks
    Ashish

Appreciate your comments...