Showing posts with label Visual Studio. Show all posts
Showing posts with label Visual Studio. Show all posts

Sunday, March 11, 2012

Extract zip files in VB.NET

Here is one way to extract zip files. The example is done in VB.NET using the Shell COM.

Declarations needed:

Imports System.IO
' Add COM reference "Microsoft Shell Controls And Automation"
Imports Shell32

Code snippet:

Dim ZipFile As String = "D:\temp\"
Dim ZipDir As String = "D:\temp\files\"
Dim Shell As Shell32.IShellDispatch2
Dim ShellFolder As Shell32.Folder

If File.Exists(ZipFile) Then
    Shell = CType(CreateObject("Shell.Application"), IShellDispatch2)

    If Not Directory.Exists(ZipDir) Then Directory.CreateDirectory(ZipDir)
    ShellFolder = Shell.NameSpace(ZipDir)
    If ShellFolder IsNot Nothing Then
    End If
End If

One downside with this approach is that a dialog box can show up while extracting the zip file.

Other ways to Zip and Unzip in C#, VB, any .NET language is using DotNetZip (recommended) or SharpZipLib as well as SevenZipSharp for 7z (7-zip). There is also the GZipStream Class that comes with .NET Framework but only works with .gz files. System.IO.Packaging.ZipPackage is available in .NET Framework 3 and newer but is not straightforward to use and not recommended.

Monday, March 5, 2012

.NET Framework requirements and versions

Normally it is a good idea to keep older .NET Framework versions. For example if you only have .NET Framework 4 installed and a software requires .NET Framework 3.5 a dialog box as below can be seen during installation. The solution is to install the version the dialog box indicates.

For users

A dialog box showing something like this can be seen: This setup requires the .NET Framework version 3.5. Please install the .NET Framework and run this setup again. The .NET Framework can be obtained from the web. Would you like to do this now?

In the setup log found in %TEMP% this can be seen for example:
Install state for .NET Framework 1.0: not installed.
Install state for .NET Framework 1.1: not installed.
Install state for .NET Framework 2.0: installed with service pack 2.
Install state for .NET Framework 3.0: not installed.
Install state for .NET Framework 3.5: not installed.
Install state for .NET Framework 4 Client: installed with no service packs.
Install state for .NET Framework 4 Full: installed with no service packs.

To find out what Framework is installed and also if it’s working as expected use .NET Framework Setup Verification Tool.

For programmers

Using Visual Studio setup project the launch condition is set to .NET Framework 3.5 as requirement on the target machine. Checking the MSI using Orca, VSDFrameworkVersion is v3.5 and VSDAllowLaterFrameworkVersions is set to True. So even though later versions are allowed you still need the required version, in this case v3.5. Lesson learned is that you cannot specify system requirement like .NET Framework v3.5 or later.

Friday, June 10, 2011

.NET code problem on Windows Server 2003 solved

I spent several hours hunting down a problem a customer had with JTB FlexReport on Windows Server 2003 SP2. The solution was in the end a single row. Things worked fine on Windows XP, Windows Vista, Windows Server 2008, Windows Server 2008 SP2  and Windows 7, but for some reason it failed on Windows Server 2003.

In the code I narrowed down that this call failed but I could not figure out why:

Database db = DatabaseFactory.CreateDatabase();

I got this error using Microsoft Enterprise Library 2.0:

Object reference not set to an instance of an object.

I was wondering if it had to do with the Enterprise Library version but I got this error using Microsoft Enterprise Library 5.0:

Activation error occured while trying to get instance of type Database, key ""

I then wondered if the problem was that I had one .exe made by VB.NET and a DLL made by C#, or that it was running as a Windows Service, or that some code had been upgraded all the way from VB6 etc.

Eventually I was able to scale away a copy of my program to just a few rows and still had the problem. Then I created a similar program from scratch and got it running. I could then start to compare the two file by file using DiffMerge and eventually I was able to find that in app.manifest I had to add the following:

<assemblyIdentity version="" name=""/>

just before <trustInfo

