Monday, June 2, 2008

Hacking VS C# 2008 Express

VS C# 2008 Express is a really nice product in many ways - you've got most of the stuff you need, but most of all, it's free!

Though, I was playing around for it a bit, and needed to support compiling against a specific platform (x86 instead of Any CPU which is default). The reason for this was that, in my project, I was refering to a dll written in managed c++ - compiled for x86! Mixing x64 and x86 is never a good solution, and as C# projects per default compiles against Any CPU they will run in 64-bits mode on a 64-bit computer. When the application tries to load the 32-bit dll, the application crashes...

Now, in VS C# 2008 Express, you cannot specify another target platform but the Any CPU platform. Guess they think that "if you need support for specific platforms, you should be able to pay for a commercial version".

I was reading through MSDN, and found this note:
Note /platform is not available in the development environment in Visual C# Express.

"Damn", I thought, "this is the end. But what does development environment mean?". I had to try it out manually using the command prompt, so I compiled the project in VS and copied the command line output from the output window, being something like:
C:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 ...
I put the line in a batch file, and added the /target:x86 flag, compiled it and... it worked! The project got compiled using x86 as a target platform.

As it turns out, VS C# 2008 Express does support the /target option, and probably others as well. They only lack the support for the option in the IDE, which is kinda a hacky way of disabling a feature, IMO.

Now, I don't want to build the project using a batch file every time, I would like to build it inside VS IDE so that others can easily build it. As the VS project files are nothing but XML, you can easily open them up in any editor you like to see their contents. I did this to find out if there was some way to add a target flag to the compiler, but unfortunately I couldn't find anything interesting (of course that would be too easy!). I continued with searching the web, and found this strange property:
CSharpProjectConfigurationProperties3.PlatformTarget Property
This member provides internal-only access to C# project configuration properties.
...
External components can access these properties through the Properties collection for the appropriate Visual Studio automation object.
"Well", I said to my self, "the Properties collection would probably refer to the XML properties found in the project file, so once again I opened up the project file, and browsed to two property groups being:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
and
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
In these groups, I added the PlatformTarget property:
<PlatformTarget>x86</PlatformTarget>
I saved the file, reloaded it in VS, recompiled the project... and viola, it worked! All you needed to do was to find out the magic property to add to the project file! :)

This proves that C# Express contains more under the hood than what's visible to the eye. With a simple editor, and some knowledge on how to search MSDN, you can get a lot out of this product!

19 comments:

Unknown said...

Very nice indeed!

What project is that? The g4m3 with asm0?

Christian Genne said...

No, an 3xpl0r3r context menu integrator for perforce support (similar to TortoiseSVN).

Anonymous said...

I stumbled upon the same problem with my app and searched for a solution. Changing the target platform is possible in VS C#2008 by the configuration manager (the comboboxes right to the compile button). There I could add the x86 platform. after doing that I could specify output paths and so on in the project properties.

Christian Genne said...

Yes, but you are referring to the commercial version, right? The express version, as far as I know, don't have this combobox... :(

Florin said...

Thank you! This saved my day.

Jared said...

Your are awesome!

I've been working on this for a while, to connect an MS Access DB, and your post did the trick!

Thanks a bunch!

Unknown said...

I found something interesting. After changing the values mentioned, when I went back into the VC# 2008 environment I had access to the Configuration Manager under the build menu!

Thanks for the great tip.

Unknown said...

Thanks so much!! Worked for me no problem.

Name: Andrew said...
This comment has been removed by the author.
Name: Andrew said...

Thanks, this seemed to work! A couple of notes:

a) The file to add this new XML property to is in the project's main csproj file (yourprojectname.csproj). Open it in a text editor.

b) You add this property to the file two times, once within the XML node mentioned by the author, and the identical XML node right under it. You'll see the same thing two times.

c) You might want to try changing references in the two nodes, from "AnyCPU" to "x86" if your file has references to it.

d) When you load up the project again, it might not compile / build. You'll see a new toolbar in VS 2008 though, at the top of the program, which seems to pop up when you add this new config option in the XML file (cool!) Click on "Any CPU" in this new toolbar, select "Configuration Manager", find the "Platform" dropdown, and select x86 in the resulting dialog.

I found it interesting that VS C# 2008 is so hackish that simply adding a XML string to this file gives you an entirely new toolbar in the program. Haha.

Bearishtrader said...

Great trick, works like a charm! TY

viagra online said...

I found something interesting. After changing the values mentioned, when I went back into the VC# 2008 environment I had access to the Configuration Manager under the build menu!

bLUeWave said...

I have found another solution in Visual C# Express 2010

Go to main menu:
Tools -> Options
Select from left tree view:
Projects and Solutions -> General
Enable Show advanced build configurations

And now in IDE you find Solution COnfiguration dropdown list and solution platforms dropdown list

Viagra said...

Great tutorial!

Elliott Broidy said...

That is a great source of information.

Anonymous said...

Wow great information about toolbar development. Hire toolbar developer exhaustive analysis is capable of reshaping toolbar solutions with accuracy and precision. Here, cutting-edge technology in collaboration with cost-efficient methods is the stand-out corner for toolbar developer.

Albendazol 400 mg kaufen said...

That's amazing. I couldn't believe that it is possible

Bimatoprost Wimpernserum said...

Does it really work? Is there any commercial version of that?

Tizanidine 4 mg said...

Great website, continue the Excellent work!