如何在 c 中的公共 API 中隐藏辅助函数

发布于 2024-09-04 23:14:33 字数 1181 浏览 13 评论 0 原文

我正在开发一个项目,需要创建一个 API。我使用套接字在服务器(我的应用程序)和客户端(使用我的 API 的其他应用程序)之间进行通信。

这个项目是用 c 语言而不是 C++

语言编写的。我有 Linux 背景,这是我第一个使用 Windows、Visual Studio 2008 和 dll 库的项目。

我在客户端和服务器之间进行了通信,但是我的一些通信在两个项目上都是重复的。我想创建一个库(可能是一个 dll 文件),两个项目都可以链接到该库,这样我就不必维护额外的代码。

我还必须创建一个包含我需要为客户提供的 API 的库。在我想要公开的 API 函数中,对这些辅助函数的调用是“重复代码”,我不想将这些函数公开给我的客户端,但我确实希望我的服务器能够使用这些函数。我该怎么做?

我将尝试用一个例子来澄清。这就是我的开始。

服务器项目:

int Server_GetPacket(SOCKET sd);
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);

客户端项目:

int Client_SendCommand(int command);
int Client_GetData(int command, char *buf, int len);
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);

这就是我想要的结果:

//Server Project:
int Server_GetPacket(SOCKET sd);

// library with public and private types
//  private API (not exposed to my client)
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);
//  public API (header file available for client)
int Client_SendCommand(int command);
int Client_GetData(int command, char *buf, int len);

谢谢,任何帮助将不胜感激。

I'm working on a project and I need to create an API. I am using sockets to communicate between the server (my application) and the clients (the other applications using my API).

This project is in c not C++

I come from a linux background and this is my first project using Windows, Visual Studio 2008, and dll libraries.

I have communication working between the client and server, but I have some that is duplicated on both projects. I would like to create a library (probably a dll file), that both projects can link to so I don't have to maintain extra code.

I also have to create the library that has the API that I need to make available for my clients. Within the API functions that I want public are the calls to these helper functions that are "duplicated code", I don't want to expose these functions to my client, but I do want my server to be able to use those functions. How can I do this?

I will try to clarify with an example. This is what I started with.

Server Project:

int Server_GetPacket(SOCKET sd);
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);

Client Project:

int Client_SendCommand(int command);
int Client_GetData(int command, char *buf, int len);
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);

This is kind of what I would like to end up with:

//Server Project:
int Server_GetPacket(SOCKET sd);

// library with public and private types
//  private API (not exposed to my client)
int ReceiveAll(SOCKET sd, char *buf, int len);
int VerifyLen(char *buf);
//  public API (header file available for client)
int Client_SendCommand(int command);
int Client_GetData(int command, char *buf, int len);

Thanks any help would be appreciated.

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

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

发布评论

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

评论(3

梦断已成空 2024-09-11 23:14:33

使用

static int ReceiveAll(SOCKET sd, char *buf, int len);

etc使函数成为文件/编译单元的本地函数。

Use

static int ReceiveAll(SOCKET sd, char *buf, int len);

etc to make the functions local to the file / compilation unit.

岁吢 2024-09-11 23:14:33

如果将“私有”函数放入 DLL 中,并使其可以通过正常方式从外部调用(例如,可以由加载该库的进程调用),那么它们对于其他函数也将是“公共”的。可以混淆名称和类似的东西,但这可能不是一个好的解决方案。

将它们放入静态链接库而不是 DLL 可能会更好。但请注意,即使在这种情况下,也有人可以明显地反汇编二进制文件并获取代码。

If you put the "private" functions in a DLL and make them externally callable by normal means (e.g., callable by a process that loads the library), then they will be "public" for others as well. It is possible to obfuscate the names and things like that, but that is probably not a good solution.

It might be better to put those into a statically linked library as opposed to a DLL. Note, though, that even in this case someone can disassemble the binary obviously and get to the code.

梦中的蝴蝶 2024-09-11 23:14:33

如果您只想公开 DLL 中的某些函数,则可以创建一个导出文件 (dllname.def),在构建项目时将其提供给链接器。此导出文件包含您想要公开提供给使用 DLL 的应用程序的函数列表。

请参阅 MSDN 文章 使用 DEF 文件从 DLL 导出

我认为您也可以通过在要导出的函数上使用 __declspec(dllexport) 关键字来实现类似的功能。

If you want to only expose certain functions in your DLL, you can create an exports file (dllname.def) that you give to the linker when you build the project. This exports file contains a list of the functions that you want to make publicly available to applications using your DLL.

See the MSDN article Exporting from a DLL Using DEF Files for more information about this.

I think you can also achieve similar functionality by using the __declspec(dllexport) keyword on the functions you want to export.

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