Showing posts with label microsoft. Show all posts
Showing posts with label microsoft. Show all posts

Wednesday, November 18, 2009

Strange missing feature in CSharpCodeProvider

Currently I'm developing a database application for my university study that uses NHibernate. To enable lazy loading of results it is required for all members of mapped class to be virtual, so NHibernate can create proxy that will be replaced by actual object when required. And if class has events they also must be virtual because actually this is the same fields and methods.

On the other hand I've decided to generate those classes from XML definitions. On of the reasons for this is that this classes seems to be just and a set of properties, but they need to implement INotifiyPropertyChanged. I've decided to write a code generator using System.CodeDom members and to gain some experience at working with members of this namespace. However I've found that CSharpCodeProvider doesn't generate virtual events! Despite the fact that I set the appropriate attribute the event is still normal so NHibernate couldn't generate proxy. I've search through CSharpCodeProvider with Reflector and found that in the method GenerateEvent that generates code of event there is no call of a method OutputVTableModifier that inserts "virtual" keyword. VBCodeGenerator also doesn't generate virtual events.

So I wonder whether this is an intentional decision or the bug in the current .NET framework version (I have 3.5 SP 1). Actually I wasn't determined enough to search for this in the older version of .NET.

Tuesday, May 26, 2009

Visual C++ Debug Executables On Nondeveloper Machines

Well, If you need only facts then: Debug builds in Visual C++ include dependency on Microsoft.VC90.DebugCRT.dll which is installed with Visual C++ environment. This builds will not run on client machines even with Visual C++ Runtine Redistributable installed. Only Release build will run on such machines.
And here is the history how did I found this.
Recently I found that I couldn't start my application written in C++ on my netbook. At first I thought that this was the problem with hardware: program was compiled on 64-bit OS and machine and I was trying to run it on 32-bit machine. However I've tried to run program on another 32-bit machines and an attempt was succesfull. But on the forth machine it again failed to start. And by the way it provided a rather meaningless message which suggested to reinstall the problem. In .NET applications such an error is displayed if *.config file of executable has en error in XML syntax. but the was no any *.config file in this application.
I've assumed that there are missing dll file. So I've installed the only dependency that could be in this simple application: Microsoft Visual C++ 2008 Runtime Reditributable. That didn't helped. I've installed 2008 SP1 version. The same result. So I've tried to read the manifest file and found the dependency on Microsoft.VC90.DebugCRT.dll. I've compiled my application as a Release build and manifest included dependency on Microsoft.VC90.CRT.dll and application was starting on all required machines! It seems that debug vresion of C Runtime is distributed only with developer environment.
So the summary: Visual Stusio (or at least Visual C++ Express) is required to be installed to run Debug builds of programs.

Monday, May 26, 2008

NAnt over MSBuild

While working on my C# projects I found it useful for me to start using a build tool. Visual Studio builds assembly files, but many other tasks such as generating documentation are not part of assembly build and can not be just added to "pre-build" and "post-build" actions. So I've decided to use NAnt.

After some working with NAnt I decided to try MSBuild. There were some reasons for that: MSBuild is a part of .NET Framework so there is no extra dependencies; moreover it provides a good integration with project files generated by VS. But after a day of converting my NAnt project to MSBuild I've decided to return back to NAnt. There a plenty of reasons for that, which I found during this day.

The most important (for me) is that almost all settings to MSBuild tasks are set through attributes. The question "What to use: attributes or elements?" is long living and still doesn't have a clear answer (e.g. see this and this articles), but I found it a little inconvenient to set up all parameters of task through attributes. I won't speak here about what as I think should be element or property, but properties with long length data doesn't look good (e.g. lists of files).
In NAnt I feel very comfortable with setting tasks up through elements and attributes and in most cases I think that type of XML node is chosen right for property. And what is more NAnt provides and ability to define in custom tasks to define what property should be: attribute or element, while MSBuild follows it's convention and all properties are set up through attributes. Quite inflexible, though it removes a headache of deciding of type of node.

Secondly writing long lists or properties is a little inconvenient in VS: I prefer to have lines of code not very long and I need to write attributes on several lines. And if I press Enter after a property on a new line VS doesn't suggest me a list of possible items. It is need to start typing on previous line and press Enter only after writing property in editor. Of course I can write all in one line and use Wrap lines ability of text editors, but I prefer not to do so.

All-in-all I've returned to NAnt as a build tool. Sometimes I write additional tasks for it (those that are not in NAnt.Contrib) which gives me an ability to use not only for application builds but for other purposes.

Links:

Sunday, December 09, 2007

Funny Bug in Visual Studio 2008 Beta

While uninstalling MS Visual Studio 2008 Beta 2 I found one funny bug.



Trivial message from uninstaller that indicates that one or more of the files or directories to remove are occupied. The only thing to do is too close this application and click Retry. If this doesn't help, well click Ignore and hope this will not have any negative effect. But... what the heck is application named 4812? Well, it's may be not a name but an ID of process so I open Task Manager and look up for a process with ID 4812. Here it is!



