12-10-2018 02:52 AM
Hi everyone,
This is probably an issue which is easy solved by you guys but I can' figure it out.
I'm running the same code, once when i execute it directly in main and the other when I'm calling it using a function.
The one in the main works, but not the other one. I'm not suer what I'm doing wrong here.
When leaving the function, I get an General Protection Fault which dosnt say much.
This is the main function, where I first INIT, then get the value from ReadAnalogInput.
This works fine!
This is the other case, where I call a function which is defined in another file.
There I'm doing the same thing, the values i correct but leaving that function (I'm at the breakpoint in red), it crash.
I have uploaded the project if that helps.
What's wrong?!
Solved! Go to Solution.
12-10-2018 05:26 AM - edited 12-10-2018 05:27 AM
**UPDATE**
I will update the issue since it will help understand the error:
This is the main code:
In the Test Function I do following:
Where (*Tests) is defined as:
int short (__stdcall *Tests)(int channel, double *value);
and loaded as:
(Tests = (void*) GetProcAddress(dllHandle, "Test"))
And the DLL is just setting the value to 5.0
It's crash if I call this function from main, but works if I execute it directly from main. Easier to understand what I mean with that from the first post.
I simply dont get what's wrong here.
12-10-2018 06:05 AM
Change:
int short (__stdcall *Tests)(int channel, double *value);
To:
int short (__cdecl *Tests)(int channel, double *value);
I'm not 100% sure, so if someone with knowledge would like to explain what's happening I would be more than happy.
I started to read here and just test myself forward:
https://docs.microsoft.com/sv-se/cpp/cpp/cdecl?view=vs-2017
But I still don't get why it works when I called the function directly in the main, compared to failed when called inside a function.
01-01-2019 02:22 PM
To answer your last question:
You are trying to retrieve a parameter from a function that has been passed by value. When passing by value, c puts a private copy of the parameter value on the stack when calling, and DISCARDS it when the function returns. Pass a pointer to the variable instead.