libnotifyをlibnetを使用するためにC#コードからc libnotifyにアクセスしようとしています。
しかし、毎回libから値を取得するのに問題があります。C#:libnotifyから値を取得するstruct
これは、問題のある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方法に2つの構造体を作成しました:このコードで
[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));
基本が機能していて、私はlibnotifyのpoiで他の関数を呼び出すことができますから通知します。notify_notification_new -method。 しかし、最後の行では、WriteLineメソッドで、デバッガは言う:
The program '...dll' has exited with code 0 (0x00000000).
も例外とエラーがありません。何がうまくいかない?それはドットネットコアの問題ですか?まだベータ版なので?
プロパティからテキストを取得する方法 app_name、summary、body ??
ご協力いただきありがとうございます。
ご回答ありがとうございます。意味がある。しかし: NotifyNotification-structに私のnot-pointerをマーシャリングした後、privの値は_0x0000000000000001_です。 私はそれをNotifyNotificationPrivate-structにマーシャリングできません。どうして?それは他の価値でなければなりません。 – bittnerd
@bittnerdああ、GObjectは構造体だからポインタではないから。 https://github.com/GNOME/glib/blob/master/gobject/gobject.h#L245-L252。あなたの最善の策は、 'NotifyNotificationPrivate *'を 'NotifyNotification *'に渡すことができる小さなCライブラリを作ることです。そうすれば、Cコンパイラはフィールドオフセットの計算を行うことができ、GNOMEフレームワークのバージョン間でGObjectの構造を変更しようとする必要はありません。 – bartonjs
ご協力いただきありがとうございます。 実際、私は余分なライブラリを使ってそのようにしたくありませんでした。しかし、私はこれが最善の方法だと思うし、これらの価値を得る別の方法を知らない。 このコメントを回答に追加すると、私はそれを受け入れます。 :) – bittnerd