Oops. It is a setup application itself blocked something and couldn't delete it. I tried to click "Retry". And it was a successful attempt process of uninstaller continued.

I even could make a guess why this short story happened but had a good final. While checking files/directories to remove setup.exe locked them. According to my guess releasing of lock is placed in destructor. When message appeared object that locked files was "deleted" but garbage collector wasn't called until that moment so hadn't called destructor. While I was thinking about this problem and taking screenshots garbage collector was invoked and called destructor that released locks. Of course this guess can be true only if setup.exe is using garbage collector. Well, as Andrew Troelsen in his book about C# suggested it is better to manually call method Dispose and release all resources when they already could be released and don't leave this job to GC, which is unpredictable.

Of course my suggestion is only a suggestion, but I think it is rather useful to try to explain bugs in not your applications even if source code is inaccessible.

Wednesday, December 05, 2007

Visual Studio 2008

In the end of November MS released the new version of its IDE Visual Studio 2008. I've used Beta version already. It that post I've written that first impression was that is is very buggy, and later practice showed that there are some mistakes. I usually had some problems with Intellisense and IDE crashed. I have to note that I used Professional Edition.

Now I'm using Express Edition. I'm not so rich to buy IDE now, so use free edition. Of course it's not so functional as Pro version and for each language (C#, C++, VB.NET, Web development) there is it's own IDE. However I'm currently programming only on C# so this is not a big problem for me. Visual Studio is rather comfortable IDE (especially comparing to Turbo C we have in unversity :(. The problem is that it can't be used for editing all types of files I need with syntax coloring. So for editing files such as Nullsoft installation scripts, Python files and others I use PSPad. But for C# Visual Studio is much more preferable because autocompletion, Intellisense and many other small but very useful features.

One interesting note about Visual Studio: with IDE many other components are installed (SQL Server Compact, etc.). But uninstalling them is a manual operation. Not rather comfortable.

I have to say that Visual Studio is one of my favorite MS products and I haven't a lot of complains about it.

Sunday, October 07, 2007

Implementing custom tool in Visual Studio 2008

I've recently wrote about making tool to generate C# code. We may say that are macros for C# (it fact it is only one defined macro — for fields and properties).

And how to implement this tool? There can be two ways: write tool on scripting language that generates C# code file and is called before compilation. Another way is to use ability of Visual Studio to switch user custom tool for generating files. Firstly I've implemented tool by first way on Python and now I want to make a custom tool for Visual Studio written on C#. But how to plug custom tools in Visual Studio? Now I've got an answer, though I've spent much time to understand this.

Firstly, to develop plug-ins for Visual Studio it is needed to download Visual Studio SDK (for appropriate version of Visual Studio). I have to say that after installing it my Visual Studio 2008 Beta 2 became very unstable: IntelliSense regularly produces errors which crashes the whole Visual Studio.

After that Visual Studio will add templates of its addins. That's a base for future project (although may be addin can be created without all that stuff that is in template).

To make custom tool for generation of files the one must implement IVsSingleFileGenerator interface. I've spent a lot of time trying to make this and fails mainly because if this problem: in some places of documentation it is said that function generate gets a parameter of type out IntPtr[], in some it is said that it is out IntPtr and in fact interface has just IntPtr[]. Not any out! So how should I return value of pointer? Ok, I'm not a great specialist in .NET and arrays a sent be reference. Even if this is not a problem why there is an array not one pointer? Another outgoing parameter of function on exit must contain an amount of bytes in that array but how this will help if I'll allocate a different amount of bytes in different pointers in array. However I don't see a use for an array and just tried to allocate it and use only index 0. But this doesn't helped me and I failed in implementing that interface. May be that a bug in SDK and interface must have out IntPtr[]? Don't know I'm not a guru in Visual Studio.

I've failed to get a proper answer from MSDN documentation and my attempts so I've googled this problem.I've found some solutions but the most notable part is that in Visual Studio 2003 there was an abstract class which covered that horrible interface under much more comfortable interface. However "evil" Microsoft removed that class from version 2005. But good guys placed dll with it in Internet, i.e. here. So It's needed to implement function GetCode from that class and tool is ready to use!

However it is still needed to plug it in Visual Studio. First step is to register it as a package. This can be done by tool regpkg. It is placed in <%MVS SDK Dir%>\Tools\Bin\ directory. Just run it with a path to our compiled tool as a argument. This step can also be done manually,

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\<%MVSVersion%>\CLSID\{<%guid%>}]
@=<%Tool name%>
"InprocServer32"="C:\\WINDOWS\\system32\\mscoree.dll"
"ThreadingModel"="Both" "Class"=<%class name of tool%>
"Assembly"="<%asm name%>, Version=<%asm version%>, Culture=<%culture%>, PublicKeyToken=<%public key%>"


As you may noticed custom tool must be in GAC. May be there is a way to keep it somewhere else but I don't know.

