アンマネージ コードと C# UI を含む C++ DLL があります。私が書いた構造体をパラメーターとして受け取るC++ DLLからインポートされた関数があります。
私が書いた構造体 (MyImage) を C# から C++ にマーシャリングした後、その中の int[] 配列の内容にアクセスできますが、内容は異なります。かなりの時間を費やし、これを解決するためにいくつかのトリックを試したので、ここで何が欠けているのかわかりません (明らかに十分ではありません)。
C# の MyImage 構造体:
[StructLayout(LayoutKind.Sequential)]
struct MyImage
{
public int width;
public int height;
public int[] bits; //these represent colors of image - 4 bytes for each pixel
}
C++ の MyImage 構造体:
struct MyImage
{
int width;
int height;
Color* bits; //typedef unsigned int Color;
MyImage(int w, int h)
{
bits = new Color[w*h];
}
Color GetPixel(int x, int y)
{
if (x or y out of image bounds) return UNDEFINED_COLOR;
return bits[y*width+x];
}
}
MyImage をパラメーターとして使用した C# 関数宣言:
[DLLImport("G_DLL.dll")]
public static extern void DisplayImageInPolygon(Point[] p, int n, MyImage texture,
int tex_x0, int tex_y0);
C++ 実装
DLLEXPORT void __stdcall DisplayImageInPolygon(Point *p, int n, MyImage img,
int imgx0, int imgy0)
{
//And below they have improper values (i don't know where they come from)
Color test1 = img.GetPixel(0,0);
Color test2 = img.GetPixel(1,0);
}
そのため、問題をデバッグしているときに、c++ 構造体の MyImage.bits 配列が異なるデータを保持していることに気付きました。
どうすれば修正できますか?