2

一般的な関数を DllImport できますが、この種のインポートに失敗しました。以下は DLL ヘッダー ファイルです。

typedef struct
{
   VOID (* API_GetUID)(CHAR *pData, DWORD DataLen);

   DWORD (* API_GetChipType)();

} API_FUNCTION_STRUCT, *API_FUNCTION_STRUCT;

extern VOID WINAPI GetAPIObject(API_FUNCTION_STRUCT *pApiFunc);

C# で正しい構造体を記述できません。

public struct test
    {
        IntPtr  API_GetUID(IntPtr pData, int DataLen);
        IntPtr      API_GetChipType();
    } 

[DllImport(@"GDevice.dll")]
public static extern void GetAPIObject(ref test test_a);

アップデート:

public struct test
{
delegate void API_GetUID(IntPtr pData, int DataLen);
delegate void API_GetChipType();
}
4

1 に答える 1

2

おそらく関数を使用する必要がありますMarshal.GetDelegateForFunctionPointer

これはIntPtr、ネイティブ メソッドへのポイントを取得し、呼び出すことができるデリゲートを返します。

public struct test
{
    IntPtr API_GetUID;
    IntPtr API_GetChipType;
} 

[DllImport(@"GDevice.dll")]
public static extern void GetAPIObject(ref test test_a);

delegate void GetUID_Delegate(IntPtr pData, uint dataLen);
delegate uint GetChipType_Delegate();

test a = new test();
GetAPIObject(ref a);

GetUID_Delegate getUID = Marshal.GetDelegateForFunctionPointer<GetUID_Delegate>(a.API_GetUID);
GetChipType_Delegate getChipType = Marshal.GetDelegateForFunctionPointer<GetChipType_Delegate>(a.API_GetChipType);

uint chipType = getChipType();

編集

または、 UnmanagedFunctionPointerAttributeを使用します。

public struct test
{
    GetUID_Delegate API_GetUID;
    GetChipType_Delegate API_GetChipType;

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    delegate void GetUID_Delegate(IntPtr pData, uint dataLen);
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    delegate uint GetChipType_Delegate();
} 

[DllImport(@"GDevice.dll")]
public static extern void GetAPIObject(ref test test_a);

test a = new test();
GetAPIObject(ref a);

uint chipType = a.API_GetChipType();
于 2013-09-24T09:54:28.910 に答える