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

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
6
Views
130
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
48
Hello, As part of our project, we are using an M241 controller. This controller interfaces with an industrial computer and a router via a switch...
Replies
2
Views
89
I'm trying to write a data in Arduino using MODWR function block .I used the code I got from online for both PLC and Arduino. I made the wiring...
Replies
4
Views
90
Hey all, i have a panelview screen (image attached), with 4 items on it. Program 1, Program 2, ...3, ...4. The PLC i am using is a compactlogix...
Replies
5
Views
166
Back
Top Bottom