What is next? Registered custom tool need to be registered as a generator. In registry dir of Visual Studio there is a directory Generators. There is some dirs in it: FA... is a C#, 16... is a VB.NET. It is needed to register tool for each language separately. In directory of proper language add directory with name of your tool, i.e. for my tool called CSGen:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Generators\{fae04ec1-301f-11d3-bf4b-00c04f79efbc}\CSGen] @="CSGen tool to generate code on C#"
"CLSID"="{<%clsid registed in \CLSID\%>}"
"GeneratesDesignTimeSource"=dword:00000001


After that start Visual Studio choose file in project and in properties type name of custom tool. After each save file will be regenerated. It is also can be forced manually: right-click on file in Solution Explorer and "Run custom tool".

These articles with examples could be helpful (I'm not a good explainer, just learning):

Friday, August 24, 2007

My recent .NET programming

A month ago I was writing about bugs in Visual Studio 2008 Beta 2. All in all I've escaped trouble and for a month I was using it. Professional edition to be exact. Of course when the Beta will expire I'll be forced to move to free Express Edition. And know I'm studying .NET and C#.

Book of Andrew Troelsen is almost at the end: only 2.5 chapter remain for me. They are all about ASP.NET and Web.

Moreover my text editor became much better. Though it is still have not vast functionality. I have an idea of turning it into IDE for any computer language (Python and Ruby are first candidates), but as I think this is not the matter of near future.

Programming for .NET on C# was good and effective, though there are some things that I don't like in it and want to replace them by equivalents from scripting languages. Of course that relates to new computer language. I made it to parse arithmetic expressions with integer numbers and make *.exe file that performs all these calculations. Not very much and I've stopped on trying to implement print function. The problem is not in the implementing particular built-in function but much more in whole mechanism of implementing functions. I've decided not to make thinking about it, but to take a pause. Though this idea firmly seats in my head so I think I'll return to it after gaining some experience. And moreover I'll need some time to feel pace of study on second year!

Furthermore now I'm working on .NET library to create diagrams. I've wanted to have one to make colourful diagrams from my data which is output from scripts, not in Excel format. For now it already can make proper diagram! So in future I need to add much more customisation to it.

Moreover, I've tried Ruby. It looks rather interesting and has simple solutions for problems which I've already faced in other languages(and not yet faced). I'll study it more deeply and try use it in my projects. For now if I need to make some routine tasks I prefer Python. Hope in near future I'll write more on computer languages.

— Buy! :)

Thursday, August 02, 2007

Microsoft Visual Studio 2008 Beta 2 — too bugy

I've recently started to try latest release of upcoming Visual Studio 2008 — Beta 2. But I found it having much problems on my system: Visual C# Express Edition crashed on compiling of my project and I've needed to reinstall it. After installing Express Editions I've tried to install Professional version. After installing MSDN I've experienced problems with my context menus in explorer — Explorer was just crashing after right mouse click. Of course may be not MSDN was a source of problem, but all in all I was needed to Recover my system. And what is more Professional stopped in the end and even refused to perform command to abort installation. After that I've recovered my system to morning state.

What is more bad, that now Visual Studio refuses to load my projects! I again trying to reinstall it... I've liked C# and .NET Framework (not in all, but in whole) and I'm wanting to program on them more, but... May be the solution is to come back from Visual Studio 2008 (all in all it is beta) to 2005. Another way is to practice on Java. The latter is better in aspect of migrating to another platforms, for example of I would want to work on Linux more that now (now it is equivalent to zero...)

Morale? I was trapped by my aptitude to use the latest versions of software... It isn't problem in many cases but not in that...

Monday, June 18, 2007

Microsoft Visual Studio Codename "Orcas" Beta 1 (VS 2008)

MS have released first beta version of its Visual Studio Codename "Orcas". Officially it will be Visual Studio 2008.

For a long time I was of bad opinion about MS & its products. However while writing programs I've realized that it is not an easy work & bugs always appear & and in such a complex structures as e.g. Windows it's almost impossible to find all bugs during beta testing. So now my attitude to MS products is not so bad as it was in the past.

I've decided to try MS Visual Studio Express Edition. At first I've used VS 2005 and now I switched to VS 2008. It's my trait — I love to be on the front edge of technology, though I'm not always able. :( Of course sometimes VS crashes, but this is not common case and I haven't got significant problems with that, in spite of Safari... At first I've used Express Edition which is free, but now I've decided to try Pro version which is free while in beta. Of course after beta period will expire I would have to return to EE, but now I want to understand "What is pro version and how it differs from EE and my favourite PSPad Editor". I think it will be good to compare VS and PSPad. However, to be honest, these are products of different types: VS is an IDE, while PSPad is a text editor, which can be used as an IDE. So to make comparison richer I think it will be useful to add to it Notepad++ and may be Netbeans:I haven't got much experience with it, but little portions of work in it make me feel that it is rather good IDE.

So... It seems I've found a good source of posts for this blog: write about software I like and don't like and why. All in all, I think PSPad is worth giving him a chance. :)