Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unicode version of CreateDirectory() API

Posted on 2004-04-22
11
Medium Priority
?
1,764 Views
Last Modified: 2013-12-03
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
Comment
Question by:alex1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:_corey_
ID: 10892073
CreateDirectoryW is failing, correct?
0
 
LVL 1

Author Comment

by:alex1234
ID: 10892161
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
 
LVL 8

Expert Comment

by:_corey_
ID: 10892258
Is there any particular reason you don't just call CreateDirectoryW explicitely from the code?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 1

Author Comment

by:alex1234
ID: 10892364
I just called CreateDirectoryW() directly - same result
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 10908399
Can you post your code snippet?
0
 
LVL 1

Author Comment

by:alex1234
ID: 10918008
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
 
LVL 20

Expert Comment

by:Madshi
ID: 10918349
Don't you need to put a "L" before C++ wide strings?

L"blabla"
0
 
LVL 1

Author Comment

by:alex1234
ID: 10918748
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
 
LVL 20

Accepted Solution

by:
Madshi earned 500 total points
ID: 10918773
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
 
LVL 8

Expert Comment

by:_corey_
ID: 10918856
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
 
LVL 1

Author Comment

by:alex1234
ID: 10918867
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question