|
i am creating a function IsFolderExists(LPTSTR str)
str = "C:\New Folder"; //It Fails to get the Window Folder
but when i
str = "C:\\New Folder"; //It gets the Windows Folder
am i doing wrong, or \\ is required. If required then how to build a path
Example : C:\New Folder into C:\\New Folder. Help.....
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
IsFolderExists(lpCmdLine)
return TRUE;
}
void IsFolderExists(LPTSTR str)
{
WIN32_FIND_DATA wfd;
HANDLE hFile;
hFile = FindFirstFile(str,&wfd);
if ( hFile != INVALID_HANDLE_VALUE )
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0)
MessageBox(NULL,_T("Is Directory"),_T(""),MB_OK);
else
MessageBox(NULL,_T("Is File"),_T(""),MB_OK);
}
else
{
MessageBox(NULL,_T("Neither File or Folder"),_T(""),MB_OK);
}
}
When i drag a folder [ NewFolder] on myapp.exe it works but
when i drag a folder having space [ New Folder ] it doesn't work.
please help...
Some Day I Will Prove MySelf :: GOLD
modified on Saturday, January 22, 2011 12:42 AM
|
|
|
|
|
The "\" is known as the (or one of?) "escape" character.
that means that if you want to put a tab, for example in a string you would put "\t" to indicate a tablature; that means that the string will look at the "\" and the next character after that and see a "t" and will understand it as a "tablature" (same thing for quotes and double quotes)
so, in general, when a string (CString) encounters a "\" in a string it will check the next character and see if it means something.
the question is how can I put a literal "\" in string without being recognize as an escape character ?
the anwser is to double the "\" ...
str = "C:\\Windows";
When hard-coding paths in string one should always put "\\", but, for example, if you have a UI with an edit box to type in the path, you don't need to use the "\\" you can just use the "\".
Watched code never compiles.
|
|
|
|
|
Of course '\' is the only C/C++ escape character for literal strings.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
IsFolderExists(lpCmdLine)
return TRUE;
}
void IsFolderExists(LPTSTR str)
{
WIN32_FIND_DATA wfd;
HANDLE hFile;
hFile = FindFirstFile(str,&wfd);
if ( hFile != INVALID_HANDLE_VALUE )
{
if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0)
MessageBox(NULL,_T("Is Directory"),_T(""),MB_OK);
else
MessageBox(NULL,_T("Is File"),_T(""),MB_OK);
}
else
{
MessageBox(NULL,_T("Neither File or Folder"),_T(""),MB_OK);
}
}
When i pass the Folder name from command line like,
myapp.exe C:\Windows -result is neither file or folder but when used as
myapp.exe C:\\Windows -result is Is Directory.
How to solve this. Help.
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
You should also print out the contents of wfd.cFileName to see exactly which file or directory you are displaying attributes for.
I must get a clever new signature for 2011.
|
|
|
|
|
how can I put a literal "\" in
string without being recognize as an escape
character ?
the anwser is to double the "\" ...
s t r = " C : \ \ W i n d o w s " ;
i agree with you. But i want to do it programatically. I want to replace \ with ''\\''. Please help
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
The '\\' notation is only needed for the compiler. It isn't needed in the runtime. (when you select a file from the open file dialog, the path is returned as "C:\New Folder"
If you want to replace them for other reasons, then use 4 '\'. i.e. to specify a SMB server, you would use CString strServer = "\\\\server_name\\folder"; .
To replace it you just need a replace function. If you are using something liks CString , it as a Replace() method.
CString strName = "\\hello";
strName.Replace("\\", "\\\\");
|
|
|
|
|
Thanx a lot.
Some Day I Will Prove MySelf :: GOLD
|
|
|
|
|
|
You already got answers the last time you posted, what's wrong with them ?
Please, read the posting guidelines at the top of this forum before posting the next time.
|
|
|
|
|
Hello guys!
I have to connect to WebDAV Server, example: http://webdav.pspace.co.kr
But I cannot find any code example for DavAddConnection Function.
And, in MSDN I found this:
"Use this function when you are connecting to a WebDAV server using the Secure Sockets Layer (SSL) protocol and therefore must specify a certificate. To connect to a WebDAV server without specifying a certificate, use a Windows networking function such as WNetAddConnection2 or WNetAddConnection3."
Does anyone know some examples of DavAddConnection?
Found some code examples for WNetAddConnection2 and tried to connect, but, unsuccessfully.
CString passW=_T("pwd123");
CString userN=_T("user1");
CString locname=_T("webdav.pspace.co.kr");
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
memset(&nr, 0, sizeof (NETRESOURCE));
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = (LPTSTR)((LPCTSTR)locname);
nr.lpRemoteName = (LPTSTR)((LPCTSTR)locname);
nr.lpProvider = NULL;
dwFlags = CONNECT_UPDATE_PROFILE;
dwRetVal = WNetAddConnection2(&nr, passW, userN, FALSE);
if (dwRetVal == NO_ERROR)
AfxMessageBox(_T("Connection added to"));
else
AfxMessageBox(_T("Error"));
I'm getting "Error".
By the way, I'm I doing right trying to connect to WebDAV server via this technic?
Thank you.
modified on Thursday, January 20, 2011 8:30 PM
|
|
|
|
|
Use this to see what error you are getting. That may shed some light on what may be causing it
if (dwRetVal == NO_ERROR)
AfxMessageBox(_T("Connection added to"));
else {
TCHAR szError[32];
_stprintf(szError, _T("Error %u"), dwRetVal);
AfxMessageBox(szError);
}
|
|
|
|
|
Thanks for the reply.
I'm getting "Error 487".
|
|
|
|
|
MSDN[^] says
Error Name: ERROR_INVALID_ADDRESS
Error Code: 487 (0x1E7)
Error Text: Attempt to access invalid address.
|
|
|
|
|
Check out your last parameter in WNetAddConnection2(&nr, passW, userN, FALSE);
According to MSDN[^] 0 is not a legal value for the parameter dwFlags and FALSE is #define FALSE 0 .
Did you mean to pass in your variable dwFlags ? (in the sample on MSDN they do)
|
|
|
|
|
Hello.
I have tried a couple of thing to make the code work. But, still, I cannot connect to our WebDAV server "webdav.pspace.co.kr".
But, using the code shown below, I'm getting a positive answer: "Connection added to":
CString locname=_T("\\\\www.barracudaserver.com\\dav\\");
DWORD dwRetVal;
NETRESOURCE nr;
DWORD dwFlags;
memset(&nr, 0, sizeof (NETRESOURCE));
nr.dwScope=RESOURCE_GLOBALNET;
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = NULL;
nr.lpRemoteName = (LPTSTR)((LPCTSTR)locname);
nr.lpProvider = _T("");
dwFlags = CONNECT_UPDATE_PROFILE;
dwRetVal = WNetAddConnection2(&nr, _T(""), _T(""), dwFlags);
if (dwRetVal == NO_ERROR)
{
AfxMessageBox(_T("Connection added to"));
}
else
{
TCHAR szError[32];
_stprintf(szError, _T("Error %u"), dwRetVal);
AfxMessageBox(szError);
}
In the case of locname=_T("\\\\webdav.pspace.co.kr\\") the result is "Error 53:", which means "The network path you specified is not found."
May be something is wrong with the server or may be I'm using the wrong approach.
Any ideas?..
|
|
|
|
|
Running your code with locname=_T("\\\\webdav.pspace.co.kr\\") and running a packet sniffer I get the response
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache Server at webdav.pspace.co.kr Port 80</address>
</body></html>
I'm sure you can figure out what the problem is from the response
|
|
|
|
|
Dear Andrew!
Thanks for your help!
I'm sorry for being pesky and dumb.
I can connect to the server using browser with username and password.
But, I'm not being able to do it using my app.
Still I get the same error: "The network path you specified is not found.".
|
|
|
|
|
I have never used WNetAddConnection2, and what I have been telling you is from reading MSDN and running your code on my computer so i dont know of any reasons that this might be hapening. Since i dont have an account on webdav.pspace.co.kr[^] the best i can offer is that you run Wireshark[^] and start the sniffer while you try to connect to the server to see what the server is sending back to you.
|
|
|
|
|
Hi,
you might want to check out this part
nr.lpLocalName = (LPTSTR)((LPCTSTR)locname);
based on MSDN: http://msdn.microsoft.com/en-us/library/aa385413(v=vs.85).aspx[^]
lpLocalName should be NULL in you case
"A pointer to a null-terminated string that specifies the name of a local device to redirect, such as "F:" or "LPT1". The string is treated in a case-insensitive manner."
but you use webdav.pspace.co.kr, which may cause the problem I think...
|
|
|
|
|
Hi,
I want to use a Active X contol in a win32 application.
How I can add that control to my project?
If you can Please explain me with a sample code.
Regards,
M.Mathivanan
|
|
|
|
|
Active X components are basically COM components.
So you can create an instance of it and then use the control in your application.
I am a HUMAN. I have that keyword in my name........
_AnsHUMAN_
|
|
|
|
|
That's a but simplistic. An ActiveX control is a COM object but writing code to correctly host it is far from trivial: in fact it's hard. The easiest way is to use MFC's or ATL's hosting code.
Steve
|
|
|
|
|
|
Hi,
Is there anyway to get the country name of system?
|
|
|
|