Skip to content

Commit

Permalink
fix add file reference (com dll)
Browse files Browse the repository at this point in the history
lcid is an integer
fix typelib registry path, the lcid part is the hexadecimal string representation of the lcid value
added exception on failed add reference
fix nre on if registry key not found

fix #278
  • Loading branch information
enricosada committed Feb 27, 2015
1 parent 9c53f72 commit 4a05993
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override string Culture
int locale = 0;
try
{
locale = int.Parse(BaseReferenceNode.LCID, CultureInfo.InvariantCulture);
locale = BaseReferenceNode.LCID;
}
catch (System.FormatException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ [ DllImport( "oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false )]
private string installedFilePath;
private string minorVersionNumber;
private string majorVersionNumber;
private string lcid;
private readonly int lcid;
#endregion

#region properties
Expand Down Expand Up @@ -76,7 +76,7 @@ public string InstalledFilePath
}

[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "LCID")]
public string LCID
public int LCID
{
get { return lcid; }
}
Expand Down Expand Up @@ -133,7 +133,7 @@ internal ComReferenceNode(ProjectNode root, ProjectElement element)

this.majorVersionNumber = this.ItemNode.GetMetadata(ProjectFileConstants.VersionMajor);
this.minorVersionNumber = this.ItemNode.GetMetadata(ProjectFileConstants.VersionMinor);
this.lcid = this.ItemNode.GetMetadata(ProjectFileConstants.Lcid);
this.lcid = int.Parse(this.ItemNode.GetMetadata(ProjectFileConstants.Lcid));
this.SetProjectItemsThatRelyOnReferencesToBeResolved(false);
this.SetInstalledFilePath();
}
Expand Down Expand Up @@ -161,14 +161,15 @@ internal ComReferenceNode(ProjectNode root, VSCOMPONENTSELECTORDATA selectorData
this.typeGuid = selectorData.guidTypeLibrary;
this.majorVersionNumber = selectorData.wTypeLibraryMajorVersion.ToString(CultureInfo.InvariantCulture);
this.minorVersionNumber = selectorData.wTypeLibraryMinorVersion.ToString(CultureInfo.InvariantCulture);
this.lcid = selectorData.lcidTypeLibrary.ToString(CultureInfo.InvariantCulture);
this.lcid = (int) selectorData.lcidTypeLibrary;

// Check to see if the COM object actually exists.
this.SetInstalledFilePath();
// If the value cannot be set throw.
if (String.IsNullOrEmpty(this.installedFilePath))
{
throw new ArgumentException();
var message = string.Format(SR.GetString(SR.ReferenceCouldNotBeAdded, CultureInfo.CurrentUICulture), selectorData.bstrTitle);
throw new InvalidOperationException(message);
}
}

Expand All @@ -195,14 +196,15 @@ internal ComReferenceNode(ProjectNode root, string filePath)
this.typeGuid = typeAttr.guid;
this.majorVersionNumber = typeAttr.wMajorVerNum.ToString(CultureInfo.InvariantCulture);
this.minorVersionNumber = typeAttr.wMinorVerNum.ToString(CultureInfo.InvariantCulture);
this.lcid = typeAttr.lcid.ToString(CultureInfo.InvariantCulture);
this.lcid = typeAttr.lcid;

