C 接口中句柄的正确类型 [英] The right type for handles in C interfaces

查看:44
本文介绍了C 接口中句柄的正确类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个在 DLL 文件中隐藏某些功能的 C api.

I'm creating a C api that hides some functionality in a DLL file.

由于内部的一切都是 C++,因此大多数函数都针对直接映射到 API 内部的 this 指针的句柄.

Since everything is C++ on the inside most of the functions works against handles which maps directly to this pointers on the inside of the API.

为了让这些句柄获得某种程度的类型安全,我这样定义它们:

To get a some degree of type safety for those handles I define them like this:

typedef struct MyType1* MyType1Handle;
typedef struct MyType2* MyType2Handle;

我实际上并没有在任何地方定义 MyType1 或 MyType2,因为我只将它们用作指针并在 api 内部将类型转换为实际的指针类型.

I don't actually define MyType1 or MyType2 at any place since I only use them as pointers and do a type cast on the inside of the api to the actual pointer type.

我的问题是,在 Visual Studio 的 clr 项目中使用我的库时,我收到此 warning: unresolved typeref token (token) for 'type';图像可能无法运行.

My problem is that when using my library in a clr project in visual studio I get this warning: unresolved typeref token (token) for 'type'; image may not run.

http://msdn.microsoft.com/en-us/library/h8027ys9(VS.80).aspx

没什么大不了的,因为它有效,但看起来不专业.

It's no big deal since it works, but it looks unprofessional.

我不喜欢使用 void*:

I don't like using void*:

typedef void* MyType1Handle;
typedef void* MyType2Handle;

这使得调用需要 MyType1Handle 和 MyType2Handle 的函数成为可能,因为它们实际上是相同的类型.

This makes it possible to call a function wanting a MyType1Handle with a MyType2Handle since they are actually the same type.

我不想使用的另一种方法是这样的

Another approach I don't want to use is something like this

typedef int MyType1Handle;
typedef int MyType2Handle;

只要 int 和指针具有相同的大小,这就会正常工作,但情况并非总是如此,似乎没有万无一失的方法来获取特定于平台的指针大小的整数.它也有与 void* 相同的类型安全问题.

This would work fine as long as int's and pointers have the same size, but that's not always the case and it seems like there is no foolproof way of getting a platform specific pointer sized integer. It has the same type safety problems as the void* as well.

我尝试过的另一种方法是这样做:

Another approach I tried was to do it like this:

struct MyType1{};
typedef struct MyType1* MyType1Handle;

这在 C 中不起作用,因为空结构是无效的 C 代码.我当然可以用一个虚拟成员扩展我的结构,但似乎应该有更好的方法来做到这一点.

This didn't work in C since empty structs is invalid C code. I could of course extend my struct with a dummy member, but it seems like there should be a better way of doing this.

所以我的问题归结为:

您通常如何以最兼容的方式指定此类类型?

How do you generally specify this kind of types in the most compatible way?

推荐答案

如果你看看微软如何定义它的 winapi 句柄 (winnt.h),它实际上是这样的:

If you look at how Microsoft defines it's winapi handles (winnt.h) it actually looks like this:

struct HWND__ { int unused; }; typedef struct HWND__ *HWND

事实上他们有一个宏:

#define DECLARE_HANDLE(name) struct name##__{int unused;}; typedef struct name##__ *name

所以……这似乎是一种常见的做法.不幸的是,除了你已经提出的这个建议之外,我无法提出其他建议提到了,但无论如何我希望它对您有所帮助.

so.. this seems to be a common practice to do so. Unfortunately I can't make another suggestion except this one, which you already mentioned, but I hope it helps you anyway.

这篇关于C 接口中句柄的正确类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