在“SDL.DLL”中找不到 SDL_LoadBMP() 入口点与 PInvoke

发布于 2024-12-23 04:21:37 字数 1140 浏览 2 评论 0原文

我正在尝试在 SDL 和我的 C# .NET 程序之间编组数据。我对 SDL.DLL 进行的前几次调用工作正常,因为我没有收到任何错误,并且我的 Windows 控制台应用程序确实打开了一个空的应用程序窗口:

My_SDL_Funcs.SDL_Init(0x0000FFFF); // SDL_INIT_EVERYTHING
IntPtr scrn = My_SDL_Funcs.SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, 0x00000000); // SDL_SWSURFACE
screen = (SDL_Surface)Marshal.PtrToStructure(scrn, typeof(SDL_Surface));
My_SDL_Funcs.SDL_WM_SetCaption("Hello World", null);
// ...

但是,当我尝试调用 SDL_LoadBMP() 时,我收到此运行时错误:

无法在 DLL“SDL”中找到名为“SDL_LoadBMP”的入口点。

SDL 文档说 SDL_LoadBMP 采用 const char* 文件名并返回指向 SDL_Surface 结构的指针。

我首先尝试将 PInvoke 声明为:

[DllImport("SDL", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr SDL_LoadBMP([MarshalAs(UnmanagedType.LPWStr)] string file);

当这不起作用时,我尝试:

public static extern IntPtr SDL_LoadBMP(IntPtr file);

并使用:

IntPtr fn = Marshal.StringToHGlobalAnsi(filename);
IntPtr loadedImage = My_SDL_Funcs.SDL_LoadBMP(fn);

假设该函数确实存在于该库(SDL.DLL 版本 1.2.14)中,我是否使用了错误的调用常量字符*?

I'm trying to marshal data between SDL and my C# .NET program. The first few calls that I make into SDL.DLL work fine, inasmuch as I get no error and my Windows console app does open an empty application window:

My_SDL_Funcs.SDL_Init(0x0000FFFF); // SDL_INIT_EVERYTHING
IntPtr scrn = My_SDL_Funcs.SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, 0x00000000); // SDL_SWSURFACE
screen = (SDL_Surface)Marshal.PtrToStructure(scrn, typeof(SDL_Surface));
My_SDL_Funcs.SDL_WM_SetCaption("Hello World", null);
// ...

When I try to call SDL_LoadBMP() however, I get this runtime error:

Unable to find an entry point named 'SDL_LoadBMP' in DLL 'SDL'.

The SDL doc says that SDL_LoadBMP takes a const char* file name and returns a pointer to a SDL_Surface struct.

I first tried declaring the PInvoke as:

[DllImport("SDL", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr SDL_LoadBMP([MarshalAs(UnmanagedType.LPWStr)] string file);

When this didn't work, I tried:

public static extern IntPtr SDL_LoadBMP(IntPtr file);

and used:

IntPtr fn = Marshal.StringToHGlobalAnsi(filename);
IntPtr loadedImage = My_SDL_Funcs.SDL_LoadBMP(fn);

Assuming that that the function actuall does exist in this library (SDL.DLL version 1.2.14), am I using the wrong invocation for a const char*?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月松间行 2024-12-30 04:21:37

我下载了您正在使用的 DLL 版本,但找不到 SDL_LoadBMP 的导出。

不过,有一个 SDL_LoadBMP_RW,因此您可以像这样设置自己的帮助程序调用:

private const string SDL = "SDL.dll";

[DllImport(SDL, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern IntPtr SDL_LoadBMP_RW(IntPtr src, int freesrc);

[DllImport(SDL, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern IntPtr SDL_RWFromFile(string file, string mode);

public static IntPtr SDL_LoadBMP(string file)
{
    return SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1);
}

更新

我查看了代码,您正在查找的调用被定义为宏,因此这就是为什么你不能直接调用它。使用上面的代码基本上与宏定义做同样的事情:

#define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)

I downloaded the DLL version you are using, and could not find an export for SDL_LoadBMP.

There is a SDL_LoadBMP_RW, though, so you could rig up your own helper call like so:

private const string SDL = "SDL.dll";

[DllImport(SDL, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern IntPtr SDL_LoadBMP_RW(IntPtr src, int freesrc);

[DllImport(SDL, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern IntPtr SDL_RWFromFile(string file, string mode);

public static IntPtr SDL_LoadBMP(string file)
{
    return SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1);
}

UPDATE:

I had a look through the code, and the call you are looking for is defined as a macro, so that is why you can't call it directly. Using the above code basically does the same thing as the macro defintion:

#define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文