ここで何が問題なのかわかりません。
何事もなく機能しているp/invoke呼び出しがたくさんあります...これを除いて。
私は問題を次のサンプルコードに減らすことができました。
いずれかの構造体メンバー(doubleまたはint)を削除すると、正常に機能します。
問題は構造体のレイアウトに何らかの形で関連していると思いますが、Cでsizeof()を実行し、C#でMarshal.SizeOf()を実行すると、両方とも同じ値を返します...したがって、構造体のサイズがC#とCで同じですが、問題は何でしょうか?
私は明らかにここで基本的な何かが欠けています。
SampleDLLCode.c
#pragma pack(1)
typedef struct SampleStruct {
double structValueOne;
int structValueTwo;
} SampleStruct;
__declspec(dllexport) SampleStruct __cdecl SampleMethod(void);
SampleStruct SampleMethod(void) {
return (SampleStruct) { 1, 2 };
}
スクリプトを作成する
gcc -std=c99 -pedantic -O0 -c -o SampleDLLCode.o SampleDLLCode.c
gcc -shared --out-implib -o SampleDLL.dll SampleDLLCode.o
C#コード
using System;
using System.Runtime.InteropServices;
namespace SampleApplication
{
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct SampleStruct {
public double structValueOne;
public int structValueTwo;
}
class Program
{
[DllImport("SampleDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern SampleStruct SampleMethod();
static void Main(string[] args)
{
SampleStruct sample = SampleMethod();
}
}
}