New to C++ , Converting C#.NET to C++ (Unmanaged / Managed ???)

I'm New to C++, normally did most of my Programming in either C# or VB.NET

I've written a DLL in C# that is used to Communicate to our Software via a BiDriectional Named Pipe, the "Original" plan was to then allow our client to use the DLL in their application so they can run their application on the same box as our app (It just handles communication to different test equipment & controls other Hardware for testing products)

Now The DLL works fine in C# & VB.NET & C++ (Visual Studio 2005,2008,2010)
The Problem is They are coding in VC++ 6.0 (and they're not going to upgrade)

So I've got to apparently re-write my DLL in C++ so they can use it.

This is a simplified version of my DLL -> http://www.pgp-protector.com/help/MyDLL.zip
(Code in post to follow)

So a few questions.
1) How do I use Named Pipes in VC++ 6.0 ?
2) How do I use Multi Definitional Arrays (Both Strings & Doubles) that are of an Undefined Length, both for Function Returns & in the Command Parameters ?
Last edited on
Code for above DLL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.IO;
using System.IO.Pipes;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace PGPDLL
{
    public class MyDLL
    {
        private static bool ValidComPort = false;
        private static bool ValidConfig = false;
        private static StreamWriter MyWriter;
        private static StreamReader MyReader;
        private static NamedPipeClientStream MyIPCPipe;
        /// <summary>
        /// Returns Last Recorded Error Encountered by the DLL.
        /// </summary>
        public static string InternalErrorReason = "No Error Default";
        private static string[] TalkPipeOut()
        {
            try
            {
                if (MyIPCPipe.NumberOfServerInstances > 0)
                {
                    string temp;
                    temp = MyReader.ReadLine();
                    return temp.Split(':');
                }
                else
                {
                    return null;
                }
            }
            catch
            {
                return null;
            }

        }
        private static bool TalkPipeIn(int ComType, string ComData)
        {
            bool ValidPipeIn = true;

            try
            {
                MyWriter.Write(ComType);
                MyWriter.Write(ComData);
            }
            catch
            {
                ValidPipeIn = false;
            }
            return ValidPipeIn;
        }
        private static void CheckServerOpen(int ServerProcessID)
        {
            bool ActiveServer = true;
            try { ActiveServer = !Process.GetProcessById(ServerProcessID).HasExited; }
            catch { ActiveServer = false; }
            // If ActiveServer = False, 
            if (ActiveServer == false)
            {
                ValidComPort = false;
                System.Windows.Forms.MessageBox.Show("Lost Connection To Tester!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MyIPCPipe.Close();
            }
        }
        static void CheckTimer(object ProcessWatchID)
        {
            int CheckID = (int)ProcessWatchID;
            while (ValidComPort)
            {
                Thread.Sleep(200);
                CheckServerOpen(CheckID);
            }
        }
        private static bool MyWrite(string QstDataOut)
        {
            bool ValidPipeOut = false;
            if (ValidComPort)
                try
                {
                    // Send Data
                    {
                        MyWriter.AutoFlush = true;
                        MyWriter.WriteLine(QstDataOut);
                    }
                    ValidPipeOut = true;
                }
                catch
                {
                    ValidPipeOut = false;
                }
            return ValidPipeOut;
        }
        private static string MyRead()
        {
            // Try to open connection with QST Client Pipe Out
            // QstRead is used for Getting Data From the Client
            string QstReadData = "";
            if (ValidComPort)
                try
                {
                    QstReadData = MyReader.ReadLine();
                }
                catch
                {
                    QstReadData = "";
                }
            return QstReadData;
        }
        /// <summary>
        /// Call to Initilize Comunication with the Server Application
        /// </summary>
        /// <returns>True if Connected, False if Unable to Connect</returns>
        public static bool ServerOpen()
        {
            if (ValidComPort == false)
            {
                MyIPCPipe = new NamedPipeClientStream(".", "MySystemsPipe", PipeDirection.InOut, PipeOptions.Asynchronous);
                ValidComPort = true;
                try { MyIPCPipe.Connect(1000); }
                catch (Exception ex)
                {
                    string Erroris;
                    Erroris = ex.Message;
                    if (Erroris == "Already in a connected state.")
                    {
                        // We're Already Connected, Ignore this error.
                        ValidComPort = true;
                    }
                    else
                    {
                        ValidComPort = false;
                        MessageBox.Show(Erroris);
                    }
                }
            }

            //QstIPCPipe.Close();

            if (ValidComPort)
            {
                int ServerProcessID = 0;
                Thread WatchdogTimer = new Thread(CheckTimer);
                string ClientProcessID = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
                MyReader = new StreamReader(MyIPCPipe);
                MyWriter = new StreamWriter(MyIPCPipe);
                ValidComPort = MyWrite(ClientProcessID);
                ServerProcessID = Convert.ToInt16(MyRead());
                WatchdogTimer.IsBackground = true;
                WatchdogTimer.Start(ServerProcessID);
            }
            return ValidComPort;
        }
        /// <summary>
        /// Used to Send String array to Server for configurations
        /// </summary>
        /// <param name="QstTestConfiguration">String Array of Configuration</param>
        /// <returns>-1 if Error, Or Total Count of Processed Lines</returns>
        public static int QstSendConfig(string[] TestConfigurationReceived)
        {
            string[] QstTestConfiguration = TestConfigurationReceived;
            int FinalValidConfigLines = QstTestConfiguration.Length;
            if (ValidComPort && QstTestConfiguration != null)
            {
                ValidConfig = true;
            }
            else
            {
                InternalErrorReason = "Comunication with Server Not Open";
            }
            return FinalValidConfigLines;
        }
        /// <summary>
        /// Runs a QST Test (Fixed I or Psudo V)
        /// Returns a Double Array for Test Results in the Format of double[Sample#,(0=Magnet,1=Test1,2=Test2,3=Test3,4=Test4)]
        /// </summary>
        /// <param name="TestID1">Calls TestID1 in Configuraton</param>
        /// <param name="TestID2">Calls TestID1 in Configuraton</param>
        /// <returns>double[TestCount,TestID]</returns>
        public static double[,] RunTest(int TestID1, int TestID2)
        {
            string QstTestSequence = "DummyData";
            if (ValidConfig & ValidComPort)
            {
                if (MyWrite("RUNQST") && MyWrite(QstTestSequence))
                {
                    if (MyRead() == "OK")
                        return null;
                    else
                        return null;
                }
            }
            if (!ValidConfig) InternalErrorReason = "No Valid Configuration Test Not Run";
            if (!ValidComPort) InternalErrorReason = "Server Not Found, Test Not Run";
            return null;
        }
        /// <summary>
        /// Request And Returns Server Status or what the Last Recorded Error was
        /// </summary>
        public static string MySystemStatus()
        {
            string TempStatus = "";
            if (!ValidConfig) TempStatus = "No Valid Config ";
            if (ValidComPort)
            {
                if (MyWrite("GETSTATUS"))
                {
                    return TempStatus + MyRead();
                }
            }
            return "Unable to Comunicate";
        }
    }
}
Topic archived. No new replies allowed.