Link to home
Start Free TrialLog in
Avatar of cellcast
cellcast

asked on

Dll Dynamic way c/c++

I have c++ console application and c++ dll. I loaded dll dynamicall using loadlibrary function.
Now i want to call the exported function of dll without declaring its signature and without using library.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cellcast
cellcast

ASKER

thanx !

But during compile time my application doesnt know the signature. during execution i will fetch dll name, function name and signature from text file and accordingly will pass the parameter to function.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What you are describing appears to be a very basic form of interpreter.
e.g.  Operation1 int arg1, double arg2, char arg3

I am guessing you have already written the instruction parser and are now trying to implement the execution engine.

An alternative (admittedly more complex) is to study the generated assembler code for each argument type in some simple (_cdecl style ?) function calls containing all the supported argument types and make an equivalent sequence of assembler instructions to push each argument onto the stack.
Then execute a simple "CALL" instruction with the function address retrieved from the DLL.


IMHO: What you propose is an EXTREMELY risky implementation because that text file is a completely open invitation to being hacked.
As I understand your main app is supposed to load DLLs, using some configuration file which descrives module and all function entries. So, the application is dealing with some abstraction and should not contain any module dependent logic. In other hand you want to implement logic to pass C++ function arguments with random types and sizes into program stack and then call function from DLL by pointer.  there is a couple risky solutions for you.

For example:

- Use a strong rule to declare DLL entries with __cdecl specification, ( caller is responsible for destroying arguments).
- Declare only one function entry type in your main app with undefined arguments count.
- Use this declared entry type to call imported functions with any count of arguments.

This code is just illustration, do not run it, because it is a dead loop.

typedef int (__cdecl *  proc_entry_t)(...);

int _tmain(int argc, _TCHAR* argv[])
{
	proc_entry_t proc=(proc_entry_t)_tmain;
    
        return (*proc)(1,NULL);
}

Open in new window


But in fact your logic has a general collision. Trying to pass arguments you will create module-dependent logic inside your app. The better solution is (if it is possible of course) to use some arguments list, and make each function exported from DLL with the same prototype, like that:

typedef int (__cdecl *  proc_entry_t)(argiments_list_t* args);

Open in new window

You can write a program or use some kind of RegExp parser to convert your text file to .h header file with function prototypes during compile time - if the text file is not supposed to be changed during execution time. Then, you can include this header file into your project and use function signatures.

But if you really want to do it all dynamically on-the-fly (including parameter passing), you really have to write some kind of interpreter which would generate machine instructions which would handle parameters passing and calling conventions handling. These instructions in a memory buffer later can be executed natively by passing program control to it. But this is rather complex algorithm.