2017-12-19 12 views
-1

こんにちは私はWindows環境でc3とcppでサンプルアプリケーションを開発しました。このアプリケーションは、これらのプログラム間で名前付きパイプを使用して通信します。私は[DllImport("kernel32.dll", SetLastError = true)]コマンドを私のcppプログラムに持っています。私は私のC#のprogaramをコンパイルするとき、私は以下のエラーを取得します。ubuntuのkernel32.dllに相当するものは何ですか?

Enter the message 
Unhandled Exception: Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'kernel32.dll': The specified module or one of its dependencies could not be found. 
(Exception from HRESULT: 0x8007007E) 
    at consoleapp.NamedPipeServer.CreateNamedPipe(String pipeName, UInt32 dwOpenMode, UInt32 dwPipeMode, UInt32 nMaxInstances, UInt32 nOutBufferSize, UInt32 nInBufferSize, UInt32 nDefaultTimeOut, IntPtr lpSecurityAttributes) 
    at consoleapp.NamedPipeServer.ListenForClients() in /home/niranjan/consoleapp/NamedPipeServer.cs:line 66 
    at System.Threading.Thread.ThreadMain_ThreadStart() 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)System.DllNotFoundException: Unable to load DLL 'kernel32.dll': The specified module or one of its dependencies could not be found. 
(Exception from HRESULT: 0x8007007E) 
    at consoleapp.NamedPipeServer.CreateNamedPipe(String pipeName, UInt32 dwOpenMode, UInt32 dwPipeMode, UInt32 nMaxInstances, UInt32 nOutBufferSize, UInt32 nInBufferSize, UInt32 nDefaultTimeOut, IntPtr lpSecurityAttributes) 
    at consoleapp.NamedPipeServer.ListenForClients() in /home/niranjan/consoleapp/NamedPipeServer.cs:line 66 
    at System.Threading.Thread.ThreadMain_ThreadStart() 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 

以下は私のC#ファイルです。私はubuntuで.Net core(2.0)コンソールアプリケーションを作成しました。 のProgram.cs以下

using System; 

namespace consoleapp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Hello World!"); 
      NamedPipeServer PServer1 = new NamedPipeServer(@"\\.\pipe\myNamedPipe1",0); 
      NamedPipeServer PServer2 = new NamedPipeServer(@"\\.\pipe\myNamedPipe2",1); 

      PServer1.Start(); 
      PServer2.Start(); 

      string Ms="Start"; 
      do 
      { 
       Console.WriteLine("Enter the message"); 
       Ms = Console.ReadLine(); 
       PServer2.SendMessage(Ms, PServer2.clientse); 
      } while (Ms != "quit"); 

      PServer1.StopServer(); 
      PServer2.StopServer(); 
     } 
    } 
} 

は私NamedPipeServer.cs以下

using System; 
using Microsoft.Win32.SafeHandles; 
using System.Text; 
using System.Runtime.InteropServices; 
using System.Threading; 
using System.IO; 

namespace consoleapp 
{ 
    public class NamedPipeServer 
    { 
     [DllImport("kernel32.dll", SetLastError = true)] 
     public static extern SafeFileHandle CreateNamedPipe(
      String pipeName, 
      uint dwOpenMode, 
      uint dwPipeMode, 
      uint nMaxInstances, 
      uint nOutBufferSize, 
      uint nInBufferSize, 
      uint nDefaultTimeOut, 
      IntPtr lpSecurityAttributes); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     public static extern int ConnectNamedPipe(
      SafeFileHandle hNamedPipe, 
      IntPtr lpOverlapped); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     public static extern int DisconnectNamedPipe(
      SafeFileHandle hNamedPipe); 

     public const uint DUPLEX = (0x00000003); 
     public const uint FILE_FLAG_OVERLAPPED = (0x40000000); 

     public class Client 
     { 
      public SafeFileHandle handle; 
      public FileStream stream; 
     } 

     public const int BUFFER_SIZE = 100; 
     public Client clientse =null; 

     public string pipeName; 
     Thread listenThread; 
     SafeFileHandle clientHandle; 
     public int ClientType; 

