views:

93

answers:

3

Hello, I have a complex problem to be solve, as I am stuck and found no way at all to solve this. Here's a code

struct MyStruct
{
    int x;    
    float y;
    char c;
};

void foo(MyStruct a_myStruct);

int _tmain(int argc, _TCHAR* argv[])    
{
    void *pMyStruct = malloc(sizeof(MyStruct));
    int* pInt = (int*)pMyStruct;

    *pInt = 10;    
    pInt++;

    float *pFloat = (float*)pInt;    
    *pFloat = 2.545;   
    pFloat++;

    char *pChar = (char*)pFloat;
    *pChar = 'c';

    _asm
    {
        pMyStruct
        call foo
    }

    return 0;
}

void foo(MyStruct a_myStruct)
{
}

Here you can see that foo is operating object on stack which is expecting stack object to be mapped when foo gets called. But unfortunately above MyStruct type not known at compile time so I have to create block of memory and then at runtime data is filled with in that block which is then passed when foo get's called with asm as show above.

Now how can i convert heap void pointer to stack type object. Somehow if i get the address location of a_myStruct argument of foo, I can point with void* to that location but again i cant dereference void* so that it get converted into object of MyStruct type.

Any other way around to solve the problem..? As in C++ we can determine type at runtime as well

Regards Hassan

+1  A: 
AShelly
I think copying the data on to stack might help but unfortunately it's not getting work here. It fails to copy properly. More over here MyStruct is a complete type, which is not known at compile time. So a char array won't work here.
Hassan
A: 

@All: I have a problem to call functions at run time in C++ which might have signature with full of user defined types that are not known at compile time.But details of those types are available to me(As I deciphered details of certain type from type library or from DIA SDK). But the main problem is now i want to call these functions at runtime. At compile time I just have address of function and details of user defined type of which object or pointer is participating as argument of that function signature. Now If i want to call that function at run time, i need to populate that type at runtime first by creating temporary block on heap and filling that block with the data .I have all details of that type.

Now problem is i don't know that function taking argument as pointer of that type of which I have details available or that argument is exactly the stack object of that type. If i have pointer to that type no problem, but if object there i have big problem to call that function at runtime.

Hassan
@Hassan: Please do not post additional explanations to your original question as an answer. Instead, you should *edit* your question (if you can) to provide more information.
stakx
+1  A: 
stakx