C#
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
|
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);
}
}
|
------------------------------------------------------------
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
|
[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;
}
}
|
------------------------------------------------------------
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
|
static NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "EXAMPLE", PipeDirection.InOut);
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++
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
|
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::Deserialize': cannot convert parameter 1 from 'HANDLE' to 'System::IO::Stream ^' |
The error above is for the HANDLE hPipe in the deserialize function.