I'm sure many of you who build a lot of standalone ArcObjects applications already know this, but I work mostly on server and mobile applications, and have never run into the problem.
Up until the release of ArcGIS 9.1, standalone ArcObjects applications initialized themselves with any available ArcGIS licenses, not requiring developers to explicitly grab a license when writing an ArcObjects application. This changed with 9.2, and the change was not very well-documented. I ran into this new 'feature' recently when trying to perform a simple connection to an SDE geodatabase. The code was very straightforward and compiled fine, and I couldn't - for the life of me - figure out why it wouldn't work. Then I ran into a couple of helpful posts on the ESRI support forums, which put me on the right track.
First, links to the posts:
Now, a solution to the problem. In this example, I'm grabbing an ArcInfo license:
public void MainForm_Load(object sender, EventArgs e)
{
//Create new AoInitialize object, used in checking out/in ArcGIS license(s)
AoInitialize m_pAoInitialize = new AoInitialize();
//Try to initialize license
try
{
if (m_pAoInitialize.IsProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) == esriLicenseStatus.esriLicenseAvailable)
{
m_pAoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
}
else
{
MessageBox.Show("ArcInfo license not available. Check your network connection.");
}
}
//Catch any errors
catch (Exception exception)
{
MessageBox.Show("Error: " + exception);
}
}
Also make sure that you also shut down the AOInitialize object when you're done with it. From the EDN documentation:
"Before an application is shut down the AOInitialize object must be shut down. This ensures that any ESRI libraries that have been used are unloaded in the correct order. Failure to do this may result in random crashes on exit due to the operating system unloading the libraries in the incorrect order."