From: Jack on
Hi vc gurus,

bool LoadMesh(std::wstring szPath)
{
//...

::GetModuleFileNameW(NULL, (LPWCH) szPath.c_str(), szPath.size());

szPath.append(szfilename);



//// Open .dat mesh
HANDLE h = CreateFileW((LPCWSTR)szPath.c_str(), GENERIC_READ, NULL, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE)
{
MessageBoxA(NULL, "Couldn't open file with CreateFile()", "Error",
MB_OK);
return E_FAIL;
}
//.....


The handle "h" is always 0xffffffff, does anyone know which part is wrong?

Thanks
Jack


From: Jack on

>
> bool LoadMesh(std::wstring szPath)
bool LoadMesh(std::wstring szFilename)
{

Thanks
Jack



From: Jack on
A side note:
When I use ASCII, hence CreateFileA,
it loads the mesh correctly (h is valid)...
Thanks
Jack


From: Giovanni Dicanio on
"Jack" <jl(a)knight.com> ha scritto nel messaggio
news:O7ncwGYfKHA.2780(a)TK2MSFTNGP05.phx.gbl...

> bool LoadMesh(std::wstring szPath)

I would pass an input string using a const reference, to avoid deep-copies,
e.g.

bool LoadMesh( const std::wstring & path )

Moreover, I tend to prefer CString class instead of STL [w]string for Win32
coding.


> ::GetModuleFileNameW(NULL, (LPWCH) szPath.c_str(), szPath.size());

I don't think that you can modify the content of an STL string using c_str
(this method returns a const pointer to enforce that).

Moreover, I don't like explicitly specifying the 'W' suffix for Win32 APIs
(compiling in Unicode is just fine).

I would write some code like this:

CString strFilename;
static const int cchFilename = 4096;
TCHAR * pszFilename = strFilename.GetBuffer( cchFilename );
::GetModuleFileName( NULL, pszFilename, cchFilename );
... verify return code...
strFilename.ReleaseBuffer();

Now 'strFilename' should contain the required string.

> //// Open .dat mesh
> HANDLE h = CreateFileW((LPCWSTR)szPath.c_str(),

I would just use CreateFile (not CreateFileW) in Unicode builds.
If szPath is an instance of std::wstring, you don't need an LPCWSTR cast for
the return value of c_str(), it is already a 'const wchar_t *', i.e.
LPCWSTR.

Giovanni



From: Giovanni Dicanio on
"Jack" <jl(a)knight.com> ha scritto nel messaggio
news:O7ncwGYfKHA.2780(a)TK2MSFTNGP05.phx.gbl...

> bool LoadMesh(std::wstring szPath)
[...]
> //// Open .dat mesh
> HANDLE h = CreateFileW((LPCWSTR)szPath.c_str(), GENERIC_READ, NULL, NULL,
> OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
> if (h == INVALID_HANDLE_VALUE)
> {
> MessageBoxA(NULL, "Couldn't open file with CreateFile()", "Error",
> MB_OK);
> return E_FAIL;
> }

I was missing that (not directly related to your original problem, but
probably worth mentioning):

1. Your function's return value is 'bool'. So you should return something
like 'true' or 'false', but not E_FAIL.
You should use E_FAIL if your function returns an HRESULT.

2. Instead of using MessageBoxA, you may want to use MessageBox, and
decorate your string literals using _T() (which will work in both ANSI/MBCS
and Unicode builds) or L"..." (Unicode builds only), e.g.:

MessageBox(
NULL,
L"Couldn't open file with CreateFile()",
L"Error",
MB_OK );

A better approach would be to store strings in resources (instead of using
string literals in source code), and load these messages from resources.

> The handle "h" is always 0xffffffff, does anyone know which part is wrong?

Please verify the content of 'szPath' passed to CreateFileW.

HTH,
Giovanni



 |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: Visual Studio 2008 on Windows 7
Next: std::vector error