Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. char msg1[]="Message1";
  5. char msg2[]="Message2";
  6.  
  7. int main(){
  8. std::string c;
  9. SECURITY_ATTRIBUTES sa={0};
  10. SECURITY_DESCRIPTOR sd={0};
  11.  
  12. InitializeSecurityDescriptor(
  13. &sd,
  14. SECURITY_DESCRIPTOR_REVISION);
  15.  
  16. SetSecurityDescriptorDacl(
  17. &sd,
  18. TRUE,
  19. NULL,
  20. FALSE);
  21. sa.bInheritHandle=false;
  22. sa.lpSecurityDescriptor=&sd;
  23. sa.nLength=sizeof(sa);
  24. HANDLE ch1=CreateNamedPipe (
  25. "\\.\pipe\testpipe",
  26. PIPE_ACCESS_DUPLEX,
  27. PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE| PIPE_WAIT,
  28. PIPE_UNLIMITED_INSTANCES,
  29. sizeof msg1, 4, 0, &sa
  30. );
  31. if(ch1==INVALID_HANDLE_VALUE){
  32. std::cout<<"ch1 "<<GetLastError()<<"n";
  33. std::getline(std::cin, c);
  34. return 1;
  35. }
  36. HANDLE ch2=CreateNamedPipe(
  37. "\\.\pipe\testpipe",
  38. PIPE_ACCESS_DUPLEX,
  39. PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE| PIPE_WAIT,
  40. PIPE_UNLIMITED_INSTANCES,
  41. sizeof msg1, 4, 0, &sa
  42. );
  43. if(ch2==INVALID_HANDLE_VALUE){
  44. std::cout<<"ch2 "<<GetLastError()<<"n";
  45. std::getline(std::cin, c);
  46. return 1;
  47. }
  48. ConnectNamedPipe(ch1, 0);
  49. ConnectNamedPipe(ch2, 0);
  50.  
  51. unsigned long foo;
  52. if(TransactNamedPipe(ch1, msg1, sizeof msg1, &foo, sizeof foo, &foo, 0)==0){
  53. std::cout<<"transact1 "<<GetLastError()<<"n";
  54. std::getline(std::cin, c);
  55. return 1;
  56. }
  57. if(TransactNamedPipe(ch2, msg2, sizeof msg2, &foo, sizeof foo, &foo, 0)==0){
  58. std::cout<<"transact2 "<<GetLastError()<<"n";
  59. std::getline(std::cin, c);
  60. return 1;
  61. }
  62. std::cout<<"Finishedn";
  63. std::getline(std::cin, c);
  64. return 0;
  65. }
  66.  
  67. #include <windows.h>
  68. #include <iostream>
  69.  
  70. char msg1[]="Message1n";
  71. char msg2[]="Message2n";
  72.  
  73. int main(){
  74. std::string c;
  75. std::cout<<"Server name:n";
  76. std::string sname;
  77. std::cin>>sname;
  78. std::cin.ignore();
  79. NETRESOURCE nr={0};
  80. if(sname!="."){
  81. std::string sname2=std::string("\\")+sname;
  82. nr.dwType = RESOURCETYPE_ANY;
  83. nr.lpRemoteName = &sname2[0];
  84. DWORD ret=WNetAddConnection2( &nr, "", "", 0);
  85. if(ret!=NO_ERROR){
  86. std::cout<<"WNetAddConnection2 "<<GetLastError()<<"n";
  87. std::getline(std::cin, c);
  88. return 1;
  89. }
  90. }
  91. std::string pname=std::string("\\")+sname+std::string("\pipe\testpipe");
  92. HANDLE phandle1=CreateFile (
  93. pname.c_str(),
  94. GENERIC_READ|GENERIC_WRITE,
  95. 0, 0, OPEN_EXISTING, 0,0
  96. );
  97. if(phandle1==INVALID_HANDLE_VALUE){
  98. std::cout<<"CreateFile1 "<<GetLastError()<<"n";
  99. std::getline(std::cin, c);
  100. return 1;
  101. }
  102. HANDLE phandle2=CreateFile (
  103. pname.c_str(),
  104. GENERIC_READ|GENERIC_WRITE,
  105. 0, 0, OPEN_EXISTING, 0,0
  106. );
  107. if(phandle2==INVALID_HANDLE_VALUE){
  108. std::cout<<"CreateFile2 "<<GetLastError()<<"n";
  109. std::getline(std::cin, c);
  110. return 1;
  111. }
  112. char msg[sizeof(msg1)];
  113. unsigned long s;
  114. unsigned long foo=0;
  115. if(ReadFile(phandle1, &msg, sizeof msg1, &s, 0)==0){
  116. std::cout<<"ReadFile1 "<<GetLastError()<<"n";
  117. std::getline(std::cin, c);
  118. return 1;
  119. }
  120. std::cout<<msg<<"n";
  121. if(WriteFile (phandle1, &foo, sizeof foo, &foo, 0)==0){
  122. std::cout<<"WriteFile1 "<<GetLastError()<<"n";
  123. std::getline(std::cin, c);
  124. return 1;
  125. }
  126. if(ReadFile(phandle2, &msg, sizeof msg1, &s, 0)==0){
  127. std::cout<<"ReadFile2 "<<GetLastError()<<"n";
  128. std::getline(std::cin, c);
  129. return 1;
  130. }
  131. std::cout<<msg<<"n";
  132. if(WriteFile (phandle2, &foo, sizeof foo, &foo, 0)==0){
  133. std::cout<<"WriteFile2 "<<GetLastError()<<"n";
  134. std::getline(std::cin, c);
  135. return 1;
  136. }
  137. std::cout<<"Finishedn";
  138. std::getline(std::cin, c);
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement