C#
Code:
MacroTempKey MTP = new MacroTempKey();
string[] Line = new string[3];
object[] TempKey = new object[5];
private void button2_Click(object sender, EventArgs e)
{
label1.Focus();
if (null != Line)
{
for(int i = 0; i < 3; ++i)
{
if (Line[i] != null && Line[i] != String.Empty)
{
Pipes.SendString(Line[i]);
}
}
}
if (null != TempKey)
{
for (int i = 0; i < 5; ++i)
{
if (TempKey[i] != null)
{
switch(i)
{
case 0:
MTP.TempKey0 = TempKey[i];
break;
case 1:
MTP.TempKey1 = TempKey[i];
break;
case 2:
MTP.TempKey2 = TempKey[i];
break;
case 3:
MTP.TempKey3 = TempKey[i];
break;
case 4:
MTP.TempKey4 = TempKey[i];
break;
case 5:
MTP.TempKey5 = TempKey[i];
break;
}
}
}
Pipes.SendObject(MTP);
}
}
------------------------------------------------------------
Code:
[Serializable]
class MacroTempKey
{
public object TempKey0
{
get;
set;
}
public object TempKey1
{
get;
set;
}
public object TempKey2
{
get;
set;
}
public object TempKey3
{
get;
set;
}
public object TempKey4
{
get;
set;
}
public object TempKey5
{
get;
set;
}
}
------------------------------------------------------------
Code:
static NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "EXAMPLE", PipeDirection.InOut);
public static void SendBytes(byte[] data, int bytes) // This works , bottom 2 functions are not tested/a work in progress related to question.
{
try
{
pipeClient.Write(data, 0, bytes);
pipeClient.Flush();
}
catch
{
}
}
public static void SendString(string s)
{
try
{
using (var binWriter = new BinaryWriter(pipeClient))
{
binWriter.Write(s);
binWriter.Flush();
}
}
catch
{
}
}
public static void SendObject(MacroTempKey message)
{
try
{
IFormatter formatWriter = new BinaryFormatter();
formatWriter.Serialize(pipeClient, message);
}
catch
{
}
}
------------------------------------------------------------
C++
Code:
HANDLE hPipe;
LPCTSTR lpszPipename = TEXT("\\\\.\\pipe\\EXAMPLE");
hPipe = CreateNamedPipe(lpszPipename,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_NOWAIT,
PIPE_UNLIMITED_INSTANCES,
BUFSIZE ,
BUFSIZE ,
0,
NULL);
[Serializable]
ref class MacroTempKey
{
public:
property Object ^TempKey0;
property Object ^TempKey1;
property Object ^TempKey2;
property Object ^TempKey3;
property Object ^TempKey4;
property Object ^TempKey5;
};
DWORD ReadClient()
{
while(!loopstop)
{
if(startread)
{
while(ReadFile(hPipe, chRequest, BUFSIZE, &cbBytesRead, NULL) >0)
{
GetAnswerToRequest(chRequest);
Sleep(20);
}
GetAnswerToOtherRequest();
Sleep(100);
SendToPipe(0, 0);
}
else
Sleep(500);
}
return 0;
}
void __stdcall GetAnswerToRequest(__in char* szRequest)
{
switch (szRequest[0])
{
case TEST:
if (szRequest[1] == 1)
{
}
else
{
}
break;
}
}
void GetAnswerToOtherRequest()
{
IFormatter^ f = gcnew BinaryFormatter();
MacroTempKey ^messageReceived = safe_cast<MacroTempKey^>(f->Deserialize(hPipe));
}
------------------------------------------------------------
As you can see, I am attempting to send string[] Line and object[] TempKey from c# to c++ through a namedPipe.
I am positive I did this correctly in c#, however the same cannot be said for the c++ part.
Can anyone help me edit the code above to properly receive the string[] Line and put it into its respective c++ string[] Line & to also receive the object[] TempKey and put it into its respective c++ class.
Here is the following error I get at build.
I need help as I am confused.
Error1 error C2664: System::Runtime::Serialization::IFormatter:
eserialize': cannot convert parameter 1 from 'HANDLE' to 'System::IO::Stream ^'
The error above is for the HANDLE hPipe in the deserialize function.