简体   繁体   English

如何在C中从公共API隐藏帮助程序函数

[英]How to hide helper functions from public API in c

I'm working on a project and I need to create an API. 我正在做一个项目,我需要创建一个API。 I am using sockets to communicate between the server (my application) and the clients (the other applications using my API). 我正在使用套接字在服务器(我的应用程序)和客户端(其他使用我的API的应用程序)之间进行通信。

This project is in c not C++ 这个项目是用C不是C ++

I come from a linux background and this is my first project using Windows, Visual Studio 2008, and dll libraries. 我来自Linux背景,这是我使用Windows,Visual Studio 2008和dll库的第一个项目。

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. 我想创建一个库(可能是dll文件),两个项目都可以链接到该库,因此我不必维护额外的代码。

I also have to create the library that has the API that I need to make available for my clients. 我还必须创建具有使我的客户可用的API的库。 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. 我希望公开的API函数中包含对这些“复制代码”帮助函数的调用,我不想将这些函数公开给客户端,但是我希望服务器能够使用这些函数。 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. 谢谢任何帮助,将不胜感激。

Use 使用

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

etc to make the functions local to the file / compilation unit. 等功能使文件/编译单元本地化。

If you put the "private" functions in a DLL and make them externally callable by normal means (eg, callable by a process that loads the library), then they will be "public" for others as well. 如果将“私有”函数放在DLL中,并通过常规方式使它们在外部可调用(例如,可通过加载库的进程调用),那么它们对于其他人也将是“公共”的。 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. 最好将它们放在静态链接库中,而不是DLL中。 Note, though, that even in this case someone can disassemble the binary obviously and get to the code. 但是请注意,即使在这种情况下,也可以有人明显地反汇编二进制文件并进入代码。

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. 如果只想公开DLL中的某些功能,则可以创建在构建项目时提供给链接器的导出文件(dllname.def)。 This exports file contains a list of the functions that you want to make publicly available to applications using your DLL. 此导出文件包含要使用DLL公开提供给应用程序的功能列表。

See the MSDN article here for more information about this. 有关更多信息,请参见此处的MSDN文章。

I think you can also achieve similar functionality by using the __declspec(dllexport) keyword on the functions you want to export. 我认为您也可以通过在要导出的函数上使用__declspec(dllexport)关键字来实现类似的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM