Skip to content

Commit

Permalink
com
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Nov 18, 2023
1 parent ce79c93 commit 2f74cc1
Show file tree
Hide file tree
Showing 41 changed files with 1,066 additions and 20 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RSCG - 87 Examples of Roslyn Source Code Generators / 9 created by Microsoft /
# RSCG - 88 Examples of Roslyn Source Code Generators / 9 created by Microsoft /

## Latest Update : 2023-11-19 => 19 November 2023
## Latest Update : 2023-11-20 => 20 November 2023

If you want to see examples with code, please click ***[List V2](https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG)***

Expand All @@ -16,8 +16,31 @@ If you want to be notified each time I add a new RSCG example , please click htt

## Content

Those are the 87 Roslyn Source Code Generators that I have tested you can see and download source code example.
Those are the 88 Roslyn Source Code Generators that I have tested you can see and download source code example.
( including 9 from Microsoft )
### 88. [Com](https://ignatandrei.github.io/RSCG_Examples/v2/docs/Com) generated on : 2023-11-20 => 20 November 2023
<details>
<summary>Expand</summary>
Author: Microsoft

Provides types that support COM interop and platform invoke services.

Commonly Used Types:
System.Runtime.InteropServices.GCHandle
System.Runtime.InteropServices.GuidAttribute
System.Runtime.InteropServices.COMException
System.DllNotFoundException
System.Runtime.InteropServices.DllImportAttribute

