0
在Mac OS上運行.Net Core時,是否可以使用PInvoke調用AppKit方法?具體來說,我想使用NSWorkspace.Recycle
方法。如何從.Net核心應用程序中調用AppKit方法?
按照documentation的PInvoke支持在MacOS,它是不明確的人會如何使用該命令與框架,例如AppKit的互動。
在Mac OS上運行.Net Core時,是否可以使用PInvoke調用AppKit方法?具體來說,我想使用NSWorkspace.Recycle
方法。如何從.Net核心應用程序中調用AppKit方法?
按照documentation的PInvoke支持在MacOS,它是不明確的人會如何使用該命令與框架,例如AppKit的互動。
PInvoke僅適用於C方法,但由於Objective C運行時基於C方法,因此我們可以使用它們。 Xamarin將提供所有必要的設置和便利方法,但這是可能的。請注意,這個例子使用不安全的代碼,因此需要在文件的csproj以下屬性設置:
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
下面是完整的代碼,注意objc_msgSend
需要進口多的時間與正確的簽名。在C中,這將通過將其轉換爲具有正確簽名的不同函數指針來完成。該框架還需要通過完整路徑導入,因爲CoreCLR沒有針對框架的探測邏輯,所以它將搜索lib{sth}.dylib
。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace NSWorkspaceExample
{
public class Program
{
static void Main(string[] args)
{
var testFile = Path.Combine(Directory.GetCurrentDirectory(), "test.txt");
File.WriteAllText(testFile, "example file content");
var cfstrTestFile = CreateCFString(testFile);
var nsURL = objc_getClass("NSURL");
var fileUrl = objc_msgSend_retIntPtr_IntPtr(nsURL, GetSelector("fileURLWithPath:"), cfstrTestFile);
CFRelease(cfstrTestFile);
var urlArray = CreateCFArray(new IntPtr[] {fileUrl});
var nsWorkspace = objc_getClass("NSWorkspace");
var sharedWorkspace = objc_msgSend_retIntPtr(nsWorkspace, GetSelector("sharedWorkspace"));
objc_msgSend_retVoid_IntPtr_IntPtr(sharedWorkspace, GetSelector("recycleURLs:completionHandler:"), urlArray, IntPtr.Zero);
CFRelease(urlArray);
CFRelease(fileUrl);
CFRelease(sharedWorkspace);
// sleep since we didn't go through the troubles of creating a block object as a callback
Thread.Sleep(1000);
}
public static IntPtr GetSelector(string name)
{
IntPtr cfstrSelector = CreateCFString(name);
IntPtr selector = NSSelectorFromString(cfstrSelector);
CFRelease(cfstrSelector);
return selector;
}
private const string FoundationFramework = "/System/Library/Frameworks/Foundation.framework/Foundation";
private const string AppKitFramework = "/System/Library/Frameworks/AppKit.framework/AppKit";
public unsafe static IntPtr CreateCFString(string aString)
{
var bytes = Encoding.Unicode.GetBytes(aString);
fixed (byte* b = bytes) {
var cfStr = CFStringCreateWithBytes(IntPtr.Zero, (IntPtr)b, bytes.Length, CFStringEncoding.UTF16, false);
return cfStr;
}
}
// warning: this doesn't call retain/release on the elements in the array
public unsafe static IntPtr CreateCFArray(IntPtr[] objectes)
{
fixed(IntPtr* vals = objectes) {
return CFArrayCreate(IntPtr.Zero, (IntPtr)vals, objectes.Length, IntPtr.Zero);
}
}
[DllImport(FoundationFramework)]
public static extern IntPtr CFStringCreateWithBytes(IntPtr allocator, IntPtr buffer, long bufferLength, CFStringEncoding encoding, bool isExternalRepresentation);
[DllImport(FoundationFramework)]
public static extern IntPtr CFArrayCreate(IntPtr allocator, IntPtr values, long numValues, IntPtr callbackStruct);
[DllImport(FoundationFramework)]
public static extern void CFRetain(IntPtr handle);
[DllImport(FoundationFramework)]
public static extern void CFRelease(IntPtr handle);
[DllImport(AppKitFramework, CharSet = CharSet.Ansi)]
public static extern IntPtr objc_getClass(string name);
[DllImport(AppKitFramework)]
public static extern IntPtr NSSelectorFromString(IntPtr cfstr);
[DllImport(FoundationFramework, EntryPoint="objc_msgSend")]
public static extern IntPtr objc_msgSend_retIntPtr(IntPtr target, IntPtr selector);
[DllImport(FoundationFramework, EntryPoint="objc_msgSend")]
public static extern void objc_msgSend_retVoid_IntPtr_IntPtr(IntPtr target, IntPtr selector, IntPtr param1, IntPtr param2);
[DllImport(FoundationFramework, EntryPoint="objc_msgSend")]
public static extern IntPtr objc_msgSend_retIntPtr_IntPtr(IntPtr target, IntPtr selector, IntPtr param);
[DllImport(FoundationFramework, EntryPoint="objc_msgSend")]
public static extern void objc_msgSend_retVoid(IntPtr target, IntPtr selector);
public enum CFStringEncoding : uint
{
UTF16 = 0x0100,
UTF16BE = 0x10000100,
UTF16LE = 0x14000100,
ASCII = 0x0600
}
}
}