VC从EXE文件弹窗中获取路径


所有测试环境为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 */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值