Skip to content

Commit

Permalink
test: Removed tlb and dll writing for demo and analysis (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
marklechtermann authored Jan 10, 2025
1 parent 4d9b0d9 commit 5caf5f5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 84 deletions.
3 changes: 1 addition & 2 deletions src/dscom.test/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ internal DynamicAssemblyBuilder CreateAssembly(AssemblyName assemblyName,

var name = GenerateAssemblyName(assemblyName, filepath, callerName);

var typeLibPath = Path.Combine(Dir, $"{name}.tlb");
return new DynamicAssemblyBuilder(name, assemblyBuilder, typeLibPath);
return new DynamicAssemblyBuilder(name, assemblyBuilder);
}

internal DynamicAssemblyBuilder CreateAssembly(AssemblyName name,
Expand Down
83 changes: 3 additions & 80 deletions src/dscom.test/builder/DynamicAssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace dSPACE.Runtime.InteropServices.Tests;

internal sealed class DynamicAssemblyBuilder : DynamicBuilder<DynamicAssemblyBuilder>
{
public DynamicAssemblyBuilder(string name, AssemblyBuilder assemblyBuilder, string path) : base(name)
public DynamicAssemblyBuilder(string name, AssemblyBuilder assemblyBuilder) : base(name)
{
TypeLibPath = path;
AssemblyBuilder = assemblyBuilder;
ModuleBuilder = CreateModuleBuilder();
AppDomain.CurrentDomain.AssemblyResolve += ResolveEventHandler;
}

private List<DynamicTypeBuilder> DynamicTypeBuilder { get; } = new List<DynamicTypeBuilder>();

private string TypeLibPath { get; }

public ModuleBuilder ModuleBuilder { get; set; }

public AssemblyBuilder AssemblyBuilder { get; set; }
Expand All @@ -49,7 +45,7 @@ public DynamicAssemblyBuilder(string name, AssemblyBuilder assemblyBuilder, stri
return null;
}

public DynamicAssemblyBuilderResult Build(bool storeOnDisk = true, bool skipTlbExeCall = false, bool useComAlias = false)
public DynamicAssemblyBuilderResult Build(bool useComAlias = false)
{
foreach (var customAttributeBuilder in CustomAttributeBuilder)
{
Expand All @@ -58,39 +54,14 @@ public DynamicAssemblyBuilderResult Build(bool storeOnDisk = true, bool skipTlbE

DynamicTypeBuilder.ForEach(dynamicTypeBuilder => dynamicTypeBuilder.CreateType());

// Only supported for .NET Framework
if (!skipTlbExeCall)
{
CreateDLLAndCreateTLBWithTlbExe();
}

var typeLibConverter = new TypeLibConverter();
var assembly = ModuleBuilder.Assembly;
var tlbFilePath = storeOnDisk ? TypeLibPath : string.Empty;
var typeLibExporterNotifySink = new TypeLibExporterNotifySink(useComAlias ? assembly : null);
if (typeLibConverter.ConvertAssemblyToTypeLib(assembly, tlbFilePath, typeLibExporterNotifySink) is not ITypeLib2 typelib)
if (typeLibConverter.ConvertAssemblyToTypeLib(assembly, string.Empty, typeLibExporterNotifySink) is not ITypeLib2 typelib)
{
throw new COMException("Cannot create type library for this dynamic assembly");
}

if (storeOnDisk && typelib is ICreateTypeLib2 createTypeLib2)
{
createTypeLib2.SaveAllChanges().ThrowIfFailed($"Failed to save type library {tlbFilePath}.");
}

if (storeOnDisk)
{
var options = new TypeLibTextConverterSettings()
{
Out = $"{TypeLibPath}.yaml",
TypeLibrary = TypeLibPath,
FilterRegex = new string[] { "file" }
};

var typeLibConvert = new TypeLibConverter();
typeLibConvert.ConvertTypeLibToText(options);
}

AppDomain.CurrentDomain.AssemblyResolve -= ResolveEventHandler;

return new DynamicAssemblyBuilderResult(typelib, ModuleBuilder.Assembly, typeLibExporterNotifySink);
Expand Down Expand Up @@ -140,56 +111,8 @@ internal void ForceTypeCreation(DynamicTypeBuilder toCreate, out Type? createdTy
DynamicTypeBuilder.Remove(toCreate);
}

[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Compatibility")]
private void CreateDLLAndCreateTLBWithTlbExe()
{
#if NETFRAMEWORK && DEBUG

var dllPath = $"{TypeLibPath}.dll";

AssemblyBuilder.Save(Path.GetFileName($"{TypeLibPath}.dll"));

var tlbexpexePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\TlbExp.exe");

if (File.Exists(tlbexpexePath))
{
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = tlbexpexePath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.Arguments = $"{dllPath} /win64 /out:{dllPath}.tlbexp.tlb";
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;

process.Start();

process.WaitForExit();
}

var options = new TypeLibTextConverterSettings()
{
Out = $"{dllPath}.tlbexp.tlb.yaml",
TypeLibrary = $"{dllPath}.tlbexp.tlb"
};

if (File.Exists(options.TypeLibrary))
{
var typeLibConvert = new TypeLibConverter();
typeLibConvert.ConvertTypeLibToText(options);
}

#endif
}

#if NETFRAMEWORK
private ModuleBuilder CreateModuleBuilder()
{
return AssemblyBuilder.DefineDynamicModule("DynamicTestModule", $"{Path.GetFileName($"{TypeLibPath}.dll")}");
}
#else
private ModuleBuilder CreateModuleBuilder()
{
return AssemblyBuilder.DefineDynamicModule("DynamicTestModule"); ;
}
#endif
}
2 changes: 1 addition & 1 deletion src/dscom.test/tests/MarshalAsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void MethodWithArrayParameterAndMarshalAsAttribute_SafeArraySubTypeNotSup
.Build();
}

var result = interface2.Build().Build(true, true);
var result = interface2.Build().Build();

var typeInfo = result.TypeLib.GetTypeInfoByName("TestInterface");
typeInfo.Should().NotBeNull("TestInterface should be generated");
Expand Down
2 changes: 1 addition & 1 deletion src/dscom.test/tests/MemIdTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void MethodWithBasePlusOneDispIdAttributeAndWithoutDispIDAttribute_DispId
.WithMethod("Method1").WithReturnType<string>().WithCustomAttribute<DispIdAttribute>(dispIdStart + 1).Build()
.WithMethod("Method2").WithReturnType<string>().Build()
.Build()
.Build(true, true);
.Build();

using var method1 = result!.TypeLib.GetTypeInfoByName("TestInterface")!.GetFuncDescByName("Method1");
using var method2 = result!.TypeLib.GetTypeInfoByName("TestInterface")!.GetFuncDescByName("Method2");
Expand Down

0 comments on commit 5caf5f5

Please sign in to comment.