     public NamedPipeServer(string PName,int Mode) 
     { 
      pipeName = PName; 
      ClientType = Mode;//0 Reading Pipe, 1 Writing Pipe 

     } 

     public void Start() 
     { 
      this.listenThread = new Thread(new ThreadStart(ListenForClients)); 
      this.listenThread.Start(); 
     } 
     private void ListenForClients() 
     { 
      while (true) 
      { 

       clientHandle =CreateNamedPipe(this.pipeName,DUPLEX | FILE_FLAG_OVERLAPPED,0,255,BUFFER_SIZE,BUFFER_SIZE,0,IntPtr.Zero); 

       //could not create named pipe 
       if (clientHandle.IsInvalid) 
        return; 

       int success = ConnectNamedPipe(clientHandle, IntPtr.Zero); 

       //could not connect client 
       if (success == 0) 
        return; 

       clientse = new Client(); 
       clientse.handle = clientHandle; 
       clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true); 

       if (ClientType == 0) 
       { 
        Thread readThread = new Thread(new ThreadStart(Read)); 
        readThread.Start(); 
       } 
      } 
     } 
     private void Read() 
     { 
      //Client client = (Client)clientObj; 
      //clientse.stream = new FileStream(clientse.handle, FileAccess.ReadWrite, BUFFER_SIZE, true); 
      byte[] buffer = null; 
      ASCIIEncoding encoder = new ASCIIEncoding(); 

      while (true) 
      { 

       int bytesRead = 0; 

       try 
       { 
        buffer = new byte[BUFFER_SIZE]; 
        bytesRead = clientse.stream.Read(buffer, 0, BUFFER_SIZE); 
       } 
       catch 
       { 
        //read error has occurred 
        break; 
       } 

       //client has disconnected 
       if (bytesRead == 0) 
        break; 

       //fire message received event 
       //if (this.MessageReceived != null) 
       // this.MessageReceived(clientse, encoder.GetString(buffer, 0, bytesRead)); 

       int ReadLength = 0; 
       for (int i = 0; i < BUFFER_SIZE; i++) 
       { 
        if (buffer[i].ToString("x2") != "cc") 
        { 
         ReadLength++; 
        } 
        else 
         break; 
       } 
       if (ReadLength > 0) 
       { 
        byte[] Rc = new byte[ReadLength]; 
        Buffer.BlockCopy(buffer, 0, Rc, 0, ReadLength); 

        Console.WriteLine("C# App: Received " + ReadLength +" Bytes: "+ encoder.GetString(Rc, 0, ReadLength)); 
        buffer.Initialize(); 
       } 

      } 

      //clean up resources 
      clientse.stream.Close(); 
      clientse.handle.Close(); 

     } 
     public void SendMessage(string message, Client client) 
     { 

       ASCIIEncoding encoder = new ASCIIEncoding(); 
       byte[] messageBuffer = encoder.GetBytes(message); 

       if (client.stream.CanWrite) 
       { 
        client.stream.Write(messageBuffer, 0, messageBuffer.Length); 
        client.stream.Flush(); 
       } 


     } 
     public void StopServer() 
     { 
      //clean up resources 

      DisconnectNamedPipe(this.clientHandle); 


      this.listenThread.Abort(); 
     } 

    } 
} 

である私のcpp programeです。

#include <stdio.h> 
#include <windows.h> 

unsigned long __stdcall NET_RvThr(void * pParam) ; 
DWORD WINAPI ThreadProc() ; 
HANDLE hPipe1,hPipe2; 
BOOL Finished; 

