简体   繁体   English

VS2012 link.exe是从命令行运行的,而不是从C#的System.Diagnostics.Process运行的?

[英]VS2012 link.exe runs from command line, but not from C#'s System.Diagnostics.Process?

Im trying to run the linker that comes with VS2012 from a C# program but i get an error saying mspdb110.dll is missing when the linker runs. 我试图从C#程序运行VS2012附带的链接器,但是我收到一条错误消息,说链接器运行时缺少mspdb110.dll。 Why is running the exe from a C# process causing this problem? 为什么从C#进程运行exe会导致此问题? It works fine from the command line (everywhere else to!). 从命令行(到其他地方!)都可以正常工作。 Heres a chunk of code, it works exactly as expected, but link.exe complains. 这是一段代码,它完全按预期工作,但是link.exe抱怨。 Im not trying to do any fancy process manipulations, i just want to start it with an argument. 我没有尝试做任何花哨的过程操作,我只想从一个参数开始。 So the question is what is being done from C# to prevent link.exe from finding a dll. 因此,问题在于如何通过C#来防止link.exe查找dll。 secondly, why does link.exe need this debug related dll to run? 其次,为什么link.exe需要此调试相关的dll才能运行? And lastly, how should i go about fixing this? 最后,我该如何解决这个问题? Oh, just to make things clear it is link.exe that is giving the errors, nothing else. 哦,只是为了使事情更清楚一点,是link.exe给出了错误,仅此而已。

    private void buildButton_Click(object sender, EventArgs e)
    {
        string linkerPath = null;

        if (File.Exists((linkerPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) +
            "\\Microsoft Visual Studio 11.0\\VC\\bin\\link.exe")))
        {
            ProcessStartInfo linkInfo = new ProcessStartInfo(linkerPath);
            linkInfo.Arguments = "/subsystem:" + subsystemComboBox.Text + " ";

            //node.Text is the full file path
            foreach (TreeNode node in treeNodes.CSArray)
            {
                linkInfo.Arguments += "\"" + node.Text + "\" ";
            }

            linkInfo.Arguments += librariesRichTextBox.Text;

            //complete example arg string: /subsystem:console "C:\testdir\test.obj" msvcrt.lib

            try
            {
                Process linkProcess = Process.Start(linkInfo);

                linkProcess.WaitForExit();

                linkProcess.Close();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show("the VS linker wasnt found!");
        }
    }

I'd imagine this is down to the working directory, when you call the process from your applicaton, the working directory used is derived from wherever the application was started from, ie bin/debug . 我想这是工作目录,当您从applicaton调用进程时,所使用的工作目录是从应用程序启动的任何地方派生的,即bin/debug

The commandline will most likely be opening it from its native location where the dll also lives. 命令行最有可能从dll所在的本机位置打开它。

Therefore your application will be looking in bin/debug for the dll instead of the correct directory 因此,您的应用程序将在bin/debug寻找dll而不是正确的目录

You can set the working directory to the correct directory through ProcessStartInfo ... 您可以通过ProcessStartInfo将工作目录设置为正确的目录...

var linkerPath = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
      "\\Microsoft Visual Studio 11.0\\VC\\bin\\link.exe");

ProcessStartInfo startInfo = new ProcessStartInfo {
    FileName = linkerPath,
    WorkingDirectory = Path.GetDirectoryName(linkerPath)};

Process p = new Process{
    StartInfo = startInfo};

A quick experiment seems to indicate that the answer will be found in your environment variables. 快速实验似乎表明答案将在您的环境变量中找到。

From within, Visual Studio, select Tools >> Visual Studio 20xx Command Prompt . 从内部的Visual Studio中,选择“ 工具” >>“ Visual Studio 20xx命令提示符”

Type the LINK command from within that command window. 在该命令窗口中键入LINK命令。 The linker will display its usual "list of things you can tell me to do." 链接器将显示其通常的“您可以告诉我要做的事情列表”。

Type the SET command and examine the environment variables. 键入SET命令并检查环境变量。 Redirect the SET output to a text file for later reference. 将SET输出重定向到文本文件以供以后参考。 Now go to whatever is your normal run-time environment for the offending program. 现在转到有问题的程序的正常运行时环境中。 Compare the output of SET with the one you saved. 将SET的输出与您保存的输出进行比较。

Eliminate those things which are the same, and one of the remaining values is the magic you seek. 消除那些相同的东西,剩下的价值之一就是您寻求的魔法。

For example, on my own machine, where I'm running VS 2008, I find values like this: 例如,在运行VS 2008的我自己的计算机上,我发现这样的值:

Path=c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN;c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools;c:\Windows\Microsoft.NET\Framework\v3.5;c:\Windows\Microsoft.NET\Framework\v2.0.50727;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCPackages;C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin;C:\Programs\Vim\Vim73\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\
VSINSTALLDIR=c:\Program Files (x86)\Microsoft Visual Studio 9.0
WindowsSdkDir=C:\Program Files\Microsoft SDKs\Windows\v6.0A\
_ACP_ATLPROV=c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\Bin\ATLProv.dll
_ACP_INCLUDE=c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\include;C:\Program Files\Microsoft SDKs\Windows\v6.0A\\include;C:\Program Files\Microsoft SDKs\Windows\v6.0A\include
_ACP_LIB=c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\lib;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\lib;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\lib\i386;C:\Program Files\Microsoft SDKs\Windows\v6.0A\\lib;C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib;C:\Program Files (x86)\Microsoft Visual Studio 9.0\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\lib
_ACP_PATH=c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin;C:\Program Files\Microsoft SDKs\Windows\v6.0A\\bin;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\bin;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\tools;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\ide;C:\Program Files (x86)\HTML Help Workshop;C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin;C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program Files (x86)\Microsoft Visual Studio 9.0\;C:\WINDOWS\SysWow64;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\

My mspdb80.dll file (VS 2008) can be found in two directories, 我的mspdb80.dll文件(VS 2008)可以在两个目录中找到,

C:\Program Files (x86)\Common Files\microsoft shared\VSA\9.0\VsaEnv\mspdb80.dll
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\mspdb80.dll

Neither of which paths appear anywhere in my "normal" environment variables, but the ...\\Common7\\IDE\\ folder appears in both PATH and _ACP_PATH in my VS dev environment. 在我的“正常”环境变量中,这两个路径都没有出现,但是在我的VS开发环境中,... \\ Common7 \\ IDE \\文件夹同时出现在PATH和_ACP_PATH中。

Running linker from normal environment = fail. 从正常环境运行链接程序=失败。

Running linker from dev environment = success. 从开发环境运行链接程序=成功。

Recommend: locate your mspdb10.dll file, add that path to your environment either in PATH or in _ACP_PATH (or both) and try again. 推荐:找到您的mspdb10.dll文件,将该路径添加到环境中的PATH或_ACP_PATH(或两者)中,然后重试。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM