Quejebo

I have an unmanaged function of signature foo(int a, ...){} using the va_args functionality. I have a managed hashmap, which stores a pointer to this function. How do I cast the reference back into a useable function My current code looks like:

(void(__cdecl*)(int, ...))hash["name"](param1, param2, param3);

this gives me compiler error C2064.

any thoughts



Re: Visual C++ Language problem casting a function with variable arguments

einaros

Split it into separate statements, such as

Code Snippet

void (__cdecl *funcptr)(int, ...);

funcptr = reinterpret_cast<void(__cdecl *)(int, ...)>(hash["name"]);

(*funcptr)(param1, param2, param3);






Re: Visual C++ Language problem casting a function with variable arguments

Quejebo

this almost works. I now get a runtime error (text follows) on the line (*funcptr)(param1, param2, param3);

First-chance exception at 0x012f5b9e in main.exe: 0xC0000005: Access violation writing location 0x00000000.

A first chance exception of type 'System.AccessViolationException' occurred in CommonHC11Firmware.dll

An unhandled exception of type 'System.AccessViolationException' occurred in CommonHC11Firmware.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.





Re: Visual C++ Language problem casting a function with variable arguments

Quejebo

Code Snippet

void (__cdecl *funcptr)(const int, ...);

IntPtr t = (IntPtr) hash["name"];

funcptr = reinterpret_cast<void(__cdecl *)(const int, ...)>((void*)t);

(*funcptr)(param1, param2, param3);

fixed with this change




Re: Visual C++ Language problem casting a function with variable arguments

einaros

It's likely to be related to function pointer or the use of var-args, but it's not caused by the code provided in this post. Use the debugger and see what's attempted written, which pointer is 0, and so forth. That should help you narrow it down to the culprit.