Skip to content

Instantly share code, notes, and snippets.

View MichalStrehovsky's full-sized avatar

Michal Strehovský MichalStrehovsky

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
PEHeaderBuilder peHeaderBuilder = new PEHeaderBuilder(machine: Machine.I386, imageCharacteristics: Characteristics.ExecutableImage);
// This is a System.Reflection.Metadata writer to generate managed COFF OBJ files similar to
// what C++/CLI generates with /clr:pure option.
//
// This will generate a c:\\temp\\blah.obj file that contains a single method.
// You can inspect the managed content of the OBJ with ildasm (the GUI doesn't work for obj files, but
// you can run it from the command line and specify /out= to disassemble).
//
// Run "link.exe /debug blah.obj /entry:MyMethod /subsystem:console" to generate an EXE file.
//
// There's also debug information. You can create a fake c:\\temp\\il.il file with a couple

Building a universal Windows 7/Windows 10 .NET EXE

The problem with building a .NET (classic) executable that runs on both clean Windows 7 install and on Windows 10 is that Windows 7 only ships with .NET 3.5 inbox and Windows 10 ships with .NET 4.X. A .NET 3.5 executable will not run on a (clean install) Windows 10 directly. It can be coerced to do so in multiple ways, but none of them are "worry-free single file" solutions (config file, registry settings, environment variables, etc.).

One of the solutions is to set COMPLUS_OnlyUseLatestCLR environment variable to 1 before the process starts. This will allow .NET 4.X to take over execution of the program. This still doesn't qualify as "worry-free" because we need a batch file or something else to set the envionment for us before the process start (it's too late once Main is executing).

One weird trick to run the same executable on both Windows 7 and Windows 10

When I said we need to set COMPLUS_OnlyUseLatestCLR environment variable to 1 bef

// On CoreCLR side: git format-patch --stdout -1 {SHA-HASH} > d:\git\in.patch
// run tool
// On CoreRT side: git am d:\git\out.patch
/*
mapping.txt:
src/tools/crossgen2/Common/CommandLine src/Common/src/CommandLine
src/tools/crossgen2/Common/Compiler src/ILCompiler.Compiler/src/Compiler
src/tools/crossgen2/Common/JitInterface src/JitInterface/src
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
MarshalHelpers.cs
else if (MarshalUtils.IsBlittableType(type) && !isField)
{
if (nativeType == NativeTypeKind.Invalid || nativeType == NativeTypeKind.LPStruct)
return MarshallerKind.BlittablePtr;
else
return MarshallerKind.Invalid;
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
public class InvokeRefReturnNetcoreTests
{
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
namespace MemberScanner
{
class Program
{
@MichalStrehovsky
MichalStrehovsky / PublicScanner.cs
Created January 24, 2018 15:09
Finds public types in an assembly that are not exposed from a set of facades
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
namespace PublicScanner
{
class Program
.assembly nocorelib { }
.method private static int32 Main()
{
.entrypoint
ldc.i4.5
ret
}