所有测试环境为vs2015.
1、文件弹窗工程,win32控制台:
#include "stdafx.h"
#include <aclapi.h>
#include <Shellapi.h>
#include <tchar.h>
#include <string>
#include <shlobj.h>
#include <iostream>
#include <sstream>
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED)
{
std::string tmp = (const char *)lpData;
std::cout << "path: " << tmp << std::endl;
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
}
return 0;
}
std::string TCHAR2STRING(TCHAR *STR)
{
int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, NULL);
char* chRtn = new char[iLen * sizeof(char)];
WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);
std::string str(chRtn);
return str;
}
std::string BrowseFolder(std::string saved_path)
{
TCHAR path[MAX_PATH];
std::string temStr;
const char * path_param = saved_path.c_str();
BROWSEINFO bi = { 0 };
bi.lpszTitle = (L"Browse for folder...");
//bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.ulFlags = BIF_BROWSEFILEJUNCTIONS;
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM)path_param;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl != 0)
{
//get the name of the folder and put it in path
SHGetPathFromIDList(pidl, path);
//free memory used
IMalloc * imalloc = 0;
if (SUCCEEDED(SHGetMalloc(&imalloc)))
{
imalloc->Free(pidl);
imalloc->Release();
}
temStr = TCHAR2STRING(path);
return temStr;
}
return "";
}
void main()
{
std::string path = BrowseFolder("");
std::cout << path << std::endl;
}
2、启动EXE,获取文件路径工程,win32:
#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include <iostream>
#include <string>
#include <shlobj.h>
#include <iostream>
#include <sstream>
/* protos */
DWORD WINAPI ReadFromPipe(LPVOID args);
/* globals */
#define CHUNK 25
#define STATICBUFFERSIZE 1000
typedef struct
{
HANDLE pipe;
char buffer[STATICBUFFERSIZE];
} pipeinfo;
pipeinfo Out = { INVALID_HANDLE_VALUE, '/0' };
pipeinfo Err = { INVALID_HANDLE_VALUE, '/0' };
int main(int argc, char *argv[])
{
//if(argc < 2)
//{
// printf("pipetest ??");
// return 0;
//}
//argv[ ] = L"D:\temp\TestNamedPipeAndThread\x64\Debug\Admini.exe";
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;
DWORD threadID;
char msg[300];
BOOL ok;
HANDLE hProcess, h, pipeThreads[2];
char cmdline[100];
CHAR szBuffer[256];
DWORD dwNumberOfBytesRead = 0;
CHAR szMsg[100];
hProcess = GetCurrentProcess();
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = INVALID_HANDLE_VALUE;
ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
/* Create a non-inheritible pipe. */
CreatePipe(&Out.pipe, &h, &sa, 0);
/* Dupe the write side, make it inheritible, and close the original. */
DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
/* Same as above, but for the error side. */
CreatePipe(&Err.pipe, &h, &sa, 0);
DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
strcpy_s(cmdline, "D:\\temp\\TestNamedPipeAndThread\\x64\\Debug\\Admini.exe");
//cmdline = L"D:\temp\TestNamedPipeAndThread\x64\Debug\Admini.exe"
/*for (int i = 1; i < argc; i++)
{
strcat_s(cmdline, argv[i]);
strcat_s(cmdline, " ");
}
*/
ok = CreateProcess(NULL, cmdline, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL, &si, &pi);
if (!ok)
{
DWORD err = GetLastError();
int chars = _snprintf_s(msg, sizeof(msg) - 1, "Tried to launch: /%s / , but got error [%u]: ", cmdline, err);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, &msg[chars], (300 - chars), 0);
WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err, NULL);
return 2;
}
/* Close our references to the write handles that have now been inherited.*/
CloseHandle(si.hStdOutput);
CloseHandle(si.hStdError);
WaitForInputIdle(pi.hProcess, 5000);
CloseHandle(pi.hThread);
/
while (TRUE)
{
bool bTest = ReadFile(
Out.pipe, // handle of the read end of our pipe
&szBuffer, // address of buffer that receives data
256, // number of bytes to read
&dwNumberOfBytesRead, // address of number of bytes read
NULL // non-overlapped.
);
if (!bTest) {
sprintf_s(szMsg, "Error #%d reading pipe.", GetLastError());
MessageBox(NULL, szMsg, "WinPipe", MB_OK);
break;
}
// do something with data.
szBuffer[dwNumberOfBytesRead] = 0; // null terminate
MessageBox(NULL, szBuffer, "WinPipe", MB_OK);
}
///
/* Start the pipe reader threads.*/
//pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID);
//pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID);
/* Block waiting for the process to end.*/
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
/* Wait for our pipe to get done reading, should it be a little slow.*/
WaitForMultipleObjects(2, pipeThreads, TRUE, INFINITE);
// CloseHandle(pipeThreads[0]);
// CloseHandle(pipeThreads[1]);
return 0;
}
DWORD WINAPI ReadFromPipe(LPVOID args)
{
pipeinfo *pi = (pipeinfo *)args;
char *lastBuf = pi->buffer;
DWORD dwRead;
BOOL ok;
char buf[2000];
again:
ok = ReadFile(pi->pipe, buf, 2000, &dwRead, 0L);
if (!ok || dwRead == 0)
{
CloseHandle(pi->pipe);
return 0;
}
else
{
buf[dwRead] = 0;
//printf("%s", buf);
std::string temStr;
temStr = buf;
std::cout <<"test"<< temStr << std::endl;
}
goto again;
return 0; /* makes the compiler happy */
}