string转LPCWSTR代码如下:
LPCWSTR ccGenerateReportWorker::string2LPCWSTR(std::string stdstr){
size_t strsize = stdstr.length() + 1;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(stdstr.length() - 1));
mbstowcs_s(&convertedChars, wcstring, strsize, stdstr.c_str(), _TRUNCATE);
return wcstring;
}
使用ShellExecute执行外部exe程序的代码如下:
#include "shellapi.h"
bool ExecultExeFile(string exepath) {
LPCWSTR lpcwpath = string2LPCWSTR(exepath);
ShellExecute(NULL, NULL, lpcwpath, NULL, NULL, SW_SHOW);
return true;
}
上述代码均能直接使用。使用上述两段代码,即可实现在vs工程中使用c++代码执行外部exe程序的功能了。