LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVI 2013 generate invalid code

Solved!
Go to solution

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);
}

 

0 Kudos
Message 1 of 3
(3,526 Views)
Solution
Accepted by topic author hglee

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

Message 2 of 3
(3,505 Views)

I'll wait for next service pack.

 

Thanks.

 

0 Kudos
Message 3 of 3
(3,496 Views)