C#dllimport意外崩溃

C# DllImport crashes unexpectedly

本文关键字:崩溃 意外 C#dllimport      更新时间:2023-10-16

i具有带有C 组件的C#应用程序。我正在与模块之间的DllImport通信。该应用程序工作了很多天,没有问题,并且有时意外崩溃。

[DllImport("recorder", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr start_recording();

是否可以在受保护的容器中运行C 零件,以免整个应用程序崩溃?

事件查看器

应用程序错误

Faulting application name: Recorder.exe, version: 1.0.0.0, time stamp: 0x59eef7cb
Faulting module name: avformat-57.dll, version: 57.71.100.0, time stamp: 0x00000000
Exception code: 0xc0000005
Fault offset: 0x00000000000d7e7f
Faulting process id: 0x186c
Faulting application start time: 0x01d3620b7e41ef96
Faulting application path: C:TestRecorder.exe
Faulting module path: C:Testavformat-57.dll
Report Id: 3fde49fc-d2ed-11e7-bf5c-002590abb7d4

.NET Runtime

Application: Recorder.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: exception code c0000005, exception address 000007FEEA407E7F

通常,在某些情况下,总是会导致一个过程退出,而您与之无关。

虽然您可以通过捕获异常,设置信号处理程序或仅修复错误的错误处理一些错误,但这只会修复特定的情况,不会给您提供一般的解决方案,并且您可能没有控制完全在模块上。

在这种情况下,常见的解决方案是将模块分为不同的代理过程,该过程将与您自己的应用程序进行通信。

在这里您可以找到更多有关Crocecess Communications的信息

我决定使用命名pipes 非常快,我可以在两个方向上进行通信。

C 示例

cout << "Connecting to pipe..." << endl;
// Open the named pipe
// Most of these parameters aren't very relevant for pipes.
HANDLE pipe = CreateFile(
    L"\\.\pipe\MyServerPipe",
    GENERIC_READ, // only need read access
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    //NULL,
    NULL
);
if (pipe == INVALID_HANDLE_VALUE) {
    cout << "Failed to connect to pipe." << endl;
    // look up error code here using GetLastError()
    system("pause");
    return 1;
}
cout << "Reading data from pipe..." << endl;

// The read operation will block until there is data to read
char buffer[128];
DWORD numBytesRead = 0;
BOOL result = ReadFile(
    pipe,
    buffer, // the data from the pipe will be put here
    127 * sizeof(char), // number of bytes allocated
    &numBytesRead, // this will store number of bytes actually read
    NULL // not using overlapped IO
);
if (result) {
    buffer[numBytesRead / sizeof(char)] = ''; // null terminate the string
    cout << "Number of bytes read: " << numBytesRead << endl;
    //wcout << "Message: " << buffer << endl;
    //wstring ws(buffer);
    //string str(ws.begin(), ws.end());
    cout << "Test:" << buffer << endl;
}
else {
    wcout << "Failed to read data from the pipe." << endl;
}
// Close our pipe handle
CloseHandle(pipe);
//wcout << "Done." << endl;
system("pause");
return 0;

C#获取所有可用管道

var listOfPipes = System.IO.Directory.GetFiles(@"\.pipe")

C#example

using (var pipeServer = new NamedPipeServerStream("MyServerPipe", PipeDirection.InOut))
{
    Console.WriteLine("NamedPipeServerStream object created.");
    // Wait for a client to connect
    Console.Write("Waiting for client connection...");
    pipeServer.WaitForConnection();
    Console.WriteLine("Client connected.");
    try
    {
        using (var bw = new BinaryWriter(pipeServer))
        {
            var data = Encoding.ASCII.GetBytes("SendInformation data1 data2 data3");
            //var data = Encoding.ASCII.GetBytes("Startrn");
            bw.Write(data);
        }
    }
    // Catch the IOException that is raised if the pipe is broken
    // or disconnected.
    catch (IOException e)
    {
        Console.WriteLine("ERROR: {0}", e.Message);
    }
}