C++编程相关——综合

1、c++ win32程序如何创建打开文件对话框:

TCHAR szBuffer[MAX_PATH] = { 0 };//保存读取的文件的文件名,注意类型是TCHAR
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = TEXT("Xef文件(*.xef)\0*.xef\0所有文件(*.*)\0*.*\0");//要选择的文件后缀   
ofn.lpstrInitialDir = TEXT("D:\\Program Files");//默认的文件路径   
ofn.lpstrFile = szBuffer;//存放文件的缓冲区   
ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
ofn.nFilterIndex = 0;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;//标志如果是多选要加上OFN_ALLOWMULTISELECT  
BOOL bSel = GetOpenFileName(&ofn);

2、如何将TCHAR转为std::string:
string filename;
//获取转换所需的目标缓存大小  
DWORD dBufSize = WideCharToMultiByte(CP_OEMCP, 0, szBuffer, -1, NULL, 0, NULL, FALSE);

//分配目标缓存  
char *dBuf = new char[dBufSize];
memset(dBuf, 0, dBufSize);

//转换  
int nRet = WideCharToMultiByte(CP_OEMCP, 0, szBuffer, -1, dBuf, dBufSize, NULL, FALSE);

if (nRet <= 0)
{
cout << "打开文件失败" << endl;
}
else
{
filename = dBuf;
cout << "打开文件成功:  " << endl;
cout << filename << endl;
}
delete[]dBuf;

3、c++里如何创建线程,并为线程函数传递参数:

参数要写为结构体形式:

typedef struct SParam
{
string filename;
}*sParam;

线程函数:

DWORD WINAPI Fun(LPVOID lpParamter)
{
sParam sparam;
sparam = (sParam)lpParamter;
Class1 ^playback = gcnew Class1();
AllocConsole();
_cprintf("in play: \n");
playback->xef_dir = gcnew System::String(sparam->filename.c_str());
flag = playback->call_play();
return 0;
}

创建线程并传递参数:

sParam param=new SParam();
param->filename = filename;
HANDLE hThread = CreateThread(NULL, 0, Fun, param, 0, NULL);

CloseHandle(hThread);

参照:http://www.cnblogs.com/XiHua/p/5028329.html

4、c++调用c#编译的dll时,如何将std::string传递给System::String^:

参考:http://blog.csdn.net/windren06/article/details/7839985

在c++的代码中:

Class1 ^playback = gcnew Class1();
playback->xef_dir = gcnew System::String(sparam->filename.c_str());
flag = playback->call_play();

其中Class1是c#里的类,而xef_dir是在c#里定义的public static string类型,而call_play()是Class1类中的方法。

5、如何将c#的Systm::string^ 转为c++的std::string:

参考:https://stackoverflow.com/questions/1300718/c-net-convert-systemstring-to-stdstring

//2017/9/5

1、windows下控制台程序判断目录是否存在及创建目录:

头文件:

判断目录是否存在:if(_access("目录名",0) == -1){}

创建目录: int flag = _mkdir("目录名");


你可能感兴趣的:(编程语言)