开发者

What is a good way to transfer data to a DLL?

Here's my scenario:

I have made a regular DLL in C++ (using VS2008) which provides C interface to exported functions. Two of the concerning functions are 1. initialize() and 2. uninitialize()

Inside initialize() I have to take some parameters from client application. So far, I could think of following two ways to do this:

First way:

I make a structure, fill it and pass object/pointer of the structure object to the DLL from client application.

    struct SData
    {
        DataType param1;
        DataType param2;
        DataType param3;
        DataType param4;
    };

Second way:

I export 4 functions from开发者_如何转开发 my DLL like

    SetParam1(DataType param1)
    SetParam2(DataType param2)
    SetParam3(DataType param3)
    SetParam4(DataType param4)

Which way or some other should I use, please suggest.


Since DLL shares the address space with your application, you can handle it all just by sending a pointer to your struct. If you happen to use different version of the DLL with your application you might also employ some kind of version checking in your function to avoid getting mixed up by different versions of it.

I personally do not recommend SetParam(param1) method because it implies a global scope in the DLL thus is not thread-safe. On the other hand passing all parameters to the function itself has a local scope, less prone to bugs, more contained, easier to understand.

For instance you don't know how many functions you need to call before your actual function with the SetParam method. But passing parameters in the function allows you to assess if you provide enough parameters just by looking at the function declaration or the struct members. In the case where you need to call individual methods to set parameters, you must have documentation.

Similarly you can put each struct member as a separate parameter in your function but too many parameters become harder to maintain in a single function call after a certain number. Using pointer to a struct in that case is better.

Think yourself as someone who purchased the DLL from a company. How would you prefer the DLL to be? Which way would be easier, more straightforward to write code on?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