// Check to see if the COM object actually exists.
this.SetInstalledFilePath();
// If the value cannot be set throw.
if (String.IsNullOrEmpty(this.installedFilePath))
{
throw new ArgumentException();
var message = string.Format(SR.GetString(SR.ReferenceCouldNotBeAdded, CultureInfo.CurrentUICulture), filePath);
throw new InvalidOperationException(message);
}
}
finally
Expand Down Expand Up @@ -281,21 +283,20 @@ internal ComReferenceNode(ProjectNode root, string filePath)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
private ProjectElement GetProjectElementBasedOnInputFromComponentSelectorData()
{

ProjectElement element = new ProjectElement(this.ProjectMgr, this.typeName, ProjectFileConstants.COMReference);

// Set the basic information regarding this COM component
element.SetMetadata(ProjectFileConstants.Guid, this.typeGuid.ToString("B"));
element.SetMetadata(ProjectFileConstants.VersionMajor, this.majorVersionNumber);
element.SetMetadata(ProjectFileConstants.VersionMinor, this.minorVersionNumber);
element.SetMetadata(ProjectFileConstants.Lcid, this.lcid);
element.SetMetadata(ProjectFileConstants.Lcid, this.lcid.ToString());
element.SetMetadata(ProjectFileConstants.Isolated, false.ToString());

// See if a PIA exist for this component
TypeLibConverter typelib = new TypeLibConverter();
string assemblyName;
string assemblyCodeBase;
if (typelib.GetPrimaryInteropAssembly(this.typeGuid, Int32.Parse(this.majorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.minorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.lcid, CultureInfo.InvariantCulture), out assemblyName, out assemblyCodeBase))
if (typelib.GetPrimaryInteropAssembly(this.typeGuid, Int32.Parse(this.majorVersionNumber, CultureInfo.InvariantCulture), Int32.Parse(this.minorVersionNumber, CultureInfo.InvariantCulture), this.lcid, out assemblyName, out assemblyCodeBase))
{
element.SetMetadata(ProjectFileConstants.WrapperTool, WrapperToolAttributeValue.Primary.ToString().ToLowerInvariant());
}
Expand Down Expand Up @@ -325,7 +326,7 @@ private void SetProjectItemsThatRelyOnReferencesToBeResolved(bool renameItemNode
if (String.Compare(MSBuildItem.GetMetadataValue(reference, ProjectFileConstants.Guid), this.typeGuid.ToString("B"), StringComparison.OrdinalIgnoreCase) == 0
&& String.Compare(MSBuildItem.GetMetadataValue(reference, ProjectFileConstants.VersionMajor), this.majorVersionNumber, StringComparison.OrdinalIgnoreCase) == 0
&& String.Compare(MSBuildItem.GetMetadataValue(reference, ProjectFileConstants.VersionMinor), this.minorVersionNumber, StringComparison.OrdinalIgnoreCase) == 0
&& String.Compare(MSBuildItem.GetMetadataValue(reference, ProjectFileConstants.Lcid), this.lcid, StringComparison.OrdinalIgnoreCase) == 0)
&& String.Compare(MSBuildItem.GetMetadataValue(reference, ProjectFileConstants.Lcid), this.lcid.ToString(), StringComparison.OrdinalIgnoreCase) == 0)
{
string name = MSBuildItem.GetEvaluatedInclude(reference);
if (Path.IsPathRooted(name))
Expand Down Expand Up @@ -365,9 +366,16 @@ private void SetInstalledFilePath()
this.typeName = typeLib.GetValue(string.Empty) as string;
}
// Now get the path to the file that contains this type library.
using (RegistryKey installKey = typeLib.OpenSubKey(string.Format(CultureInfo.InvariantCulture, @"{0}\win32", this.lcid)))

// lcid
// The hexadecimal string representation of the locale identifier (LCID).
// It is one to four hexadecimal digits with no 0x prefix and no leading zeros.
using (RegistryKey installKey = typeLib.OpenSubKey(string.Format(CultureInfo.InvariantCulture, @"{0:X}\win32", this.lcid)))
{
this.installedFilePath = installKey.GetValue(String.Empty) as String;
if (installKey != null)
{
this.installedFilePath = installKey.GetValue(String.Empty) as String;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ protected override string GetLocalizedString(string value)
/*internal, but public for FSharp.Project.dll*/ public const string ProjectProperties = "ProjectProperties";
/*internal, but public for FSharp.Project.dll*/ public const string Quiet = "Quiet";
/*internal, but public for FSharp.Project.dll*/ public const string QueryReloadNestedProject = "QueryReloadNestedProject";
/*internal, but public for FSharp.Project.dll*/ public const string ReferenceCouldNotBeAdded = "ReferenceCouldNotBeAdded";
/*internal, but public for FSharp.Project.dll*/ public const string ReferenceAlreadyExists = "ReferenceAlreadyExists";
/*internal, but public for FSharp.Project.dll*/ public const string ReferenceWithAssemblyNameAlreadyExists = "ReferenceWithAssemblyNameAlreadyExists";
/*internal, but public for FSharp.Project.dll*/ public const string ReferencesNodeName = "ReferencesNodeName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@
<value>Advanced</value>
<comment>Project Property Page Caption</comment>
</data>
<data name="ReferenceCouldNotBeAdded" xml:space="preserve">
<value>A reference to '{0}' could not be added.</value>
<comment>ReferenceCouldNotBeAdded error message</comment>
</data>
<data name="ReferenceAlreadyExists" xml:space="preserve">
<value>A reference to '{0}' could not be added. A reference to the component '{1}' already exists in the project.</value>
<comment>ReferenceAlreadyExists error message</comment>
Expand Down

0 comments on commit 4a05993

Please sign in to comment.