gpt4 book ai didi

c++ - 在 LPSTR 文件路径中用双反斜杠或正斜杠替换反斜杠

转载 作者:行者123 更新时间:2023-11-28 07:30:32 25 4
gpt4 key购买 nike

我正在尝试向 WIN32 API 发送消息以打开 WAV 文件。我可以通过将 LPARAM 参数设置为 L"C:/path/file.wav"来完成此操作,效果非常好!但是,我一直在尝试使用对话框生成文件路径的字符串。 OpenFileDialog 函数可以将选定的文件路径作为 LPSTR 数据类型返回,这听起来很完美!但是,此文件路径被识别为 Windows 文件路径,因此由反斜杠组成。由于这些反斜杠没有被转义或替换为正斜杠,编译器开始因格式不正确的通用字符名称而报错。因此,将 LPSTR 文件路径中的所有\替换为\或/似乎微不足道。我一直在尝试通过多种方式来做到这一点,但似乎没有任何效果。

wchar_t* SelectAudioFile(HWND windowHandle) {
OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = windowHandle;
ofn.lpstrFile = (LPWSTR)szFile;

ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);

ofn.lpstrFilter = L"Waveform Audio File Format (*.wav)\0*.wav\0";

ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box.

if (GetOpenFileName(&ofn)==TRUE) {
CreateFile(ofn.lpstrFile,
GENERIC_READ,
0,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
MessageBox(windowHandle,ofn.lpstrFile,0,0);
}

//Here the backslash should be escaped or replaced.

const wchar_t * currentPath = ofn.lpstrFile; //This is a LPSTR
wchar_t * newPath;
int filePathLength = sizeof(currentPath);

for (int i=0; i < filePathLength; i++) {
if (currentPath[i] == "\\") {
newPath[i] = "\\\\";
} else {
newPath[i] = filePath[i];
}
}
return newPath;


}

下一行会发布一条消息,告诉它打开路径中的某个文件

PostMessageW(hwnd, WMA_OPEN, 0, (LPARAM)SelectAudioFile(hwnd)); 

因此用静态文件路径替换 LPARAM 是可行的!

我应该如何替换文件路径中的反斜杠?

感谢一百万次!

最佳答案

你的代码有几个问题:

  1. 您错误地混合了 Ansi 和 Unicode。

  2. 您正在返回一个指向您尚未为其分配任何内存的本地缓冲区的指针。

  3. 您正在使用 PostMessage() 异步发布本地缓冲区,该缓冲区可能会超出范围并在窗口处理消息之前变得无效。

  4. 即使 GetOpenFileName() 失败或被取消,您仍要发布消息。

试试这个:

bool SelectAudioFile(HWND windowHandle, wchar_t *audioFile, int maxAudioFile)
{
OPENFILENAMEW ofn = {0};

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = windowHandle;
ofn.lpstrFile = audioFile;
ofn.nMaxFile = maxAudioFile;
ofn.lpstrFilter = L"Waveform Audio File Format (*.wav)\0*.wav\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

if (GetOpenFileNameW(&ofn))
{
MessageBoxW(windowHandle, audioFile, 0, 0);
return true;
}

return false;
}

wchar_t szAudioFile[MAX_PATH+1] = {0};
if (SelectAudioFile(hwnd, szAudioFile, MAX_PATH))
SendMessageW(hwnd, WMA_OPEN, 0, (LPARAM)szAudioFile);

或者:

#include <string>

std::wstring SelectAudioFile(HWND windowHandle)
{
OPENFILENAMEW ofn = {0};
wchar_t szFile[MAX_PATH+1] = {0};

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = windowHandle;
ofn.lpstrFile = szFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"Waveform Audio File Format (*.wav)\0*.wav\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

if (GetOpenFileNameW(&ofn))
MessageBoxW(windowHandle, szFile, 0, 0);

return szFile;
}

std::wstring audioFile = SelectAudioFile(hwnd);
if (audioFile.length() > 0)
SendMessageW(hwnd, WMA_OPEN, 0, (LPARAM)audioFile.c_str());

关于c++ - 在 LPSTR 文件路径中用双反斜杠或正斜杠替换反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17830044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com