Monday, December 7, 2009

RegCap.exe Unhandled Exception with Deployment Apps

If you have Deployment Projects in Visual Studio 2008 (maybe 2005 as well), and your app to deploy uses Make Assembly COM-Visible, RegCap.exe will crash when building the deployment project on Windows 7 (maybe on Vista as well).

The work-around appears to be running Visual Studio in XP Compatibility mode.

Here is some more info:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=466157&wa=wsignin1.0

 *** Update ***

The previous link does not appear to exist anymore, but there is a Stack Overflow article that tells equivalent information:

http://stackoverflow.com/questions/2334005/visual-studio-registry-capture-utility-has-stopped-working-error-compiling-c-sh

Wednesday, November 11, 2009

Get the User's Temporary Directory in .NET

System.IO.Path.GetTempPath()

or

Environment.GetEnvironmentVariable("temp")

Tuesday, October 27, 2009

How to find Regasm.exe and use it in a .Bat file

I need to use RegAsm.exe to register several assemblies in a .bat file; however, since RegAsm.exe is not in the system path, it is difficult to just use like Regsvr32 which is always in the system path. I suppose the reason for this is that there can be multiple .NET Frameworks installed and you need to use the correct one.

You can use the WINDIR enviroment variable to get the root of the path. It will probably be something like:

C:\WINDOWS

You then need to add "\Microsft.NET\Framework\" to the path, and finally the path to the specific .NET Framework you are using:

SET fwpath="%windir%\Microsoft.NET\Framework\v2.0.50727"

%fwpath%\regasm.exe app.dll

How to pass TARGETDIR with CustomActionData and not get an Error

Maybe it is just me, but it seems like everything I need to do with an MSI setup is never as easy as it should be. Here is just an other example.