Now that I found the solution I looked up more details and found that reading any configuration settings from the applications .config file is not working. I also found that it actually is due to a bug in Windows Server 2003. Calls to System.Configuration.ConfigurationManager.AppSettings return null. In Windows Server 2003, if the application has the following SxS manifest, then CLR will not use the application config file even if it exists. This bug does not exist in Windows XP, Windows Vista or Windows 7. Application config file is not used if the application has certain SxS manifest and Lesson Learned: Missing language attribute in ‘assemblyidentity’ element can cause SxS (SideBySide) application loading failure on Windows XP and Server 2003

Sunday, June 5, 2011

How to rename a registry key with .NET

Let’s say I have a registry key named “HKEY_CURRENT_USER\SOFTWARE\JTB World 1” and I want to rename it to “HKEY_CURRENT_USER\SOFTWARE\JTB World 2”. This is easily done in the Registry Editor (regedit.exe).

But neither Microsoft.Win32.Registry class nor .NET Framework My.Computer.Registry provide rename functionality.

One solution is using REG.EXE, copying the key and all subkeys and then delete the old key. No need to recursively copy keys as would be needed if using the Registry class.

Here is a VB.NET example that easily can be converted to for example C#, BAT/CMD/VBS (VBScript). No error checking added. WindowStyle is used to avoid seeing Windows command window showing.

