C# 如果我通过CreateProcess()启动.net进程,则无法加载文件或程序集“protobuf net”错误 我在C中创建了.NET应用程序,当从控制台CMD窗口运行它时,它执行的很好,但是如果我试图通过C++调用我自己的应用程序来启动应用程序,我就有如下例外: Unhandled Exception: System.TypeInitializationException: The type initializer for 'AppLog_NS.LogReader' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'protobuf-net, Version=1.0.0.280, Culture=neutral, ublicKeyToken=257b51d87d2e4d67' or one of its dependencies. The system cannot find the file specified.File name: 'protobuf-net, Version=1.0.0.280, Culture=neutral, PublicKeyToken=257b51d87d2e4d67' at AppLog_NS.LogReader..cctor()

C# 如果我通过CreateProcess()启动.net进程,则无法加载文件或程序集“protobuf net”错误 我在C中创建了.NET应用程序,当从控制台CMD窗口运行它时,它执行的很好,但是如果我试图通过C++调用我自己的应用程序来启动应用程序,我就有如下例外: Unhandled Exception: System.TypeInitializationException: The type initializer for 'AppLog_NS.LogReader' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'protobuf-net, Version=1.0.0.280, Culture=neutral, ublicKeyToken=257b51d87d2e4d67' or one of its dependencies. The system cannot find the file specified.File name: 'protobuf-net, Version=1.0.0.280, Culture=neutral, PublicKeyToken=257b51d87d2e4d67' at AppLog_NS.LogReader..cctor(),c#,.net,windows,runtime-error,C#,.net,Windows,Runtime Error,下面是我如何调用CreateProcess的 GetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT, 0, 0, 0, 0, 0, &sd); saAttr.nLength = sizeof(saAttr); saAttr.bInheritHandle = FALSE; saAttr.lpSecurityDescriptor = sd; memset(&si, 0, sizeof(STARTUPINFO)); si.cb =

下面是我如何调用CreateProcess的

GetSecurityInfo(GetCurrentProcess(), SE_KERNEL_OBJECT, 0, 0, 0, 0, 0, &sd);
saAttr.nLength = sizeof(saAttr);
saAttr.bInheritHandle = FALSE;
saAttr.lpSecurityDescriptor = sd;

memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
memset(&pi, 0, sizeof(PROCESS_INFORMATION));

// Launch bootstrapper
CreateProcess(NULL, cmd_path,  &saAttr, NULL, FALSE, 0, NULL, NULL, &si, &pi);
仅供参考,我创建的父进程和.NET应用程序都位于同一目录中,并且我在该目录中也有protobuf-NET.dll。否则,它应该无法从cmd窗口正确启动

protobuf-net.dll不在GAC中,我假设只要它在同一个目录中,它就应该能够找到dll并正确启动.net应用程序。在这种情况下,是否需要配置或执行某些操作

另一件需要注意的事情是FusionLog显示PrivatePath设置为NULL,并显示“未找到任何应用程序配置文件”。这对我来说非常奇怪,因为我还将配置文件添加到同一目录中以指定PrivatePath,但这似乎也没有任何作用

因此,我假设在创建.NET进程时需要传递某些env变量或其他内容

但我真的想不出我需要使用的任何选项。 谁能给我一些建议吗


提前感谢。

我知道您正在尝试从C程序启动.net应用程序。一个好的地方应该是.net类进程的源代码。它调用名为StartWithCreateProcessstartInfo的私有方法,该方法是:

这将为您提供有关如何设置CreateProcess调用的详细信息。他们正在进行大量的设置,然后调用CreateProcess,如下所示:

                   retVal = NativeMethods.CreateProcess (
                           null,               // we don't need this since all the info is in commandLine
                           commandLine,        // pointer to the command line string
                           null,               // pointer to process security attributes, we don't need to inheriat the handle
                           null,               // pointer to thread security attributes
                           true,               // handle inheritance flag
                           creationFlags,      // creation flags
                           environmentPtr,     // pointer to new environment block
                           workingDirectory,   // pointer to current directory name
                           startupInfo,        // pointer to STARTUPINFO
                           processInfo         // pointer to PROCESS_INFORMATION
                       );
与您的调用相比,我看到了一些差异:

CreateProcess(NULL, cmd_path,  &saAttr, NULL, FALSE, 0, NULL, NULL, &si, &pi);

您可以尝试与Process相同类型的参数。启动对CreateProcess的调用,然后从那里开始。

您是否尝试将应用程序所在的目录传递给CreateProcess方法CurrentDirectory/WorkingDirectory?由于缺少任何代码,请改为使用ShellExecute。这更容易正确。谢谢你的建议。我试图设置工作目录,但没有任何效果。我找到了解决问题的方法,但如果我能对此有所了解,我将不胜感激。基本上我改变了调用CreateProcess的方式。我没有为命令行参数提供完整路径,而是为ApplicationName提供了可执行文件名。所以我调用如下:CreateProcessAppLog.exe,NULL,&saAttr,NULL,FALSE,0,NULL,NULL,&si,π这是可行的,但不确定为什么这是可行的,但另一个不行。此修复程序在vba中即使使用WorkingDirectory也不起作用
CreateProcess(NULL, cmd_path,  &saAttr, NULL, FALSE, 0, NULL, NULL, &si, &pi);