Category: .NET

22 Nov

Porting .NET app to Vista.. DEP strikes!

STOP

An interesting issue happened these days when I tried to port some apps to Vista. Basically all .NET apps compiled with Visual Studio 2005/2008 are marked “NX compatible” by default. If your .NET app uses an incompatible DLL or COM object, the app will crash. What I found funny was that the message was a standard access violation error instead of a more specific DEP error. Debugging it was not easy: I was unable to step through the code in VS2005 and using WinDbg wasn’t much of help too, except that the line where it crashed was something like a mov [esp+24h], constant with ESP well within limits — an instruction which should not generate an access violation exception given the current ESP value.  At that point I was starting to think that my “attempt to read or write protected memory“ was, in fact, something else. Luckily my mind went to DEP and in less than 1 minute of Google search I was able to find this link with a good solution which, for the lazy and to preserve history is to add the following two lines as a post-build step. call “$(DevEnvDir)..\tools\vsvars32.bat” editbin.exe /NXCOMPAT:NO $(TargetPath) I tried, without much faith, and it worked. And the C# app was finally working on Vista. And while you are fuddling with post-builds, go on and enable LARGEADDRESSAWARE while you are at it. 32bit users will thank you.

(more...)

21 Nov

TargetInvocationException in asynchronous web service call

I’ve seen so many solutions around to solve a TargetInvocationException raised by an asynchronous web service call in .NET and they are all but satisfactory. Some go so far to create a worker thread which is overkill and, above all, a clear sign of cargo culting. The solution is very very simple. The exception is raised if a call fails due to a network problem, and you have accessed the Result properties of the completion event arguments. Simply check if the Error property is null before accessing the Result property (it’s this access which raises the exception).

(more...)

Filed under: .NET, Programming

16 Apr

A little better MeasureString…

System.Drawing.MeasureString is a shit. You are better work it out with API calls and so on, but if you are on Mono that is out of choice. Apart from the known problems (not working correctly without Antialiasing enabled, requiring specific StringFormats), it also skips trailing spaces. At least this should fix that issue up: int CountTrailingSpaces(string str) { int spaces = 0; int i = 0; for (i = 0; i < str.Length; i++) { if (str[i] == ‘ ‘) ++spaces; else break; } for (int j = str.Length – 1; j > i; j–) { if (str[j] == ‘ ‘) ++spaces; else break; } return spaces; } SizeF MeasureString(Graphics gfx, string str, Font font) { // these are the times you hate MS.. SizeF size = gfx.MeasureString(str, font, new PointF(0.0f, 0.0f), StringFormat.GenericTypographic); if ((str.EndsWith(” “)||(str.StartsWith(” “)))) { SizeF spacesize = gfx.MeasureString(“! !”, font, new PointF(0.0f, 0.0f), StringFormat.GenericTypographic); SizeF spacesizemin = gfx.MeasureString(“!!”, font, new PointF(0.0f, 0.0f), StringFormat.GenericTypographic); float spacewidth = spacesize.Width – spacesizemin.Width; int spaces = CountTrailingSpaces(str); size.Width += (spacewidth*(float)spaces); } return size; }

(more...)

Filed under: .NET, Code Snippets, Programming