but I compile this code to a dll, and then call it from LabView.
When I run it outside the dll, it works fine; but when I try to do thorugh the dll, the behaviour is really strange: sometimes it works, others, not...
Could you please tell me if I'm missing something?
This does not appear to be sourced for DLL linking/compilation. You don't have any exposed functions to call from external apps, you don't have a dll main but instead an int main function which is used in console applications.
What IDE are you using? It hopefully has a template project of dll that you can make to see what it looks like? Else search the web for the topic, there's hundreds of articles out there. Depending on your OS and your compiler, you may want to define the externals one way or another.