I need to pass the Target Directory for the installation to the InstallClass that I am using in the MSI Custom Actions (I have to do this because of other things in the MSI that don't work like they should). However, when I define the CustomActionData as:

/targetPath=[TARGETDIR]

or

/targetPath="[TARGETDIR]"


I get the following error:

Error 1001. Exception occurred while initializing the Installation:System.IO.FileNotFoundException


The solution I have found is to say this:

/targetPath="[TARGETDIR]\"

I found this info from the following post:

http://www.dotnet247.com/247reference/msgs/35/179584.aspx


I later found the Microsoft documentation on solving the problem:

http://msdn.microsoft.com/en-us/library/2w2fhwzz(VS.71).aspx

Thursday, October 22, 2009

Import an Excel File with VB.NET

Here is a simple solution for importing an Excel file into VB.NET using an OleDbConnection:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_22487951.html

Wednesday, September 16, 2009

Some Good Examples of MemoryStream in C#

Here are several good examples of using MemoryStream in C#:

http://www.java2s.com/Code/CSharp/File-Stream/CreateaMemoryStream.htm

Formatting DateTime in C#

Here is an excellent list of the C# Formatting specifications for the DateTime type. The VB.NET formatting could easily be derived from this post as well:

http://www.csharp-examples.net/string-format-datetime/

Monday, September 14, 2009

How to Save a Bitmap as a 1BPP (bitonal) TIFF File in .NET

If you try to use the EncoderValue.CompressionCCITT4 value to save a Bitmap out as a 1 BPP Tiff file, you will get an error "Invalid Parameter Value". It turns out that .NET can't go from an RGB to a Bitonal image with the Save method on a Bitmap (no matter how you set the encoder parameters). The following posting by Michael McCloskey solves the problem:

http://www.codeproject.com/KB/GDI-plus/BitonalImageConverter.aspx?msg=2020403


Easy to use and runs quickly.

Thursday, September 3, 2009

My Trials and Tribulations getting a WCF Service running on 64 bit Windows Server 2008 with 32 bit Native Dlls

My WCF Service uses a native DLL written in C++. This native Dll can be 32 bit or 64 bit. The 32 bit version relies on other 3rd party Dlls that are not available in 64 bit. And the 64 bit version is the same thing without the 3rd party dependencies.

1. IIS 7 is easy enough to work in 32 bit or 64 bit mode (on a 64 bit Windows Server 2008 server). By default, the application pool is 64 bit, but you can create another application pool for 32 bit applications or change the default application pool to 32 bit by using the “Enable 32-bit Applications” switch under Set Application Pool Defaults. Then on the Advanced Setting page for your WCF Services Virtual Directory, you can select the Application Pool to use. More Info: http://blogs.msdn.com/rakkimk/archive/2007/11/03/iis7-running-32-bit-and-64-bit-asp-net-versions-at-the-same-time-on-different-worker-processes.aspx

2. The \Windows\System32 directory is for 64 bit files on a 64 Bit OS. And SysWow64 contains the 32 bit files. (Windows on Windows 64 bit). Confusing, but once you know, you know.

3. Getting a 32 bit app using MFC with shared DLLs running on a 64 bit OS can give you a funny error: “The application has failed to start because its side-by-side configuration is incorrect”. The 64 bit OS doesn’t get the standard 32 bit MFC Runtime dlls installed. So you can deliver the 32 bit dependencies or just recompile the DLL using MFC in a Static Library. Easy to fix either way, but the error message is not so obvious. More Info: http://www.eggheadcafe.com/conversation.aspx?messageid=31209382&threadid=31189019

4. The WCF Service always runs fine on the development machine. Deploying it to a real IIS server then has various difficulties to overcome. Mostly, the problem is with security and permissions. If you get: “The caller was not authenticated by the service.” You can possibly just change the binding from wsHttpBinding to basicHttpBinding in the web.config file. Here is more discussion on the issue: http://stackoverflow.com/questions/284538/wcf-error-the-caller-was-not-authenticated-by-the-service

Simple Way to tell if your .NET app is running in 32bit or 64bit Mode

Here is a quick and easy was to tell if your .NET app is running in 32 bit or 64 bit mode:


C#

bool is64bit = IntPtr.Size == 8;


VB.NET

Dim is64bit As Boolean = IntPtr.Size = 8



More information on from:

http://channel9.msdn.com/forums/Coffeehouse/465445-How-can-I-tell-if-my-NET-application-is-running-in-64bit-on-the-64bit-server/

Wednesday, September 2, 2009

Problems with Native DLLs, DllImport, and ASP.NET

I have long had a problem when using ASP.NET where my native Dlls can't be found when the .NET Assembly calls a function from the native Dll. The only solution I have been able to find is to put the native DLLs in a directory that is in the system PATH (or in a directory that is already in the PATH like Windows\system32). I have searched the internet many times and finally found a posting that explains why even if there doesn't appear to be a good solution:

http://bytes.com/topic/c-sharp/answers/843777-asp-net-dllimport


Stephen Cheng explains that the .NET assemblies are copied to a ASP.NET temporary directory before they are are run. Since the .NET assemblies really don't know anything about the native .DLLs, the native DLLs don't get copied. When the .NET assembly tries to use a method in the native DLL, it is not there and you get the error message:

"An exception of type 'System.EntryPointNotFoundException' occurred in xxx.DLL but was not handled in user code Additional information: Unable to find an entry point named 'xxxx' in DLL '.\xxx.dll'."

It doesn't matter that the Native dll is in your ASP.NET project's bin directory, because that is not where it is run from.

The solutions given are to use LoadLibrary to get the dll loaded into memory, but you have to know where it is. You can turn the shadowCopyBinAssemblies off in the web.config file, but this has problems. And finally, you can add the location of the native dll to the system PATH.

If any one knows of a better solution, please post a comment.

Sunday, August 30, 2009

Posting Code in Blogger

This posting is off topic too, but I have finally found some good instructions on posting code samples on Blogger. See this post for details:

http://abhisanoujam.blogspot.com/2008/12/blogger-syntax-highlighting.html

Thursday, August 27, 2009

Restarting IIS from the Command-Line

This posting is a little off topic, but I always forget the command-line command to restart IIS. It is simply: iisreset


Here is more info at:

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/003ed2fe-6339-4919-b577-6aa965994a9b.mspx?mfr=true

Tuesday, August 4, 2009

How to tell in VB.NET or C# if the Ctrl or Shift Key are being pressed

Sometimes you may need to know if the Ctrl or Shift key is being pressed by checking its state instead of using a key event. Also, most solutions to this problem involve some pinvoking and the win32api GetKeyState, but it turns out to be very simple as this posting describes:

http://predicatet.blogspot.com/2008/04/how-to-detect-if-shift-or-control-key.html


Here is the gist:

if (Control.ModifierKeys == Keys.Control)
{

}

Wednesday, July 22, 2009

Getting Started Building HTML Help Files (.CHM) from Visual Studio's XML Documentation

If you want to build HTML Help files (.CHM) from the XML Documentation embedded in your .NET code, here is a good post on how to get started:

http://saftsack.fs.uni-bayreuth.de/~dun3/archives/integrate-xml-code-comments-into-visual-studio-20052008-using-sandcastle-and-html-help-20/150.html


Here is some more information after you get going:

http://www.codeproject.com/KB/XML/csharpcodedocumentation.aspx

Here is even more good info on the XML Documentation Syntax from Alan Dean:

http://thoughtpad.net/alan-dean/cs-xml-documentation.html

Making your .CHM FIles (HTML Help) use a larger Font

This post is a little off topic, but this tip is worth a little more press.

If you can't read a .CHM file (HTML Help file) because the font is just too small, you can make it readable again (Thanks Garth Jones).

http://smsug.ca/blogs/garth_jones/archive/2008/10/20/tip-of-the-day.aspx


Options/Internet Options/Accessibility -> Ignore Fonts Sizes on specified Web pages

Monday, July 20, 2009

Good Example of using IDisposable with File Streams

Here is a simple, yet good example of how to implement IDisposable when dealing with File Streams:


http://www.blackwasp.co.uk/IDisposable.aspx


Here is a similar example with some discussion, but the first link is probably better:

http://stackoverflow.com/questions/1136210/am-i-implementing-idisposable-correctly

Creating Custom Dictionary for Code Analysis in Visual Studio

Here is a good description of how to add your own custom words to the spelling dictionary used by Visual Studio's Code Analysis Naming rules (also known as FXCop):

http://duncanjasmith.blogspot.com/2008/07/creating-custom-dictionary-for-code.html

Friday, July 17, 2009

Getting Started with Virtual Earth/Bing Maps

Here is a great video on how to get going with Virtual Earth (now Bing Maps).

This video gets you through all of the problems that show up that the documentation fails to cover.

http://www.liveside.net/developer/archive/2008/09/29/video-getting-started-with-the-virtual-earth-web-service.aspx

Monday, July 6, 2009

Setting the Exit Code when exiting a VB.NET or C# Console App

To set the Exit Code (ErrorLevel) when a VB.NET or C# Console Application exits, use the following (or similar):

Dim retCode as Integer = 0
.
.
.
System.Environment.Exit( retCode)



The call to Exit will end the application.

Thursday, July 2, 2009

Implementing a SQL-like 'LIKE' Comparison in VB.NET

I searched the net for some time to find an easy way to implement the logic in the SQL LIKE in a VB.NET function. There were many over simplified examples. Then I found this elegant solution using regular expressions (regex). The original is found here in C#:

http://bytes.com/groups/net-c/253519-using-regex-create-sqls-like-like-function


I have provided a VB.Net version below:


Public Function IsSqlLikeMatch(ByVal input As String, ByVal pattern As String) As Boolean

' Turn "off" all regular expression related syntax in
' the pattern string.


pattern = Regex.Escape(pattern)

' Replace the SQL LIKE wildcard metacharacters with the
' equivalent regular expression metacharacters.


pattern = pattern.Replace("%", ".*?").Replace("_", ".")

' The previous call to Regex.Escape actually turned off
' too many metacharacters, i.e. those which are recognized by
' both the regular expression engine and the SQL LIKE
' statement ([...] and [^...]). Those metacharacters have
' to be manually unescaped here.


pattern = pattern.Replace("\[", "[").Replace("\]", "]").Replace("\^", "^")

Return Regex.IsMatch(input, pattern)

End Function

Thursday, June 25, 2009

Converting code between VB.NET and C# using SharpDevelop

While I really love the online VB.NET to C# and C# to VB.NET code converters provided by developerFusion, I have also found the open source SharpDevelop application to be a very good tool for converting .NET code from one flavor to another. It is especially good at converting whole projects or larger amounts of code. I think it is worthy a post:

http://www.icsharpcode.net/OpenSource/SD/

Code Conversion Features:

http://community.sharpdevelop.net/blogs/mattward/articles/FeatureTourCodeConversion.aspx

Code Conversion is one very small part of SharpDevelop. The rest is worth a look too!

Friday, June 5, 2009

Converting a Byte Array to a String in VB.NET

Here is one way to convert a byte array to a String in VB.NET:


Dim bArray() As Byte = {65, 66, 67, 68, 69}
Dim str As String
str = System.Text.Encoding.ASCII.GetString(bArray)



More examples of converting Byte Arrays to Strings and vice versa in VB.NET and C# can be found here:

http://www.chilkatsoft.com/faq/DotNetStrToBytes.html

Getting Application Path for a Console App

In a previous post, I gave examples of getting the Application Path for a Windows Form app and a Windows Mobile app. Getting the Application Path for a Console app is similar to getting one for the Windows Mobile app:


Dim appPath As String = _
System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

Monday, May 18, 2009

TechEd 2009 in Los Angeles



This year the TechEd Developers Conference was merged with the TechEd IT conference and happened last week in Los Angeles. While there were significantly fewer Developer oriented sessions, it was interesting to sit in some of the IT sessions. With around 700 sessions to choose from and only about 20 you can actually attend, the need to be particular about what you want to learn becomes very important. I heavily weighted my track with “Mobile”, but I also checked out SQL Server’s Spatial capabilities, and the future directions of the development environments and programming languages.

The push, I mean focus, this year was definitely the yet to be released Windows 7 and Windows Server 2008 R2, followed by a healthy dose of Virtualization in the form of Hyper-V and Windows Virtual PC.

I did have an opportunity to run my apps on a beta copy of Windows 7 at the “Bring Your Own App Lab.” There were a few glitchy things with reading some values out of the registry, but overall I was very pleased with how my apps worked the first time on Windows 7. I have already checked off a lot of grief on my to-do list from this experience. The free booze they were passing out was not even necessary to favorably color my opinion of Windows 7.

The focus last year was definitely LINQ, which seems to have been toned down now to is actual value. WPF and Silverlight, looked cool to try, but now they look usable. Microsoft’s creation of tools to help spawn the development of new programming languages is supposed to bring about an explosion in new languages. I along with everyone else in the Polyglot Programmer session cringed at the idea of having to learn dozens of new languages, but like LINQ, this idea may fade into a manageable and productive tool in our programming arsenal. I was hoping to see Windows Workflow (WF) be a hotter topic than it apparently is, but it may take a few iterations before it catches on.

I was pretty pleased with TechEd 2009. I will continue to process the information for a while.



Tuesday, May 5, 2009

Scaling one Rectangle to fit in Another Rectangle

Here is a great posting that shows several approaches to scaling one rectangle to fit inside of another:

http://www.vbdotnetforums.com/graphics-gdi/33829-scale-rectangle-fit-another.html

Monday, April 13, 2009

GetComputerName equivalent in VB.NET and C#

The Win32Api GetComputerName has a simpler equivalent in the .NET world:



Dim compName as String = Environment.MachineName.ToString

Wednesday, April 8, 2009

Getting the Version of an OleDB Provider

There may a much simpler way of determining the version of an OleDb provider (like OraOledb.Oracle.1) than I have shown below, but I have yet to find one.

The OleDbConnection object will tell you the version of the Server through its ServerVersion property:

Dim myConn As New OleDbConnection(g_gtech.GetConnectionString)
myConn.Open()
Dim versionStr as String = myConn.ServerVersion.ToString
myConn.Dispose()


But, I often want to the know the version of the Provider itself. The only way I have found to do this is to sift through the registry and read the ProductVersion off the provider DLL itself.

For example:

Dim versionStr as String = _
GetProviderVersion("OraOleDb.Oracle.1")

Here is the supporting code:


Public Function GetProviderVersion(ByVal providerName As String) As String

Try
Dim clsid As String = GetRegistryValue(Registry.LocalMachine, _
"SOFTWARE\Classes\" + providerName + "\clsid", "")

If clsid.Trim = "" Then Return "Not Installed"
Dim path As String = _
GetRegistryValue(Registry.LocalMachine, "SOFTWARE\Classes\CLSID\" + clsid + "\InprocServer32", "")

Dim Info As FileVersionInfo
Info = FileVersionInfo.GetVersionInfo(path)
Return Info.ProductVersion.ToString

Catch ex As Exception

Return "Unable to get Version"

End Try

End Function

Public Function GetRegistryValue(ByVal regKey As RegistryKey, _
ByVal subKey As String, ByVal valueName As String) As String

Dim value As String = ""
Dim registryKey As RegistryKey = regKey
Dim registrySubKey As RegistryKey
registrySubKey = registryKey.OpenSubKey(subKey)
If registrySubKey IsNot Nothing Then
Try
value = registrySubKey.GetValue(valueName).ToString
Catch ex As Exception
value = ""
End Try
registrySubKey.Close()
End If
Return value
End Function

If there is a simple or more elegant way to find the Provider version, please comment.

Friday, March 27, 2009

Setting the DateTimePicker to a Blank Value

I wanted to use the DateTimePicker control on a data entry form; however, it always has to be set to a value. I did not want to set the date value to Now or some other preset value because it is not clear to the user that the value needs to be set. I found several solutions to this problem, but the one below appears to work and is easy to use.

Initialize the DateTimePicker control to a blank value by setting the Format property to Custom and the CustomFormat property to a space (" "):

DateTimePickerConstDate.CustomFormat = " "
DateTimePickerConstDate.Format = DateTimePickerFormat.Custom


Then, when the value changes, change the Format property back to its original value (or define a usable Custom Format:

Private Sub DateTimePickerConstDate_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePickerConstDate.ValueChanged

DateTimePickerConstDate.Format = DateTimePickerFormat.Short

End Sub


For more information on this solution and other approaches, see the following:

http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/46b2a370-72ec-485c-9361-bfa505bb6863/

Sunday, March 22, 2009

Sorting a VarChar Attribute as a Number in a SQL Statement

This post is not particularly .NET, but I am posting it anyway.

I was needing to sort the results of a SQL statements by a Priority attribute (which has the values 1 to 10). Unfortunately, the Priority attribute is defined as VarChar, so 10 always follows 1 when you do an ORDER BY Priority.

There are many solutions to this problem, but this simplest (although not the more readily obvious, and maybe not the most efficient) is this:

SELECT * FROM Data_Table ORDER BY Len(Priority), Priority ASC

Another approach is:

SELECT * FROM Data_Table ORDER BY RIGHT('0000' + RTRIM(LTRIM(Priority)), 4)


There are other approaches in the posts where I found this solution:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81901

http://www.eggheadcafe.com/conversation.aspx?messageid=29907381&threadid=29907381

Thursday, February 12, 2009

Appending a File with StreamWriter

The StreamWriter class has an Append flag that can be set to append to the file:


Dim outfile As New StreamWriter(filename, True)


The are other overloaded versions with the append flag as well.