Com by Microsoft
Nuget / site data
Details
Info
Name: Com
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
Author: Microsoft
NuGet: https://www.nuget.org/packages/System.Runtime.InteropServices/
You can find more details at https://learn.microsoft.com/en-us/dotnet/standard/native-interop/comwrappers-source-generation
Source : https://github.com/dotnet/runtime
Original Readme
.NET Runtime
- What is .NET?
- How can I contribute?
- Reporting security issues and security bugs
- Filing issues
- Useful Links
- .NET Foundation
- License
This repo contains the code to build the .NET runtime, libraries and shared host (dotnet
) installers for
all supported platforms, as well as the sources to .NET runtime and libraries.
What is .NET?
Official Starting Page: https://dotnet.microsoft.com
- How to use .NET (with VS, VS Code, command-line CLI)
- Install official releases
- Install daily builds
- Documentation (Get Started, Tutorials, Porting from .NET Framework, API reference, ...)
- Supported OS versions
- Roadmap
- Releases
How can I contribute?
We welcome contributions! Many people all over the world have helped make this project better.
- Contributing explains what kinds of contributions we welcome
- Workflow Instructions explains how to build and test
- Get Up and Running on .NET Core explains how to get nightly builds of the runtime and its libraries to test them in your own projects.
Reporting security issues and security bugs
Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter. You can also find these instructions in this repo's Security doc.
Also see info about related Microsoft .NET Core and ASP.NET Core Bug Bounty Program.
Filing issues
This repo should contain issues that are tied to the runtime, the class libraries and frameworks, the installation of the dotnet
binary (sometimes known as the muxer
) and the installation of the .NET runtime and libraries.
For other issues, please file them to their appropriate sibling repos. We have links to many of them on our new issue page.
Useful Links
- .NET Core source index / .NET Framework source index
- API Reference docs
- .NET API Catalog (incl. APIs from daily builds and API usage info)
- API docs writing guidelines - useful when writing /// comments
- .NET Discord Server - a place to discuss the development of .NET and its ecosystem
.NET Foundation
.NET Runtime is a .NET Foundation project.
There are many .NET related projects on GitHub.
- .NET home repo - links to 100s of .NET projects, from Microsoft and the community.
- ASP.NET Core home - the best place to start learning about ASP.NET Core.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.
General .NET OSS discussions: .NET Foundation Discussions
License
.NET (including the runtime repo) is licensed under the MIT license.
About
Generating Com Declarations
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- MyShell.cs
This is the CSharp Project that references Com
<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>
This is the use of Com in Program.cs
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}");
This is the use of Com in MyShell.cs
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
);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- test.IShellExecute.cs
// <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.10.36612")]
[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
{
}
}
Usefull
Download Example (.NET C# )
Share Com
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Com