我正在尝试从我的 c# 代码访问 c libnotify,以便在我的带有 dotnet 核心的 linux 笔记本电脑上使用 libnotify。
但是每次从库中获取值都有问题。
这是有问题的c代码:
typedef struct _NotifyNotification NotifyNotification;
typedef struct _NotifyNotificationPrivate NotifyNotificationPrivate;
struct _NotifyNotification
{
/*< private >*/
GObject parent_object;
NotifyNotificationPrivate *priv;
};
struct _NotifyNotificationPrivate
{
guint32 id;
char *app_name;
char *summary;
char *body;
/* NULL to use icon data. Anything else to have server lookup icon */
char *icon_name;
/*
* -1 = use server default
* 0 = never timeout
* > 0 = Number of milliseconds before we timeout
*/
gint timeout;
GSList *actions;
GHashTable *action_map;
GHashTable *hints;
gboolean has_nondefault_actions;
gboolean updates_pending;
gulong proxy_signal_handler;
gint closed_reason;
};
NotifyNotification *
notify_notification_new (const char *summary,
const char *body,
const char *icon);
现在我在我的 c# 代码中创建了两个结构和一个 extern 方法:
[StructLayout(LayoutKind.Explicit)]
internal struct NotifyNotification
{
[FieldOffset(1)]
public NotifyNotificationPrivate priv;
}
[StructLayout(LayoutKind.Explicit)]
internal struct NotifyNotificationPrivate
{
[FieldOffset(0)]
public uint id;
[FieldOffset(1)]
public IntPtr app_name;
[FieldOffset(2)]
public IntPtr summary;
[FieldOffset(5)]
public int timeout;
}
[DllImport("libnotify.so.4", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr notify_notification_new([MarshalAs(UnmanagedType.LPStr)] string summary,
[MarshalAs(UnmanagedType.LPStr)] string body,
[MarshalAs(UnmanagedType.LPStr)] string icon);
使用此代码,我将所有内容都转换为结构:
NotifyNotification no = (NotifyNotification) Marshal.PtrToStructure(not, typeof(NotifyNotification));
Console.WriteLine(Marshal.PtrToStringAnsi(no.priv.summary));
基础工作正常,我可以使用来自notify_notification_new -method的指针从 libnotify 调用其他函数。但在最后一行,使用 WriteLine,调试器说:
The program '...dll' has exited with code 0 (0x00000000).
没有例外,也没有错误。怎么了?dotnet核心有问题吗?因为它还处于测试阶段?
如何从属性app_name、summary、body中获取文本?
非常感谢您的帮助。