Forum
Using cnc_exeprgname2() to retrieve program path on C# Console Application
Quote from Mattia Donnini on March 18, 2022, 12:03 pmHi guys, i am new to Focas/Fanuc Programming, i followed this post to start my first app -> https://hierthinking.com/2020/04/25/its-time-to-focas-p1/
Everything works fine and i can connect with the CNC using cnc_allclibhndl3() function.
I then tried to find a function that returns the path of program currently running on the machine and i found the function cnc_exeprgname2().
I did not found the declaration on fwlib32.cs so i added it like this:
[DllImport("FWLIB30i.dll", EntryPoint = "cnc_exeprgname2")]
public static extern short cnc_exeprgname2(ushort FlibHndl, out string path_name);When i call the function it returns with the error -8 that seems to be related with the Handle, i think that the error is caused by the following:
i connect with the machine using cnc_allclibhndl3() from FWLIB32 but when i try to retrieve the path i use cnc_exeprgname2() from FWLIB30i.Maybe this is the thing causing the problem, how can i solve it?
Hi guys, i am new to Focas/Fanuc Programming, i followed this post to start my first app -> https://hierthinking.com/2020/04/25/its-time-to-focas-p1/
Everything works fine and i can connect with the CNC using cnc_allclibhndl3() function.
I then tried to find a function that returns the path of program currently running on the machine and i found the function cnc_exeprgname2().
I did not found the declaration on fwlib32.cs so i added it like this:
[DllImport("FWLIB30i.dll", EntryPoint = "cnc_exeprgname2")]
public static extern short cnc_exeprgname2(ushort FlibHndl, out string path_name);
When i call the function it returns with the error -8 that seems to be related with the Handle, i think that the error is caused by the following:
i connect with the machine using cnc_allclibhndl3() from FWLIB32 but when i try to retrieve the path i use cnc_exeprgname2() from FWLIB30i.
Maybe this is the thing causing the problem, how can i solve it?
Quote from Versex on March 18, 2022, 12:48 pmHi Mattia,
Normally when I see this -8 error it is caused by trying to use the handle across multiple threads. Now, I am not sure if you are running this in a loop or if you are just calling it a single time. But just to clarify, if you are using something like a timer to repeatedly call the function, it will fail. This is because of the implementation of the timer in C#. Every time the elasped event is triggered, it pulls a thread from the thread pool to execute the function. This is problematic when using Focas. Once you obtain a handle on one thread, it cannot be used on any other thread.
Now, this may or may not be your issue. But the first step would to be see what is being returned from cnc_allclibhndl3() and also check the handle number to make sure it isn't 0. Once you have done that, if everything looks good, let me know and we can look further into the issue.
Hi Mattia,
Normally when I see this -8 error it is caused by trying to use the handle across multiple threads. Now, I am not sure if you are running this in a loop or if you are just calling it a single time. But just to clarify, if you are using something like a timer to repeatedly call the function, it will fail. This is because of the implementation of the timer in C#. Every time the elasped event is triggered, it pulls a thread from the thread pool to execute the function. This is problematic when using Focas. Once you obtain a handle on one thread, it cannot be used on any other thread.
Now, this may or may not be your issue. But the first step would to be see what is being returned from cnc_allclibhndl3() and also check the handle number to make sure it isn't 0. Once you have done that, if everything looks good, let me know and we can look further into the issue.
Quote from Mattia Donnini on March 21, 2022, 3:56 amHi Versex, thanks for the reply, i fixed the -8 error but i don't know if the method declaration is correct. In the fwlib32.cs i added the method like this:
[DllImport("FWLIB32.dll", EntryPoint = "cnc_exeprgname2")]
public static extern short cnc_exeprgname2(ushort FlibHndl, out char path_name);Is this correct? If yes how can i retrieve a full path if the method takes as parameter a single char??? I expected to pass a string, not a single char.
Hi Versex, thanks for the reply, i fixed the -8 error but i don't know if the method declaration is correct. In the fwlib32.cs i added the method like this:
[DllImport("FWLIB32.dll", EntryPoint = "cnc_exeprgname2")]
public static extern short cnc_exeprgname2(ushort FlibHndl, out char path_name);
Is this correct? If yes how can i retrieve a full path if the method takes as parameter a single char??? I expected to pass a string, not a single char.
Quote from Versex on March 22, 2022, 9:48 amHi Mattia,
Sorry, it took me a while to get back to you. I have been sick and I am just starting to get better. Anyway, the example you posted looks close to what you would need, but keep in mind that the Focas library was written in C and not everything translates well between C and C#. For that reason, it is necessary to take an extra step to abstract the second parameter out a bit by using a confusing bit of code for it. Now I have not tested this out... so if it doesn't work, let me know and I will be more than happy to take another look. But here is what I think you will need.
[DllImport("FWLIB32.dll", EntryPoint = "cnc_exeprgname2")]
public static extern short cnc_exeprgname2(ushort FlibHndl, [Out,MarshalAs(UnmanagedType.AsAny)] Object path_name);
This will hopefully work for you. If you look at some of the other functions that I know take string inputs such as nc_wrpdf_curdir, you will see they do the same thing for the string parameters. Hope this helps. Let me know!
Hi Mattia,
Sorry, it took me a while to get back to you. I have been sick and I am just starting to get better. Anyway, the example you posted looks close to what you would need, but keep in mind that the Focas library was written in C and not everything translates well between C and C#. For that reason, it is necessary to take an extra step to abstract the second parameter out a bit by using a confusing bit of code for it. Now I have not tested this out... so if it doesn't work, let me know and I will be more than happy to take another look. But here is what I think you will need.
[DllImport("FWLIB32.dll", EntryPoint = "cnc_exeprgname2")]
public static extern short cnc_exeprgname2(ushort FlibHndl, [Out,MarshalAs(UnmanagedType.AsAny)] Object path_name);
This will hopefully work for you. If you look at some of the other functions that I know take string inputs such as nc_wrpdf_curdir, you will see they do the same thing for the string parameters. Hope this helps. Let me know!
Quote from Mattia Donnini on March 23, 2022, 4:37 amHi Versex,
it works perfectly now, i call the method like this:
char[] path = new char[255];
ret = Focas1.cnc_exeprgname2(handle, path);Thanks for the help!
Hi Versex,
it works perfectly now, i call the method like this:
char[] path = new char[255];
ret = Focas1.cnc_exeprgname2(handle, path);
Thanks for the help!