int main(int argc, char *argv[]) 
{ 
    //Pipe Init Data 
    char buf[100]; 

    LPTSTR lpszPipename1 = TEXT("\\\\.\\pipe\\myNamedPipe1"); 
    LPTSTR lpszPipename2 = TEXT("\\\\.\\pipe\\myNamedPipe2"); 

    DWORD cbWritten; 
    DWORD dwBytesToWrite = (DWORD)strlen(buf); 

    //Thread Init Data 
    DWORD threadId; 
    HANDLE hThread = NULL; 

    BOOL Write_St=TRUE; 

    Finished=FALSE; 




    hPipe1=CreateFile(lpszPipename1, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); 
    hPipe2=CreateFile(lpszPipename2, GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); 


    if ((hPipe1 == NULL || hPipe1 == INVALID_HANDLE_VALUE)||(hPipe2 == NULL || hPipe2 == INVALID_HANDLE_VALUE)) 
    { 
     printf("Could not open the pipe - (error %d)\n",GetLastError()); 

    } 
    else 
    { 

     hThread = CreateThread(NULL, 0, &NET_RvThr, NULL, 0, NULL); 
     do 
     { 
      printf ("Enter your message: "); 
      scanf ("%s",buf); 
      if (strcmp (buf,"quit") == 0) 
       Write_St=FALSE; 
      else 
      { 
       WriteFile(hPipe1, buf, dwBytesToWrite, &cbWritten, NULL); 
       memset(buf,0xCC,100); 

      } 

     }while(Write_St); 

     CloseHandle(hPipe1); 
     CloseHandle(hPipe2); 
     Finished=TRUE; 
    } 

    getchar(); 


} 
unsigned long __stdcall NET_RvThr(void * pParam) { 
    BOOL fSuccess; 
    char chBuf[100]; 
    DWORD dwBytesToWrite = (DWORD)strlen(chBuf); 
    DWORD cbRead; 
    int i; 

    while(1) 
    { 
     fSuccess =ReadFile(hPipe2,chBuf,dwBytesToWrite,&cbRead, NULL); 
     if(fSuccess) 
     { 
      printf("C++ App: Received %d Bytes : ",cbRead); 
      for(i=0;i<cbRead;i++) 
       printf("%c",chBuf[i]); 
      printf("\n"); 
     } 
     if (! fSuccess && GetLastError() != ERROR_MORE_DATA) 
     { 
      printf("Can't Read\n"); 
      if(Finished) 
       break; 
     } 
    } 
} 

私はkernal32.dllに相当するubuntuを理解しようとしています。誰かが私にこの問題を理解させるのを助けることができますか?どんな助けでも大歓迎です。ありがとうございました。

+1

linuxに相当するものはありません。 AFAIK – Fildor

+0

"私のcppプログラムのコマンド"これはどちらですか? CppまたはC#?なぜSystem.IO.Pipesを使用しないのですか? http://voices.canonical.com/tag/named%20pipes/ – Fildor

+0

ありがとうございます。それはcppプログラムです。私はcpp programehereを投稿します。 –

答えて

1

Ubuntuのkernel32.dllに相当するものは何ですか?

Kernel32.dllは、Windows固有のライブラリです。あなたはUbuntuのような他のオペレーティングシステムでは見つけられません(Wineのようなエミュレーションレイヤーを使用しない限り)。したがって、プラットフォーム固有のコードをUbuntuで使用することはできません。

したがって、プラットフォームに依存しない方法で名前付きパイプを使用するにはどうすればよいですか。

幸いにも、.NET Frameworkの設計者は、この問題を解決しました。.NET Core 2.0には、System.IO.Pipes Namespaceが含まれています。名前付きパイプによるプロセス間通信の管理ラッパーが含まれています。

+0

ありがとう、あなたは私の日を救った。だから[DllImport( "kernel32.dll"、SetLastError = true)と書くのではなく、私は何を書くべきですか? –

+0

@NiranjanGodbole: '[DllImport(" kernel32.dll "、SetLastError = true)]の代わりにpublic static extern SafeFileHandle CreateNamedPipe(...など)を参照すると、System.IOのドキュメント.Pipes名前空間とそこに(名前付きパイプを作成する)置換メソッドを見つける。 'DllImport'を含む他のすべてのメソッドについても同じことを繰り返します。直接の置換がないかもしれないことに注意してください(.NETメソッドはおそらくSafeFileHandleを返しますが、他のいくつかのマネージクラスを返します)ので、残りのコードも若干書き換える必要があります。 – Heinzi

+0

したがって、たとえばNamedPipeServerStream pipe = new NamedPipeServerStream( "testpipe")は、静的な外部static SafeFileHandle CreateNamedPipeの置き換えになりますか? –

関連する問題