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

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

问题描述

我创建一个C API,它隐藏在一个DLL文件的一些功能。

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

既然一切是C ++在里面的大部分功能对工作手柄直接映射到这个指针的API的内部。

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项目中使用我的图书馆时,我得到这个警告:无法解析typeref令牌(令牌)的类型;图像可能无法运行。

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.

<一个href=\"http://msdn.microsoft.com/en-us/library/h8027ys9%28VS.80%29.aspx\">http://msdn.microsoft.com/en-us/library/h8027ys9(VS.80).aspx

这不是什么大问题,因为它的工作原理,但它看起来不专业。

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

我不喜欢使用无效*:

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;

这将罚款,只要廉政局和指针大小相同的工作,但事实并非总是如此,它好像有得到一个特定于平台的指针大小的整数没有万无一失的方法。它具有相同类型的安全问题,因为无效*以及

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 $ 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

其实他们有一个宏这样的:

in fact they have a macro for this:

#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天全站免登陆