Пример на С++ из MSDN не работает! Подскажите,пожалуйста,как устранить следующие ошибки!!!
Вылезают следующие ошибки:
Error 1 error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [16]' to 'LPCWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 30
Error 2 error C2664: 'GetTempPathW' : cannot convert parameter 2 from 'char [4096]' to 'LPWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 40
Error 3 error C2664: 'GetTempFileName' : cannot convert parameter 1 from 'char [4096]' to 'LPCTSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 47
Error 4 error C2664: 'CharUpperBuffW' : cannot convert parameter 1 from 'char [4096]' to 'LPWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 72
Error 5 error C2664: 'MoveFileExW' : cannot convert parameter 1 from 'char [260]' to 'LPCWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 88
#include "stdafx.h"
#include "windows.h"
#include "stdio.h"
#include "WinNT.h"
using namespace System;
#define BUFSIZE 4096
int main(array<System::String ^> ^args)
{
HANDLE hFile;
HANDLE hTempFile;
DWORD dwBytesRead, dwBytesWritten, dwBufSize=BUFSIZE;
char szTempName[MAX_PATH];
char buffer[BUFSIZE];
char lpPathBuffer[BUFSIZE];
// Open the existing file.
hFile = CreateFile("d:/original.txt", // file name
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file.");
return 0;
}
// Get the temp path
GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path
// Create a temporary file.
GetTempFileName(lpPathBuffer, // directory for temp files
"NEW", // temp file name prefix
0, // create unique name
szTempName); // buffer for name
hTempFile = CreateFile((LPTSTR) szTempName, // file name
GENERIC_READ | GENERIC_WRITE, // open for read/write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hTempFile == INVALID_HANDLE_VALUE)
{
printf("Could not create temporary file.");
return 0;
}
// Read 4K blocks to the buffer.
// Change all characters in the buffer to upper case.
// Write the buffer to the temporary file.
do
{
if (ReadFile(hFile, buffer, 4096,
&dwBytesRead, NULL))
{
CharUpperBuff(buffer, dwBytesRead);
WriteFile(hTempFile, buffer, dwBytesRead,
&dwBytesWritten, NULL);
}
} while (dwBytesRead == BUFSIZE);
// Close both files.
CloseHandle(hFile);
CloseHandle(hTempFile);
// Move the temporary file to the new text file.
if (!MoveFileEx(szTempName,
"allcaps.txt",
MOVEFILE_REPLACE_EXISTING)) {
printf("Could not move temp file.");
return 0;
}
//Console::WriteLine(L"Hello World");
return 0;
Здравствуйте, 2_amigos, Вы писали:
_>Пример на С++ из MSDN не работает! Подскажите,пожалуйста,как устранить следующие ошибки!!!
У вас включёна поддержка UNICODE при компиляции, а строки — не ункодные. Либо уберите уникод поддержку, либо пишите так:
hFile = CreateFile(L"d:/original.txt", // file name
... << RSDN@Home 1.1.4 stable SR1 rev. 568>>
Здравствуйте, 2_amigos, Вы писали:
_>Пример на С++ из MSDN не работает! Подскажите,пожалуйста,как устранить следующие ошибки!!!
_>Вылезают следующие ошибки:
_>Error 1 error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [16]' to 'LPCWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 30
_>Error 2 error C2664: 'GetTempPathW' : cannot convert parameter 2 from 'char [4096]' to 'LPWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 40
_>Error 3 error C2664: 'GetTempFileName' : cannot convert parameter 1 from 'char [4096]' to 'LPCTSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 47
_>Error 4 error C2664: 'CharUpperBuffW' : cannot convert parameter 1 from 'char [4096]' to 'LPWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 72
_>Error 5 error C2664: 'MoveFileExW' : cannot convert parameter 1 from 'char [260]' to 'LPCWSTR' d:\MyDocuments\Visual Studio 2005\Projects\c_plus\10\10\10.cpp 88
... (skipped)
_> hFile = CreateFile("d:/original.txt", // file name
_> GENERIC_READ, // open for reading
_> 0, // do not share
_> NULL, // default security
_> OPEN_EXISTING, // existing file only
_> FILE_ATTRIBUTE_NORMAL, // normal file
_> NULL); // no template
_> if (hFile == INVALID_HANDLE_VALUE)
_> {
_> printf("Could not open file.");
_> return 0;
_> }
Ну а чо тут непонятного та? Компилишь в уникоде, а строки используешь однобайтовые...
CreateFile(
_T("..."), ...); ....
_stprintf(
_T("Could not open file."));
Или убери #define UNICODE
Только Путин, и никого кроме Путина! О Великий и Могучий Путин — царь на веки веков, навсегда!
Смотрю только Соловьева и Михеева, для меня это самые авторитетные эксперты.
КРЫМ НАШ! СКОРО И ВСЯ УКРАИНА БУДЕТ НАШЕЙ!
Здравствуйте, Cruser, Вы писали:
C>Здравствуйте, 2_amigos, Вы писали:
_>>Пример на С++ из MSDN не работает! Подскажите,пожалуйста,как устранить следующие ошибки!!!
C> У вас включёна поддержка UNICODE при компиляции, а строки — не ункодные. Либо уберите уникод поддержку, либо пишите так:
C>C> hFile = CreateFile(L"d:/original.txt", // file name
C>
Как лучше сделать: убрать юникод из проекта или кое-что дописать в проекте???