Take the 2-minute tour ×

I need to use a function in a obj file for which I don't have the source code. The function works well and does its job but it corrupts the stack (runtime esp was not properly saved.. error in visual studio c++ 2010), and I don't know assembly well enough to find the problem and patch it. (From what Ive read it has to do with not having an equal number of pops and pushes.) I have tried executing the function in a separate thread but since it shares the same memory as my main program, it also crashes. The function works and does everything it needs to do, expect that it corrupts the stack and make the application crash. Is there any way I can isolate it from my main application? Ive read about CreateProcess but from what I understood it executes a separate .EXE file which I don't want to do. I want to execute a function as if it was an external program, so it does not crash my main program even though it corrupts the stack. Anyway to do such a thing in C/C++? Or maybe someone has another idea to fix things up? Also, I don't care if its "dirty" or not standard compliant since im writing this program for personal use. Since the function does what it needs to do Im really trying to keep it even though it corrupts the call stack.

Thank you very much

share|improve this question
"The function works well and does its job but it corrupts the stack" - works well? Clearly not!!! –  Mitch Wheat Jun 19 '11 at 4:13
Well it does what I want it to do, im just looking for a way to keep it, if you know what I mean –  jack excell Jun 19 '11 at 4:16
have you considered that you might not be calling it correctly? –  Mitch Wheat Jun 19 '11 at 4:21
add comment

1 Answer

That smells very much like a calling convention mismatch. Is it documented anywhere how the function is to be called? Do you have a header file containing a function prototype for the function? Try calling it with a different calling convention—if you're calling it as cdecl, try calling it as stdcall, or vice-versa.

See also What can go wrong when you mismatch the calling convention?.

share|improve this answer
add comment

Your Answer


