使用VsDevCmd.bat以编程方式创建本机DLL

时间:2015-05-07 20:41:28

标签: winapi

我需要使用参数运行VsDevCmd.bat但是,当我运行此代码时,VsDevCmd.bat会被执行,但不会传入任何参数。

代码如下:

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\Tools\\VsDevCmd.bat";
ShExecInfo.lpParameters = "cl.exe /D_USRDLL /D_WINDLL C:\\MyDLL.cpp C:\\MyDLL.def /link /DLL /OUT:MyDLL.dll";
ShExecInfo.lpDirectory = "C:\\";
ShExecInfo.nShow = SW_MAXIMIZE;
ShExecInfo.hInstApp = NULL;

int nRet = (int) ShellExecuteEx(&ShExecInfo);

if (nRet >= 32)
{
    DWORD dw = GetLastError();
    char szMsg[250];
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, szMsg, sizeof(szMsg), NULL);
    MessageBox(hWnd, szMsg, "ERROR", MB_ICONEXCLAMATION | MB_OK);
}

WaitForSingleObject (ShExecInfo.hProcess, INFINITE);

1 个答案:

答案 0 :(得分:0)

如果要从应用程序中编译DLL并且使用Visual Studio,为什么不执行devenv来构建包含要构建的DLL的解决方案文件?

查看devenv的文档。

所以你的代码应该是这样的:

SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "devenv";
ShExecInfo.lpParameters = "/build yoursolution.sln";
ShExecInfo.lpDirectory = "C:\\";
ShExecInfo.nShow = SW_MAXIMIZE;
ShExecInfo.hInstApp = NULL;

if (ShellExecuteEx(&ShExecInfo) != FALSE)
{
    WaitForSingleObject (ShExecInfo.hProcess, INFINITE);

    DWORD exitCode;        
    if (GetExitCodeProcess(ShExecInfo.hProcess, &exitCode) != FALSE)
    {
        /*
        **  Process exit code of devenv 
        **  I haven't found something on the internet but I assume that exitCode=0 means success!
        */
        if (exitCode != 0)
        {
            MessageBox(hWnd, "devenv failed!", "ERROR", MB_ICONEXCLAMATION | MB_OK);
        }
    }
    else
    {
        DWORD dw = GetLastError();
        char szMsg[250];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, szMsg, sizeof(szMsg), NULL);
        MessageBox(hWnd, szMsg, "ERROR", MB_ICONEXCLAMATION | MB_OK);        
    }

    CloseHandle(ShExecInfo.hProcess);
}
else
{
    DWORD dw = GetLastError();
    char szMsg[250];
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, szMsg, sizeof(szMsg), NULL);
    MessageBox(hWnd, szMsg, "ERROR", MB_ICONEXCLAMATION | MB_OK);
}