【发布时间】:2013-07-08 04:48:00
【问题描述】:
我正在尝试学习如何围绕 DLL 编写包装器,但遇到了一些障碍。我有一个这样声明的结构:
[StructLayout(LayoutKind.Sequential)]
unsafe struct SDL_Surface
{
public readonly UInt32 flags;
public readonly SDL_PixelFormat* format;
public readonly int w, h;
public readonly int pitch;
public void* pixels;
/// <summary>Application data associated with the surface</summary>
public void* userdata;
/// <summary>information needed for surfaces requiring locks</summary>
public readonly int locked;
public readonly void* lock_data;
/// <summary>clipping information</summary>
public readonly SDL_Rect clip_rect;
/// <summary>info for fast blit mapping to other surfaces</summary>
private SDL_BlitMap *map; // <--- Cannot take the address of, get the size of, or declare a pointer to a managed type
/// <summary>Reference count -- used when freeing surface</summary>
public int refcount;
}
当我尝试编译我的项目时,它给出了上述错误。
但是你会注意到它上面,我确实有一个指向另一个结构的指针。我试图弄清楚这两个结构之间的区别是什么使一个工作但另一个不工作,但我不确定;它们都是不安全的结构。它们如下:
[StructLayout(LayoutKind.Sequential)]
unsafe struct SDL_PixelFormat
{
UInt32 format;
SDL_Palette *palette;
byte BitsPerPixel;
byte BytesPerPixel;
fixed byte padding [2];
UInt32 Rmask;
UInt32 Gmask;
UInt32 Bmask;
UInt32 Amask;
byte Rloss;
byte Gloss;
byte Bloss;
byte Aloss;
byte Rshift;
byte Gshift;
byte Bshift;
byte Ashift;
int refcount;
SDL_PixelFormat *next;
}
unsafe internal delegate int SDL_blit(SDL_Surface* src, SDL_Rect* srcrect, SDL_Surface* dst, SDL_Rect* dstrect);
[StructLayout(LayoutKind.Sequential)]
unsafe struct SDL_BlitMap
{
SDL_Surface* dst;
int identity;
SDL_blit blit;
void* data;
SDL_BlitInfo info;
/* the version count matches the destination; mismatch indicates
an invalid mapping */
UInt32 dst_palette_version;
UInt32 src_palette_version;
}
[StructLayout(LayoutKind.Sequential)]
struct SDL_Rect
{
int x, y;
int w, h;
}
那么我必须改变什么才能使这个编译?
我相信是SDL_BlitMap 中对SDL_blit 的引用导致了问题。我已将其声明为代表;还有什么我应该声明的吗?在 C 语言中是这样定义的:
typedef int (*SDL_blit) (struct SDL_Surface * src, SDL_Rect * srcrect,
struct SDL_Surface * dst, SDL_Rect * dstrect);
【问题讨论】:
-
你能向我们展示你对
SDL_Rect的声明吗? -
@SimonWhitehead:添加到问题中。
-
SDL_Surface怎么样?这是破坏链条的东西..因为到目前为止,这是为我编译的。 -
@SimonWhitehead:在顶部添加了课程的其余部分。
-
您可能需要确保您的代理具有正确的调用约定 (
cdecl)。请参阅this question 了解如何执行此操作的示例。
标签: c#