08-21-2013 10:30 PM
Hi,
CVI 2013 generate invalid code for function pointer.
For example, this code works fine in CVI 2012, but not in CVI 2013. Any idea?
#include <windows.h> #include <stdio.h> struct FunctionType { const char *name; void *func; }; static const struct FunctionType functions[] = { { "func", GetFullPathName }, }; typedef DWORD (WINAPI *GetPathFunc)(LPCSTR, DWORD, LPSTR, LPSTR *); int main(int argc, char *argv[]) { char buf[256]; GetPathFunc f = (GetPathFunc)functions[0].func; if (f != GetFullPathName) { puts("Invalid ptr"); } else { if (f(argv[0], sizeof(buf), buf, NULL) > 0) { puts(buf); } } return (0); }
Solved! Go to Solution.
08-22-2013 04:06 AM
Hello hglee,
Your code looks fine. I have filed bug report #423480 to track the issue.
Our new compiler is importing GetFullPathName inconsistently. In the main function, it refers directly to the implementation in kernel32.dll. In the static variable, it refers to the "import pointer" for the function (pointer to the implementation). Interestingly, in CVI 2012 we refered to the "import stub" in both cases (a jump through the pointer to the implementation).
As a temporary workaround, you can manually undo the extra level of indirection by changing one line in your code:
GetPathFunc f = *(GetPathFunc*)functions[0].func;
However, this is DANGEROUS because, depending on how we decide how to fix the problem, the workaround may become a bug itself in the future and it may crash your program. So please keep this in mind when you modify your code (for example, add a comment and a link to this post to remind you).
Thanks,
Peter
08-22-2013 05:52 AM
I'll wait for next service pack.
Thanks.