views:

58

answers:

3

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.

+3  A: 

Use

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

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

Dirk Eddelbuettel
if I declare the private functions as static and put them in a library, would I be able to call it from my Server project i.e. from Server_GetPacket() ?
emge
Functions in the same *file* will see them. Others will not.
Dirk Eddelbuettel
+1  A: 

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.

Mark Wilkins
I am more concerned about providing a clean API that only contains the public functions. I just want to avoid having all the helper functions be part of the API. Are you saying that I create a static library for the "private" functions and then put the "public API" functions in a DLL?
emge
@emge: That is one possibility. It depends on your development architecture a bit on whether that makes sense. A "simpler" solution would be to simply include the source files in your project and build them directly into the binary. Then build the public API DLL as a separate project with the desired exported functions.
Mark Wilkins
Thanks, I like the "simpler" solution, but how do I get around the issue of having duplicate code if I just include the source file in my project and the API DLL needs to use some of those same functions?
emge
@emge: I probably am not understanding the question. But can't you just put the shared code in something like sharedcode.c and then include that one file (or multiple files as needed) in both client and server projects? The code is built into both projects then, but I don't think that violates the DRY rule.
Mark Wilkins
Ok thanks, I am going to try having a common.c file and see if I can get everything to work that way. What is the DRY rule?
emge
@emge: DRY = Don't Repeat Yourself. Sorry - I shouldn't have used acronyms. http://en.wikipedia.org/wiki/Don't_repeat_yourself
Mark Wilkins
Ah yes, thanks again.
emge
A: 

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 here 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.

bde