Nuget: [https://www.nuget.org/packages/System.Runtime.InteropServices/](https://www.nuget.org/packages/System.Runtime.InteropServices/)


Link: [https://ignatandrei.github.io/RSCG_Examples/v2/docs/Com](https://ignatandrei.github.io/RSCG_Examples/v2/docs/Com)

Source: [https://github.com/dotnet/runtime](https://github.com/dotnet/runtime)

</details>

### 87. [RDG](https://ignatandrei.github.io/RSCG_Examples/v2/docs/RDG) generated on : 2023-11-19 => 19 November 2023
<details>
<summary>Expand</summary>
Expand Down
5 changes: 4 additions & 1 deletion v2/Generator/MultiGeneratorV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public MultiGeneratorV2(string root)
{"OptValidator",new(true,new(2023,11,17),Category.EnhancementClass) },
{"ConfigBinder",new(true,new(2023,11,18),Category.API) },
{"RDG",new(true,new(2023,11,19),Category.API) },
{"COM",new(true,new(2023,11,20),Category.EnhancementProject) },

};
var noCategory = generators.Where(it=>it.Value.Category == Category.None).ToArray();
Expand Down Expand Up @@ -360,7 +361,9 @@ public string[] SourceNoRSCG()
text = text.Replace("(./docs/", $"({d.Generator!.Source}/docs/");
text = text.Replace("(CONTRIBUTING.md)", $"({d.Generator!.Source}/CONTRIBUTING.md)");
text = text.Replace("(./CODE-OF-CONDUCT.md)", $"({d.Generator!.Source}/CODE-OF-CONDUCT.md)");

text = text.Replace("(docs/project/dogfooding.md)", $"({d.Generator!.Source}/docs/project/dogfooding.md)");
text = text.Replace("(docs/workflow/README.md)", $"({d.Generator!.Source}/docs/workflow/README.md)");
text = text.Replace("(SECURITY.md)", $"({d.Generator!.Source}/SECURITY.md)");
return text;
}
;
Expand Down
1 change: 1 addition & 0 deletions v2/Generator/all.csv
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ Nr,Key,Source,Category
85,OptValidator, https://github.com/dotnet/runtime,EnhancementClass
86,ConfigBinder, https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8#configuration-binding-source-generator,API
87,RDG, https://github.com/dotnet/aspnetcore,API
88,COM, https://github.com/dotnet/runtime,EnhancementProject
59 changes: 59 additions & 0 deletions v2/book/examples/Com.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

<h1>RSCG nr 88 : Com</h1>

<h2>Info</h2>
Nuget : <a href="https://www.nuget.org/packages/System.Runtime.InteropServices/" target="_blank">https://www.nuget.org/packages/System.Runtime.InteropServices/</a>

<p>You can find more details at : <a href="https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation" target="_blank"> https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation</a></p>

<p>Author :Microsoft</p>

<p>Source : <a href="https://github.com/dotnet/runtime" target="_blank">https://github.com/dotnet/runtime</a> </p>

<h2>About</h2>

Generating Com Declarations

<h2>
How to use
</h2>
<h3>
Add reference to the <a href="https://www.nuget.org/packages/System.Runtime.InteropServices/" target="_blank">Com</a> in the csproj
</h3>
<img src="images/Com/ComDemo.csproj.png" width="580" height="580" />

<h3>This was for me the <b>starting</b> code</h3>

<br />
I have <b>coded</b> the file Program.cs
<br />
<img src="images/Com/csFiles/Program.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file MyShell.cs
<br />
<img src="images/Com/csFiles/MyShell.cs.png" width="580" height="580" />
<hr />
<h3>And here are the <i>generated</i> files</h3>

<br />
The file <i>generated</i> is test.IShellExecute.cs
<br />
<img src="images/Com/generated/test.IShellExecute.cs.png" width="580" height="580" />

<p>
You can download the code and this page as pdf from
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/Com'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Com
</a>
</p>


<p>
You can see the whole list at
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG
</a>
</p>

Binary file added v2/book/examples/images/Com/ComDemo.csproj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions v2/book/examples/images/Com/ComDemo.csproj.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!--
<PackageReference Include="System.Runtime.InteropServices" />
-->
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions v2/book/examples/images/Com/csFiles/MyShell.cs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace test;

// Import the ShellExecute function from Shell32.dll using ComImport
[ComImport]
[Guid("00000000-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IUnknown
{
void QueryInterface(ref Guid riid, out IntPtr ppvObject);
void AddRef();
void Release();
}

//[ComImport]
[GeneratedComInterface(StringMarshalling = StringMarshalling.Utf8)]
[Guid("000214F9-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface IShellExecute
{
IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
}


// Replace this with your actual ShellExecute COM class
public class ShellExecuteClass : IShellExecute
{
public IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd)
{
// Implement the ShellExecute functionality
return NativeMethods.ShellExecute(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd);
}
}

// NativeMethods class to import necessary functions from Shell32.dll
static class NativeMethods
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions v2/book/examples/images/Com/csFiles/Program.cs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using test;

IShellExecute shellExecute = new ShellExecuteClass();

// Open a file using the default associated program
IntPtr result = shellExecute.ShellExecute(
IntPtr.Zero, // HWND (handle to parent window)
"open", // Operation to perform
"notepad.exe", // File to open (replace with your file or URL)
"", // Parameters
"", // Working directory
1 // Show command (SW_SHOWNORMAL)
);

Console.WriteLine($"ShellExecute Result: {result}");
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 133 additions & 0 deletions v2/book/examples/images/Com/generated/test.IShellExecute.cs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// <auto-generated />
#pragma warning disable CS0612, CS0618
file unsafe class InterfaceInformation : global::System.Runtime.InteropServices.Marshalling.IIUnknownInterfaceType
{
public static global::System.Guid Iid { get; } = new(new global::System.ReadOnlySpan<byte>(new byte[] { 249, 20, 2, 0, 0, 0, 0, 0, 192, 0, 0, 0, 0, 0, 0, 70 }));

private static void** _vtable;
public static void** ManagedVirtualMethodTable => _vtable != null ? _vtable : (_vtable = InterfaceImplementation.CreateManagedVirtualFunctionTable());
}

[global::System.Runtime.InteropServices.DynamicInterfaceCastableImplementationAttribute]
file unsafe partial interface InterfaceImplementation : global::test.IShellExecute
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.ComInterfaceGenerator", "8.0.9.3103")]
[global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
nint global::test.IShellExecute.ShellExecute(nint hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd)
{
var(__this, __vtable_native) = ((global::System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider)this).GetVirtualMethodTableInfoForKey(typeof(global::test.IShellExecute));
byte* __lpOperation_native = default;
byte* __lpFile_native = default;
byte* __lpParameters_native = default;
byte* __lpDirectory_native = default;
nint __retVal = default;
int __invokeRetVal = default;
// Setup - Perform required setup.
scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __lpDirectory_native__marshaller = new();
scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __lpParameters_native__marshaller = new();
scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __lpFile_native__marshaller = new();
scoped global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __lpOperation_native__marshaller = new();
try
{
// Marshal - Convert managed data to native data.
__lpDirectory_native__marshaller.FromManaged(lpDirectory, stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize]);
__lpParameters_native__marshaller.FromManaged(lpParameters, stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize]);
__lpFile_native__marshaller.FromManaged(lpFile, stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize]);
__lpOperation_native__marshaller.FromManaged(lpOperation, stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize]);
{
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned.
__lpDirectory_native = __lpDirectory_native__marshaller.ToUnmanaged();
__lpParameters_native = __lpParameters_native__marshaller.ToUnmanaged();
__lpFile_native = __lpFile_native__marshaller.ToUnmanaged();
__lpOperation_native = __lpOperation_native__marshaller.ToUnmanaged();
__invokeRetVal = ((delegate* unmanaged[MemberFunction]<void*, nint, byte*, byte*, byte*, byte*, int, nint*, int> )__vtable_native[3])(__this, hwnd, __lpOperation_native, __lpFile_native, __lpParameters_native, __lpDirectory_native, nShowCmd, &__retVal);
}

// NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call.
global::System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(__invokeRetVal);
global::System.GC.KeepAlive(this);
}
finally
{
// CleanupCallerAllocated - Perform cleanup of caller allocated resources.
__lpDirectory_native__marshaller.Free();
__lpParameters_native__marshaller.Free();
__lpFile_native__marshaller.Free();
__lpOperation_native__marshaller.Free();
}

return __retVal;
}
}

file unsafe partial interface InterfaceImplementation
{
[global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute(CallConvs = new[] { typeof(global::System.Runtime.CompilerServices.CallConvMemberFunction) })]
internal static int ABI_ShellExecute(global::System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch* __this_native, nint hwnd, byte* __lpOperation_native, byte* __lpFile_native, byte* __lpParameters_native, byte* __lpDirectory_native, int nShowCmd, nint* __invokeRetValUnmanaged__param)
{
global::test.IShellExecute @this = default;
string lpOperation = default;
string lpFile = default;
string lpParameters = default;
string lpDirectory = default;
ref nint __invokeRetValUnmanaged = ref *__invokeRetValUnmanaged__param;
nint __invokeRetVal = default;
int __retVal = default;
try
{
// Unmarshal - Convert native data to managed data.
lpDirectory = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__lpDirectory_native);
lpParameters = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__lpParameters_native);
lpFile = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__lpFile_native);
lpOperation = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__lpOperation_native);
@this = global::System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch.GetInstance<global::test.IShellExecute>(__this_native);
__invokeRetVal = @this.ShellExecute(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd);
// NotifyForSuccessfulInvoke - Keep alive any managed objects that need to stay alive across the call.
__retVal = 0; // S_OK
// Marshal - Convert managed data to native data.
__invokeRetValUnmanaged = __invokeRetVal;
}
catch (global::System.Exception __exception)
{
__retVal = global::System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller<int>.ConvertToUnmanaged(__exception);
}

return __retVal;
}
}

file unsafe partial interface InterfaceImplementation
{
internal static void** CreateManagedVirtualFunctionTable()
{
void** vtable = (void**)global::System.Runtime.CompilerServices.RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(global::test.IShellExecute), sizeof(void*) * 4);
{
nint v0, v1, v2;
global::System.Runtime.InteropServices.ComWrappers.GetIUnknownImpl(out v0, out v1, out v2);
vtable[0] = (void*)v0;
vtable[1] = (void*)v1;
vtable[2] = (void*)v2;
}

{
vtable[3] = (void*)(delegate* unmanaged[MemberFunction]<global::System.Runtime.InteropServices.ComWrappers.ComInterfaceDispatch*, nint, byte*, byte*, byte*, byte*, int, nint*, int> )&ABI_ShellExecute;
}

return vtable;
}
}

namespace test
{
[global::System.Runtime.InteropServices.Marshalling.IUnknownDerivedAttribute<InterfaceInformation, InterfaceImplementation>]
public partial interface IShellExecute
{
}
}

namespace test
{
public partial interface IShellExecute
{
}
}
6 changes: 5 additions & 1 deletion v2/book/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</head>
<body>
<h1>
This is the list of 87 RSCG with examples =>
This is the list of 88 RSCG with examples =>
</h1>

<table >
Expand Down Expand Up @@ -374,6 +374,10 @@ <h1>
<td>87</td>
<td><a href="examples/RDG.html">RDG</a></td>
</tr>
<tr>
<td>88</td>
<td><a href="examples/Com.html">Com</a></td>
</tr>
</table>


Expand Down
1 change: 1 addition & 0 deletions v2/book/pandocHTML.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ input-files:
- examples/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator.html
- examples/Microsoft.Extensions.Configuration.Binder.html
- examples/RDG.html
- examples/Com.html

# or you may use input-file: with a single value
# defaults:
Expand Down
20 changes: 20 additions & 0 deletions v2/rscg_examples/Com/description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"generator":{
"name":"Com",
"nuget":[
"https://www.nuget.org/packages/System.Runtime.InteropServices/"
],
"link":"https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation",
"author":"Microsoft",
"source":"https://github.com/dotnet/runtime"
},
"data":{
"goodFor":["Generating Com Declarations"],
"csprojDemo":"ComDemo.csproj",
"csFiles":["Program.cs","MyShell.cs"]
},
"links":{
"blog":"",
"video":""
}
}
Loading

0 comments on commit 2f74cc1

Please sign in to comment.