Using ctAPI Library in c#

Join Date
Mar 2015
Location
Iran
Posts
3
Hi, My code is as below:
public void Init() { IntPtr hCTAPI = CTAPI.ctOpen(null, "u1", "1", 2); TestMe(hCTAPI); } [DllImport("ctapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)] public static extern IntPtr ctOpen(string sComputerName, string sUsername, string sPassword, UInt32 nMode); [DllImport("ctapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)] public static extern bool ctTagRead(IntPtr hCTAPI, [MarshalAs(UnmanagedType.LPStr)] string tag, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder value, int length); public static void TestMe(IntPtr ip) { var tag = "tag1"; var tagPtr = Marshal.StringToHGlobalAnsi(tag); System.Text.StringBuilder vsl = new StringBuilder(20); bool b = ctTagRead(ip, tag, vsl, 20);//, tagValueItemsPtr); }
bool b is always false. I dont know what to do!

I used GetLastError Function and I found out there is a "No connection could be made because the target machine actively refused it" error. but I am trying to connect to citect on my own computer which has an installed and working citect on it, and in citect.ini [ctapi] remote is set to 1.
 
I do not use Citect software, so I will not be able to help with the answer, but I will at least help make your post easier to read for others that may be able to help. This is the C# code formatted:
Code:
 public void Init() {
     IntPtr hCTAPI = CTAPI.ctOpen(null, "u1", "1", 2); TestMe(hCTAPI);
 }
       
 [DllImport("ctapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
 public static extern IntPtr ctOpen(string sComputerName, string sUsername, string sPassword, UInt32 nMode);
        
 [DllImport("ctapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
 public static extern bool ctTagRead(IntPtr hCTAPI, [MarshalAs(UnmanagedType.LPStr)] string tag,
                        [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder value, int length);
        
 public static void TestMe(IntPtr ip) { 
     var tag = "tag1";
     var tagPtr = Marshal.StringToHGlobalAnsi(tag);
     System.Text.StringBuilder vsl = new StringBuilder(20);
     bool b = ctTagRead(ip, tag, vsl, 20);
     //, tagValueItemsPtr);
 }
 
Open with CtApi

Hi, did you end up getting it to work?

Try setting the first three arguments to ctOpen to null, since these are not needed when connecting to a Citect instance on the same computer.

The CTAPI function reference also recommends that you set the mode argument to 0.

E.g.
hCTAPI = ctOpen(NULL, NULL, NULL, 0);

Also, I believe that .NET uses Unicode strings. Make sure all strings you pass through CTAPI are ANSI or the tag name will be invalid, causing ctTagRead to always return false.
 
Hi Maximus,
Nope, It did not work for me.
I Injected a DLL into Citect to do my work. But my antivirus is now suspicious of what I do.
Thanks
 

Similar Topics

Hi I'm very new to PLC programming and I'm trying to find out if this library (Tc2_NcFifoAxes) is necessary for our task In our case we need to...
Replies
0
Views
38
I have a project to automate four generator sets. The system will monitor and store the load demand of the factory. Once there's Power outage, the...
Replies
0
Views
79
Adding ethernet equipment to an existing panel that has none. We have some solid ethernet cables coming from other remote cabinets that I plan to...
Replies
3
Views
134
I'm trying to control a device via MODBUS RTU and the ModbusRtuMasterV2_PcCOM in Twincat 3. I've configured a device with the right com port and...
Replies
7
Views
251
Hi, I'm trying to use the IO Device Library (Product Versions) which is configured to work with the 1756-EN4TR & 1756-EN2TR but my system uses...
Replies
0
Views
79
Back
Top Bottom