Skip to content

Commit

Permalink
Enable long paths on ResolveComReference task
Browse files Browse the repository at this point in the history
  • Loading branch information
ccastanedaucf committed Dec 3, 2018
1 parent a836f05 commit e435d47
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/Tasks.UnitTests/ComReference_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void TestStripTypeLibNumber()
return; // "COM is only found on Windows"
}

Assert.Equal(null, ComReference.StripTypeLibNumberFromPath(null, new FileExists(FileExistsMock)));
Assert.Equal("", ComReference.StripTypeLibNumberFromPath("", new FileExists(FileExistsMock)));
Assert.Equal(@"C:\test\typelib1.dll", ComReference.StripTypeLibNumberFromPath(@"C:\test\typelib1.dll", new FileExists(FileExistsMock)));
Assert.Equal(@"C:\test\typelib2\2.dll", ComReference.StripTypeLibNumberFromPath(@"C:\test\typelib2\2.dll", new FileExists(FileExistsMock)));
Assert.Equal(@"C:\test\typelib3.\3dll", ComReference.StripTypeLibNumberFromPath(@"C:\test\typelib3.\3dll", new FileExists(FileExistsMock)));
Expand Down
33 changes: 21 additions & 12 deletions src/Tasks/ComReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,7 @@ internal static string StripTypeLibNumberFromPath(string typeLibPath, FileExists
{
try
{
var sb = new StringBuilder(NativeMethodsShared.MAX_PATH);
System.Runtime.InteropServices.HandleRef handleRef = new System.Runtime.InteropServices.HandleRef(sb, libraryHandle);
int len = NativeMethodsShared.GetModuleFileName(handleRef, sb, sb.Capacity);
if ((len != 0) &&
((uint)Marshal.GetLastWin32Error() != NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER))
{
typeLibPath = sb.ToString();
}
else
{
typeLibPath = "";
}
typeLibPath = GetModuleFileName(libraryHandle);
}
finally
{
Expand All @@ -413,6 +402,26 @@ internal static string StripTypeLibNumberFromPath(string typeLibPath, FileExists
return typeLibPath;
}

private static string GetModuleFileName(IntPtr handle)
{
bool success = false;
var buffer = new StringBuilder();

// Try increased buffer sizes if on longpath-enabled Windows
for (int bufferSize = NativeMethodsShared.MAX_PATH; !success && bufferSize <= NativeMethodsShared.MaxPath; bufferSize *= 2)
{
buffer.EnsureCapacity(bufferSize);

var handleRef = new System.Runtime.InteropServices.HandleRef(buffer, handle);
int pathLength = NativeMethodsShared.GetModuleFileName(handleRef, buffer, buffer.Capacity);

bool isBufferTooSmall = ((uint)Marshal.GetLastWin32Error() == NativeMethodsShared.ERROR_INSUFFICIENT_BUFFER);
success = pathLength != 0 && !isBufferTooSmall;
}

return success ? buffer.ToString() : string.Empty;
}

/// <summary>
/// Gets the type lib path for given type lib attributes(reused almost verbatim from vsdesigner utils code)
/// NOTE: If there's a typelib number at the end of the path, does NOT strip it.
Expand Down

0 comments on commit e435d47

Please sign in to comment.