[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1812
  • Last Modified:

Unicode version of CreateDirectory() API

When I call CreateDirectory("c:\\"invalid name"\tmp") ) Win32 API it returns FALSE and GetLastError() returns a valid error code (incorrect syntax). Now, if I call unicode version of CreateDirectoryW() GetLastError() does not report any error. How can I check the error of unicode CreateDirectoryW()?
0
alex1234
Asked:
alex1234
  • 5
  • 3
  • 2
  • +1
1 Solution
 
_corey_Commented:
CreateDirectoryW is failing, correct?
0
 
alex1234Author Commented:
Yes, it returns FALSE and, according to API documentation, GetLastError() should return error code and it didn't for CreateDirectoryW().

I call CreateDirectoryW() by loading kernel32.dll dynamically and explicitly calling CreateDirectoryW(). Calling CreateDirectoryA() from the same dynamically loaded kernel32.dll causes GetLastError() return correct error code
0
 
_corey_Commented:
Is there any particular reason you don't just call CreateDirectoryW explicitely from the code?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
alex1234Author Commented:
I just called CreateDirectoryW() directly - same result
0
 
Anthony2000Commented:
Can you post your code snippet?
0
 
alex1234Author Commented:
The code (two tests under Windows2000):

1)

if ( ! CreateDirectory("c:\\\"invalid name\"\\Tmp") )
{
DWORD err = GetLastError();
}

In this case err == some error code (incorrect syntax, I don't remember the code value now)

1)

if ( ! CreateDirectoryW("c:\\\"invalid name\"\\Tmp") )
{
DWORD err = GetLastError();
}

In this case err == 0 and directory was not created
0
 
MadshiCommented:
Don't you need to put a "L" before C++ wide strings?

L"blabla"
0
 
alex1234Author Commented:
Madshi, you've just directed me to resolving the issue! The exact code thet didn't work was:

CreateDirectoryW(_bstr_t("\\\\?\\c:\\\"invalidname\""), NULL );

I've tried this and it worked:

CreateDirectoryW(L"\\\\?\\c:\\\"invalidname\"", NULL );

So, the problem was _bstr_t, somehow CreateDirectoryW() does not report errors when _bstr_t is used. I still don't understand why??



0
 
MadshiCommented:
Actually I'm a Delphi programmer, so it's hard for me to tell. But in Delphi if I use automated wide string support, the GetLastError value might be destroyed, too. Is that _bstr_t something like automated wide string support? In that case please try to understand what the compiler makes out of your code! It will first have to convert the ansi string to wide, then it will call CreateDirectoryW with the converted string, then it will free the converted string. Only THEN you ask GetLastError. I think freeing a wide string destroys the GetLastError value. Do you see the problem?

But as I said, I'm only guessing here...
0
 
_corey_Commented:
The SysFreeString in _bstr_t might be the culprit, but hard to tell since the function doesn't say it will use error values.

Definately something coming off the stack is killing the error values.
0
 
alex1234Author Commented:
Madshi,

it looks like you are 100% right! This code works:

_bstr_t str("\\\\?\\c:\\\"invalidname\"");
if ( ! CreateDirectoryW((LPCWSTR)(wchar_t*)str, NULL ) )
{
DWORD err = GetLastError();
}

Thanks!!!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now