Click here to Skip to main content
16,016,781 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: dynamically calling dll functions Pin
munawar196830-May-05 19:08
munawar196830-May-05 19:08 
GeneralRe: dynamically calling dll functions Pin
Cedric Moonen30-May-05 21:20
Cedric Moonen30-May-05 21:20 
GeneralRe: dynamically calling dll functions Pin
munawar196830-May-05 22:09
munawar196830-May-05 22:09 
GeneralRe: dynamically calling dll functions Pin
Tom Archer31-May-05 2:50
Tom Archer31-May-05 2:50 
GeneralRe: dynamically calling dll functions Pin
Tom Archer31-May-05 2:49
Tom Archer31-May-05 2:49 
GeneralRe: dynamically calling dll functions Pin
Ryan Binns30-May-05 22:46
Ryan Binns30-May-05 22:46 
GeneralRe: dynamically calling dll functions Pin
Tom Archer31-May-05 2:52
Tom Archer31-May-05 2:52 
GeneralRe: dynamically calling dll functions Pin
Ryan Binns31-May-05 3:21
Ryan Binns31-May-05 3:21 
Tom Archer wrote:
I kinda figured that inline assembler would be needed to push the params onto the stack, but that's exactly what I don't know how to do.

OK. I'll give a few examples. Hopefully you can extend them.

Say you've got the address of a function in a variable called funcPtr and you need to pass a float and an int and the function returned a char, then you could do something like this (assuming C style calling convention):
char callFunction(void *funcPtr, float parm1, int parm2)
{
  __asm push parm1
  __asm push parm2
  __asm call funcPtr
}
Of course this can be extended for more arguments, and it should work for doubles (64-bit values) as well. I'm not sure how it works for values less than 32 bits. I think the compiler pushes an entire zero-extended 32-bit value, so you'll need to take that into account (store smaller values in a 32-bit temporary variable first).

You should be able to put this in a loop as follows:
char callFunction(void *funcPtr, int *values, int numValues)
{
  for (int i=0; i<numValues; i++)
  {
    __asm push values[i]
  }
  __asm call funcPtr
}
It should work alright, but I have never had to do this, so I'm not 100% certain. If it has trouble with the array index, assign values[i] to a temporary variable first (make sure it's local to the function, not the for loop, or you'll have problems with stack corruption).

As I mentioned before, another way I've done it is to allocate a block of memory for the actual parameter contents as they would appear on the stack, and the copy it all in one go. It makes the inline assembly much easier and predictable:
char callFunction(void *funcPtr, void *stackPtr, int stackSize)
{
  // Make room on the stack for the parameters
  __asm sub esp,stackSize
  // Copy the parameters to the stack
  __asm mov esi,stackPtr   // Source address
  __asm mov edi,esp        // Destination address
  __asm mov ecx,stackSize  // Number of bytes to copy
  __asm rep movsb          // Copy the bytes
  // Call the function
  __asm call funcPtr	
}
This version I have taken directly from some code I have written and tested, so I know it works. This is what I would recommend doing, as you can then assemble the stack information without using inline assembly (pointer arithmatic is much easier to understand Wink | ;) ) and just copy the data and call the function using a simple consistent method.

[edit]If you use the last technique, remember that for the C calling convention the parameters are passed in such a way that the highest address has the right-most parameter.[/edit]

Hopefully that gives you enough information to get it working.

Cheers,

Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

GeneralRe: dynamically calling dll functions Pin
Ryan Binns31-May-05 4:09
Ryan Binns31-May-05 4:09 
GeneralRe: dynamically calling dll functions Pin
Tom Archer1-Jun-05 1:32
Tom Archer1-Jun-05 1:32 
GeneralRe: dynamically calling dll functions Pin
munawar196831-May-05 18:43
munawar196831-May-05 18:43 
GeneralRe: dynamically calling dll functions Pin
Tom Archer1-Jun-05 1:31
Tom Archer1-Jun-05 1:31 
GeneralRe: dynamically calling dll functions Pin
geo_m31-May-05 20:01
geo_m31-May-05 20:01 
GeneralExe as child process Pin
Michael Klim30-May-05 10:42
Michael Klim30-May-05 10:42 
GeneralRe: Exe as child process Pin
Ravi Bhavnani30-May-05 15:45
professionalRavi Bhavnani30-May-05 15:45 
GeneralProgram executes automatically Pin
Identity Undisclosed30-May-05 9:27
Identity Undisclosed30-May-05 9:27 
GeneralRe: Program executes automatically Pin
Toni7830-May-05 10:41
Toni7830-May-05 10:41 
GeneralRe: Program executes automatically Pin
Identity Undisclosed30-May-05 10:57
Identity Undisclosed30-May-05 10:57 
GeneralRe: Program executes automatically Pin
Christian Graus30-May-05 14:06
protectorChristian Graus30-May-05 14:06 
GeneralRe: Program executes automatically Pin
Identity Undisclosed31-May-05 2:32
Identity Undisclosed31-May-05 2:32 
GeneralRe: Program executes automatically Pin
Toni7830-May-05 16:18
Toni7830-May-05 16:18 
GeneralRe: Program executes automatically Pin
Toni782-Jun-05 3:46
Toni782-Jun-05 3:46 
GeneralRe: Program executes automatically Pin
Tom Archer30-May-05 14:02
Tom Archer30-May-05 14:02 
GeneralRe: Program executes automatically Pin
Identity Undisclosed31-May-05 2:33
Identity Undisclosed31-May-05 2:33 
GeneralRe: Program executes automatically Pin
Ravi Bhavnani30-May-05 15:47
professionalRavi Bhavnani30-May-05 15:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.