Dim startInfo As New ProcessStartInfo("REG.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.Arguments = "COPY """ & OldKey & """ """ & NewKey & """ /s"
Dim myProcess As Process = Process.Start(startInfo)
startInfo.Arguments = "DELETE """ & OldKey & """ /f"

Monday, February 7, 2011

Error 1001. The specified service has been marked for deletion

I’ve created an installer using a Visual Studio 2008 deployment project where I install and uninstall a Windows service using a custom action and run into this problem when reinstalling the program.

Error 1001. The specified service has been marked for deletion

Installation Incomplete: The installer interrupted before JTB FlexReport Core could be installed. You need to restart the installer to try again. Click "Close" to exit.

Restarting Windows helps but I wanted to avoid that.

I tried to use sc delete but that didn’t work. I found a tip that services.msc should be closed but I did not have it running. I tried to manually tweak the registry HKLM\SYSTEM\CurrentControlSet\Services but it did not help either.

It took a while to identify the problem but the solution was easy. Close or exit from Sysinternals Process Explorer and run the setup again. For some reason Process Explorer is locking the service process and causing this problem.

Wednesday, December 29, 2010

How to start a new process de-elevated after installation

I was trying to launch an application at the end of an installation I’ve created with Visual Studio. My problem was that the installation is running with elevated privileges as administrator (UAC) and I want the application to run as a normal user.

One problem with the application that was launched by the installer was that when reading and writing to the registry it ended up at HKEY_USERS\.DEFAULT instead of HKEY_CURRENT_USER. I noticed that the process was run by NT AUTHORITY\SYSTEM instead of the expected COMPUTERNAME\USERNAME. This will also cause problems if you want to open a webpage after the installation and there is no current Web Browser running.

I knew how to elevate the process using the verb runas.

p.StartInfo.UseShellExecute = true; p.StartInfo.Verb = "runas";

But I could not find any way to use the verb to de-elevate. I searched for a solution online but could only find convoluted ways like using CreateProcessAsUserW, Code Injection in the shell process or using the task scheduler to register a task to run as the currently logged on desktop user.

I found this post and tried using the User Account Control Helpers to no avail for some reason.

Process p = UacHelpers.UserAccountControl.CreateProcessAsStandardUser("notepad.exe", "");

Eventually I came up with this simple C# code thinking it might do the trick. It actually runs the new program with reduced permissions but not as the intended user. This could be useful in other situations though.

Process p = new Process(); p.StartInfo.FileName = "runas.exe"; p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; p.StartInfo.Arguments = "/trustlevel:0x20000 notepad.exe"; p.Start();

Unfortunately it didn’t help me here.

I then turned back to the UacHelpers source code and commented out these rows because I noticed that the process was not really elevated from the start so I did not want the simple way to start the process.

//if (!IsCurrentProcessElevated) //{ // return Process::Start(exePath, arguments); //}

Using this trick it was possible to have an application like my setup that runs with administrator privileges (thanks to UAC) to start another process with standard user privileges and as the expected user.

For more details on the subject see also Aaron Margosis’ WebLog.

Please comment if this helps you or if you have any related suggestions on better approach.

Thursday, October 28, 2010

Asynchrony Await and the Async CTP

More Asynchrony in programming to look forward to. And for users it means more responsive and quicker applications.

Async CTP introduces a new keyword await, which indicates that the caller would like control to return when an asynchronous method call has completed. And the Async modifier does not create additional threads.

public async Task<int> SumPageSizesAsync(IList<Uri> uris) { int total = 0; foreach (var uri in uris) { statusText.Text = string.Format("Found {0} bytes ...", total); var data = await new WebClient().DownloadDataTaskAsync(uri); total += data.Length; } statusText.Text = string.Format("Found {0} bytes total", total); return total; }

Public Async Function SumPageSizesAsync(uris As IList(Of Uri)) As Task(Of Integer) Dim total As Integer = 0 For Each uri In uris statusText.Text = String.Format("Found {0} bytes ...", total) Dim data = Await New WebClient().DownloadDataTaskAsync(uri) total += data.Length Next statusText.Text = String.Format("Found {0} bytes total", total) Return total End Function

Making Asynchronous Programming Easy

Asynchrony in C# 5, Part One

Announcing the Async CTP for Visual Basic (and also Iterators!)

The async CTP homepage:

Tuesday, December 22, 2009

Run-time error '429': ActiveX component can't create object solution for VBA 64-bit

Code like this will result in error in AutoCAD Architecture 2010 64-bit:
Set SchedApp = New AecScheduleApplication

Or this in AutoCAD 2010 64-bit:
Set SSM = New AcSmSheetSetMgr

One method that seems to work is to move the code from within a sub to the declaration like this:
Private SchedApp As New AecScheduleApplication

Another method is to replace it using GetInterfaceObject like this:
Set SchedApp = ThisDrawing.Application.GetInterfaceObject("AecX.AecScheduleApplication.6.0")

The reason is that VBA runs as an out-of-process client in 64-bit applications.

Unfortunately this also means that Sheet Set Manager API cannot be used in 64-bit applications because it is a COM server that cannot be called from another process as opposed to an ActiveX server.

Another 64-bit related problem is in AutoCAD Architecture 2009/2010 and AutoCAD MEP 2009/2010. In Tools>References the AutoCAD tlb shows up but not for the verticals. The same problem is also in .NET if you look at the COM tab when adding references.

The solution for this is a fix in the registry that look like this:

@="C:\\Program Files\\Common Files\\Autodesk Shared\\AecXBase60.tlb"

It needs to be done for each tlb you want to use. There is a win64 key but the win32 key is missing so it needs to be added.

Here is also a Microsoft KB describing similar problems with Office.

Saturday, October 10, 2009

Solution to registering of DLL problem on 64-bit systems

I was preparing installers for DWG Explorer Column Handler (DWG Columns) when I run into problem with the 64-bit versions for Vista and Windows 7. I used Visual Studio 2008, a C++ project and a basic setup project with a couple of custom actions to register and unregister a DLL file. The 32-bit setup worked without problems.

I first got the following error on Vista 64-bit and Windows 7 64-bit.

Error 1723. There is a problem with this Windows Installer package.
A DLL required for this install to complete could not be run. Contact your support personnel or package vendor.
Action _474A8F1D_82B8_4FC0_8E65_22E8CDBD2B7B, entry: _InstallPropertySchema@4, library:
C:\Program Files\JTB World\DWG Explorer Column Handler\DWGPropertyHandler.dll

After trying to create the setup directly on a 64-bit system I was able to install but the uninstall failed like this.

Error 1723. There is a problem with this Windows Installer package.
A DLL required for this install to complete could not be run. Contact your support personnel or package vendor.
Action _8B4F6610_75E7_462C_AB3D_BDC92ECCF640, entry: _UninstallPropertySchema@4, library:
C:\Program Files\JTB World\DWG Explorer Column Handler\DWGPropertyHandler.dll

I first figured out that I could edit the MSI file with Orca. In the table CustomAction I found the Actions I needed to change.

was changed to

This worked good but needed to be done manually each time.

I then tried to use a trick to change how the DLL is built so it will use the correct name. I tried with the following link step in
Visual Studio>Project Properties>Linker properties>Command Line>Additional options

/export:_InstallPropertySchema@4=InstallPropertySchema /export:_UninstallPropertySchema@4=UninstallPropertySchema

And that worked just great. Now I have installers working for XP, Vista, Windows 7 and both 32-bit and 64-bit.

Sunday, September 13, 2009

A problem has been encountered while loading the setup components - solution

I tried to add and remove features in the Visual Studio 2008 setup as I needed to add a component and got the error "A problem has been encountered while loading the setup components. Canceling setup." Not very helpful.
The solution for me was to uninstall the latest updates and hotfixes for VS 2008 and then try again. (KB973675, KB971092)

Tuesday, July 28, 2009

AutoCAD .NET Developer's Guide

This is really great news! I have reviewed a lot of the guide and can testify that it will be useful for many.
This guide provides information on how to use the AutoCAD .NET API with Microsoft Visual Studio and the programming languages VB.NET and C#. Information specific to developing applications using Microsoft Visual Studio can be found under the topics “Getting Started with Microsoft Visual Studio” and “Develop Applications with Microsoft Visual Studio.”
Programmers developing with the .NET Framework using a development environment other than Microsoft Visual Studio can skip these two chapters. However, all of the example code in this guide is presented in VB.NET and C#.
You can access the AutoCAD .NET Developer’s Guide online:


Getting Started with Microsoft Visual Studio

Basics of the AutoCAD .NET API

Control the AutoCAD Environment

Create and Edit AutoCAD Entities

Dimensions and Tolerances

Work in Three-Dimensional Space

Define Layouts and Plot

Use Events

Develop Applications with VB.NET and C#

VBA/VB to VB.NET and C# Comparison

VBA to VB.NET and C# Comparison

AutoCAD .NET Developer's Guide

Here is also some great screencasts from Microsoft.

Sunday, June 7, 2009

CodeRush Xpress free Visual Studio 2008 add-in

CodeRush Xpress is a FREE Visual Studio 2008 add-in containing 60+ refactorings, 7 editing features, and full support for C# and Visual Basic.

Download here

CodeRush Xpress includes the following features.

  • Duplicate Line
  • Highlight All References
  • Increase or Reduce Selection
  • Smart Clipboard Operations
  • Generate from Using (TDD)
  • Quick Navigation Window
  • Quick File Navigation

In addition, you receive the following refactorings.

Add Parameter
Create Overload
Create With Statement
Encapsulate Field
Encapsulate Field (read only)
Extract Interface
Extract Method
Extract Property
Extract XML Literal to Resource
Flatten Conditional
Introduce Constant
Introduce Constant (local)
Introduce Local
Introduce Local (replace all)
Inline Temp
Inline With Statement
Make Explicit
Make Explicit (and Name Anonymous Type)
Make Implicit
Method to Property
Move Declaration Near Reference
Move Initialization To Declaration
Move Type to File
Name Anonymous Type
Property to Method(s)
Remove Assignments to Parameter
Reorder Parameters
Remove Unused Parameter
Replace Temp with Query
Reverse Conditional
Simplify Expression
Split Initialization from Declaration
Split Temporary Variable
Widen Scope
Widen Scope (promote constant)
Widen Scope (promote to field)
Add Block Delimiters
Combine Conditionals
Compress to Lambda Expression
Compress to Ternary Expression
Convert to Auto-implemented Property
Convert to Initializer
Create Backing Store
Decompose Initializer
Decompose Parameter
Expand Lambda Expression
Expand Ternary Expression
Extract Method
Flatten Conditional
Inline Delegate
Inline Temp
Introduce Local
Make Explicit
Make Implicit
Move Type to File
Name Anonymous Method
Name Anonymous Type
Reverse Conditional
Split Conditional
Use String.Format
Use StringBuilder

Via The Visual Basic Team

Thursday, June 4, 2009

openFileDialog problem to open shortcuts and a solution

I found a problem with SSMPropEditor that when I select a shortcut to a file with the open dialog box it tried to open the actual shortcut and not the actual file it pointed to that it should follow its way to.

DereferenceLinks property is used to get or set a value indicating whether the dialog box returns the location of the file referenced by the shortcut or whether it returns the location of the shortcut (.lnk). But the default is true and still it did not work to dereference the link.

I also found that it worked on XP but not on Vista and Windows 7. It didn’t matter if I changed the .NET Framework version from 2.0 to 3.5 either.

Here is the code that returns the LNK file instead of the TXT file.

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FileName = "*.txt";
openFileDialog1.DefaultExt = "txt";
openFileDialog1.FilterIndex = 1;
DialogResult res = openFileDialog1.ShowDialog();

The solution that I found after some trial and error was the following. Notice the FileName property change. I made sure to not use the asterisk.

openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FileName = "";
openFileDialog1.DefaultExt = "txt";
openFileDialog1.FilterIndex = 1;
DialogResult res = openFileDialog1.ShowDialog();

Monday, May 11, 2009

Windows 7 RC performance benchmark for CAD

According to this test in PC World:

We also measured a noteworthy 7 percent speed increase in our Autodesk 3ds max 8.0 SP3 (DirectX) test on the HP Pavillion desktop, which had an nVidia GeForce 9300GE graphics board. nVidia's drivers appear to be better optimized for Windows 7 than Windows Vista.

My real world experience running Windows 7 RC on a low end and low priced notebook has been very positive compared to using Vista on the same hardware and with the same software (Office 2007 SP2, Visual Studio 2008, SQL Server 2008, AutoCAD 2010, AutoCAD Architecture 2010, etc). I actually can use most of these software at the concurrently up and running something that was not possible on Vista.

I love the boot speed, shutdown speed, general speed in day-to-day usage, lower memory consumption, stability as well as I do like some of the new look and features.

The notebook is an AMILO Pa 2510 with AMD Turion 64 X2 Mobile Technology TL-58 (1.9 GHz), 2GB RAM and an 120GB hard disk with 5400 revs, ATI Radeon X1200 video card and 1280x800 WXGA screen. Have to test a stationary later on. More on this notebok.

I’m pretty sure you get a better performance also on a high end stationary PC.

What is your experience?

Wednesday, April 29, 2009

Learn how to migrate from VBA to .NET for AutoCAD

VBA is going away from AutoCAD in a coming release but ActiveX that is the API will remain so you can use the same API you are familiar with within .NET using COM Interop. So if you’re familiar with the ActiveX Object Model you get get up and running in .NET quicker.

In the four examples in this Autodesk Developer Network Network DevTV Visual Basic Express is used and you learn how to prepare code to use late binding and then export from VBA via VB6 format and end up with VB.NET code. There is even a great free macro that makes the VB6 project without requiring VB6.

AutoCAD VBA to .NET Migration Basics
download (140 Mb)
supporting files (1.72 Mb)

  • Example 1 is showing manual migration using VB6
  • Example 2 is showing the migration using the migration macro
  • Example 3 is dealing with user forms and problems with late binding and why it is good to change Object to something more specific using early binding
  • Example 4 is for referencing other type libraries than the AutoCAD ones. The example shows object referencing Excel and Word

Via Through the Interface

See also the posts More details on VBA in AutoCAD 2010 products, AutoCAD 2010 VBA and VSTA future and No VBA installed with AutoCAD 2010

Friday, April 24, 2009

ShowModalDialog API changes in AutoCAD 2010

I got this error in some .NET code I updated to get running for AutoCAD 2010.

Error    1    Reference required to assembly 'PresentationCore, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' containing the implemented interface 'System.Windows.Interop.IWin32Window'. Add one to your project.    D:\VB.NET\MainProject\Mainclass.vb    15    74    MainProject

I found that I needed to change this code showing a modal dialog box.

Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Application.MainWindow, DialogObj)

to look like this as well as adding the PresentationCore reference.

Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(Application.MainWindow.Handle, DialogObj, False)

Tuesday, April 21, 2009

AutoCAD Architecture 2010 stopped working at startup

I made an installation of ACA 2010 64-bit on Vista 64-bit and after after installation I could not start ACA. I tried to make a repair and still same problem.

It almost started ACA completely but then I got “AutoCAD Application has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.” I did not get any CER dialog after that. acad.exe just then silently crashed.

As I have Visual Studio 2008 I did also try the Debug option and after that I actually got a CER dialog. Other than that the only thing to do was Close program.

I tried the Process Monitor and identified that the last loaded ARX file was AcPEXCtl.arx so I renamed it and was able to launch ACA just fine.

Update. So much for this workaround. Today AutoCAD Architecture 2010 crashed again so it seem to not be related to this ARX file anyway.

Anyone knowing what AcPEXCtl.arx is and why this happens?

I noticed that if I got ACA running I could after that manually load AcPEXCtl.arx without any problem.

Update and my solution.

I tried with /nohardware switch and it didn’t work but when I also run it as administrator as well I got it running. After that I could start it as normal.

Monday, April 13, 2009

TreeTrim for Visual Studio project cleanup

TreeTrim can be useful if you want to cleanup your projects.

It's a tool that strips out debug files and folders in your source code tree and also zips and emails amongst other things.

You can either run it from the command line or from Windows Explorer. The setup program intalls context menu commands when right-clicking on folders in Windows Explorer.

See also the TreeTrim FAQ

In one project I had it cleaned out 30 MB. I tested on a Visual Studio 2008 VB.NET project on Windows 7 and it worked great.

Via Steve Dunn’s blogpost New tool: TreeTrim.

Saturday, April 11, 2009

MetaDraw review

I tested MetaDraw from Bennet-Tec some time ago. Unfortunately I have not had time to do a in depth test or use it in some real development so this is quite brief.

MetaDraw is available as either ActiveX or .NET WinForms component for creating drawings, diagrams, image annotations. MetaDraw has optional DXF support (import and export) making it possible to display and convert 2D CAD drawings for example from AutoCAD. I was able to save a complex drawing in AutoCAD 2010 to DXF 2010 format and view and edit it with MetaDraw.

If you have the need to display images in raster or vector format, convert between formats, support zoom and pan, edit and add labels, annotations and the like MetaDraw is worth looking at.

With MetaDraw you can include basic CAD functionality in your software like editing objects and even have links between object making it easy to make diagrams. The API is extensive so you can do quite a lot of things with it. Several code examples are available and together with the documentation you should be up and running quickly.

If you have Internet Explorer (ActiveX required) you can try these demos directly in the web browser. Online Drawing, HotSpots Demo, Puzzle Sample and MD Map Demo.

The MetaDraw product have been around many years continually being improved upon. The latest version when writing this was released in March 2009. I tested using Visual Studio 2008 and Windows Vista as well as Windows 7.

This animated GIF is created when I try one of the sample applications that shows the functionality to link objects.

MetaDraw .NET 2.0 is a 32-bit DLL so it can not be used in 64-bit applications even though it can be used in 64 bit environments if the application is 32-bit.

Disclaimer. I was offered a free license with no strings attached.

Tuesday, March 17, 2009

uCertify review – IT Certification training

If you want to be certified (for example MCTS: Microsoft Visual Studio 2008) it might be a good idea to consider some ways to train and also to assess your knowledge before doing the exam.

I got a chance to try one of the PrepKits from uCertify. I have developed professionally for many years but the way the certification tests are done and what they might cover I found that to pass I would really need something like uCertify.

Below are some screenshots of the interface. I found it to be intuitive and easy to use.

Click to enlarge

Click to enlarge

Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner