4

This is a code organization question.

I got my basic code working but when I expand it, it will be terrible. I have a DLL which I don't have a .lib for. Therefore I have to use the whole LoadLibrary()/GetProcAddress() combo. It works great. But this DLL that I'm referencing has 100+ functions. My current process is:

1) Typedef a type for the function. E.g.: typedef short(_stdcall *type1)(void);

2) Assign a function name that I want to use such as type1 function_1;

3) Load the DLL, then do something like: function_1 = (type1)GetProcAddress(hinstLib, "_mangled_funcName@5");

Normally I would like to do all of my function definitions in a header file but because I have to use the load library function, its not that easy. The code will be a mess. Right now I'm doing (1) and (2) in a header file and was considering making a function in another .cpp file to do the load library and dump all of the (3)'s in there. I considered using a namespace for the functions so I can use them in the main function and not have to pass over to the other function.

Any other tips on how to organize this code? My goals are to be able to use function_1 as a regular function in the main code. If I have to a ref::function_1 that would be okay but I would prefer to avoid it. This code for all practical purposes is just plain C at the moment.

3 Answers 3

2

You can try to generate .lib file from .dll: https://wiki.videolan.org/GenerateLibFromDll/

After that the task is much simpler and cleaner - to create a header with function declarations. This can be done manually or (semi-)automated.

5
  • Link only answers aren't generally very useful as links may die, and they're often hardly disguised spam, making people highly suspicious of clicking links. At the very least give a short summary of what the resource you're linking to says that's relevant to the question you're trying to answer.
    – jwenting
    Commented Jun 11, 2014 at 9:56
  • @jwenting: sorry but you got it wrong. It's not "link only" answer. It's an idea how to solve the problem (quite simple idea that can be expressed in couple of words) with a link to an example proving this is doable. Simple search provides couple of such links, e.g.: stackoverflow.com/questions/9360280/… Commented Jun 11, 2014 at 11:35
  • wow I didn't realize it was that easy to make a .lib file. that would probably be the cleanest solution. The idea itself was extremely helpful and the link made it I will go give it a shot. thanks!
    – m25
    Commented Jun 11, 2014 at 18:59
  • @m25: your entire comment could be expressed by single click "upvote" :) Commented Jun 11, 2014 at 20:15
  • I need a rep of 15. i'm a mere 11. or else I would totally upvote. I did accept it though
    – m25
    Commented Jun 12, 2014 at 20:44
1

Create a compact meta format (AKA a DSL) for describing the function signatures and write a code generator which reads that meta-format and produces all of the needed headers and cpp files for the loadLibrary code. So you don't have to bother with the readability of the C or C++ code too much, just make sure your meta-format is well readable.

5
  • I can't say I've ever heard of this process before. Do you know of another post or a site where there is information on this process?
    – m25
    Commented Jun 9, 2014 at 17:22
  • 1
    @m25: really, you have never heard of code generation? Maybe you have heard of buzzwords like "domain specific languages". I recommend to get a copy of "The pragmatic programmer", there you will find tips like this one: pragmatictips.com/29
    – Doc Brown
    Commented Jun 9, 2014 at 21:27
  • I was actually referring to the compact meta format. I had heard of code generation but had never heard much or tried to implement it. Thanks for the link and the tips. I like the code generation idea. thanks for your help!
    – m25
    Commented Jun 10, 2014 at 13:26
  • @m25: choose a meta format or DSL which fits best to your environment. It could be a plain and simple text file, an XML file, a C-like description similar to a CORBA-IDL spec, a machine-readable UML diagram (if you use CASE tools). Your choice should depend on the tools you have experience with. For example, if you can use a scripting language like Perl or Python for the code generator, there are tons of libraries available for parsing text files, XML files or even a complete language.
    – Doc Brown
    Commented Jun 10, 2014 at 13:47
  • @m25: here blogs.perl.org/users/jeffrey_kegler/2012/08/… is an entry point for Perl, and here stackoverflow.com/questions/1547782/mini-languages-in-python one for Python. But if you prefer something different, just google "create dsl with <language of your choice>"
    – Doc Brown
    Commented Jun 10, 2014 at 13:55
0

A code generator script would be a very neat solution, you should investigate.

If you want to stick to C only, you could probably clean up the code a bit using a couple macros. Maybe one to declare the typedef and the variable in one go, similar to this:

#define FUNC_DECL(retType, name, param)\
     typedef retType (_stdcall * name##_f)(param);\
     name##_f name;

And one to initialize the variable in a cleaner way:

#define FUNC_LOAD(name, lib)\
     name = (name##_f)GetProcAddress(lib);

So you would now just need to add a FUNC_DECL(function_X) in the global scope of a C file and call FUNC_LOAD(function_X, lib) in some initialization function. You would still have to declare an extern statement in a header file however, if function_X is to be visible elsewhere. E.g.: extern function_X_f function_X;

2
  • thanks for tip @glampert that would help clean things up. I'll give it a swing. I had never used the ## operator. did a little research on it. looks pretty useful. thanks again!
    – m25
    Commented Jun 10, 2014 at 13:49
  • Yes, it is called the Concatenation operator. You can get pretty creative with it and do a lot of cool stuff. gcc.gnu.org/onlinedocs/cpp/Concatenation.html
    – glampert
    Commented Jun 10, 2014 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.