Tuesday, June 15, 2010

Message Compiler errors and how I solved them

As part of my day job, for the last several weeks I have written a Windows Service in .Net 3.5. Today I did some clean-up of messages written to the EventLog. Because internally there are several parts to this service, I wanted to separate the messages using the Category field. After some searching of the web, it seems this isn't drop dead trivial. But, as it didn't seem terribly complex either, I decided to dive into the task.

One of the requirements is that the text for the categories are in a resource DLL. Unfortunately, these aren't natively supported by the .Net framework. You have to manually create the files, compile them with two compilers and then link it into the DLL. Even though it consisted of several steps, it still seemed pretty straight forward based on the details on MSDN. Following the instructions, I created a simple .mc file and tried compiling it with mc.

The Message Compiler emitted a bunch of errors:
  • invalid character (0x29)
  • invalid character (0x57)
  • invalid character (0x10)
  • Invalid message file token
Obviously something was wrong. Another web search revealed nothing. I tried to make minor changes to the file, reduced things to the bare minimum, but continued to get these errors. It seemed I had some odd characters in the file that none of the the editors I used revealed. Finally I used the command line "type" command and there they were: the Byte Order Marks. Arg. After some head scratching, I used the old text mode "edit" command and it revealed the characters. I easily deleted them and tried to compile. It worked! No errors!

Of course I really wanted to edit the file in Visual Studio and so made a change to the newly edited file and saved it. VS kindly put the BOM right back in. It can be so annoyingly helpful. Yet more searching for a means of eliminating the BOM didn't reveal anything that I wanted to use. I found a script or two that I could install in the IDE, but I didn't want to force the rest of the team to have to do this. The more environmental things that have to be configured and maintained, the harder it is to setup a new development machine. After a bit of pouring through VS's Tool | Options, I found Text Editor | File Extensions. This allows you to define which editor to use for a given file type. I added the .mc extension and told it to use the User Control Editor. Sure enough, this editor didn't put the BOM in the file. While not ideal for the team environment, it is better than a script.

So, these were some of the lessons I learned today working with Message Text Files and the Message Compiler. I hope it helps someone else.


Inventory Management Software said...

Thanks for sharing your post and it was superb .I would like to hear more from you in future too.
Inventory Management Software

Harley Pebley said...

Thanks. I'm glad you found it useful.

Tom said...

This doesn't seem to work for me (in Visual Studio 2010). The User Control Editor does the same thing. I can clear out a .mc text file with "cat NUL: >test.mc" and it will run through the compiler, but as soon as I reload it (an empty file!) in VS2010 and hit Ctrl+S to save it (again, empty)... it adds the characters back in. I'm thinking I'm going to have to write a console app just to strip them out! By the way, before I found your post I too resorted to the old DOS edit command to create a clean file. I wonder how many programmers these days even know about edit.

Tom said...

Finally found out how to get VS2010 to save without BOM. You do a "Save As" and click the drop-down on the Save button. Select "Save with Encoding..." then change the Encoding to "Unicode (UTF-8 without signature) - Codepage 65001" and click Ok. FINALLY!

Harley Pebley said...

Cool. Thanks for the update Tom.