WNetAddConnection2 returns error 1200
I have shared network folder on my disk C:\folder.
When I use WNetAddConnection2 I get error 1200. My code is:
Code:
DWORD dwResult;
NETRESOURCE nr;
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = (LPWSTR)"folder";
nr.lpRemoteName = (LPWSTR)"\\\\ASYA\\folder";
nr.lpProvider = NULL;
dwResult = WNetAddConnection2(&nr,NULL,(LPCWSTR) "Nastya",CONNECT_UPDATE_PROFILE);
if (dwResult == NO_ERROR)
wprintf(L"Connection added to %s\n", nr.lpRemoteName);
else
wprintf(L"WNetAddConnection2 failed with error: %u\n", dwResult);
What is my mistake? How can I solve this problem?
Re: WNetAddConnection2 returns error 1200
Are you compiling as Unicode?
1200 means that the specified device name is invalid. Try (not tried)
Code:
nr.lpLocalName = L"folder";
nr.lpRemoteName = L"\\\\ASYA\\folder";
and similar for nastya
Re: WNetAddConnection2 returns error 1200
Quote:
Originally Posted by
NastyaG
What is my mistake? How can I solve this problem?
Your mistake is that you think that casting ANSI string to LPWSTR would miraculously turn it to Unicode string. But the trick just won't work, because what you do is making compiler believe the string is wide string. While it's realy not.
Quite a standard mistake for beginner who does not understand C pointers.
And yes, about how to solve... You either provide wide string literal (this is what 2kaud advised), or explicitly tranlate string to the required encoding. E.g. using CString class:
Code:
CString name = "folder";
nr.lpLocalName = name.GetBuffer();
Oh! And you'd better get to habit of zeroing structures you're not going to initialize in every member.
Code:
NETRESOURCE nr = {0};
Just to avoid passing garbage.
Re: WNetAddConnection2 returns error 1200
Quote:
Originally Posted by
Igor Vartanov
Your mistake is that you think that casting ANSI string to LPWSTR would miraculously turn it to Unicode string. But the trick just won't work, because what you do is making compiler
believe the string is wide string. While it's realy not.
Quite a standard mistake for beginner who does not understand C pointers.
And yes, about how to solve... You either provide wide string literal (this is what 2kaud advised), or explicitly tranlate string to the required encoding. E.g. using CString class:
Code:
CString name = "folder";
nr.lpLocalName = name.GetBuffer();
Oh! And you'd better get to habit of zeroing structures you're not going to initialize in every member.
Code:
NETRESOURCE nr = {0};
Just to avoid passing garbage.
Yes, I changed this:
Code:
DWORD dwResult;
NETRESOURCE nr;
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = L"K:";
nr.lpRemoteName = L"\\\\ASYA\\folder";
nr.lpProvider = NULL;
dwResult = WNetAddConnection2(&nr,L"",L"",CONNECT_UPDATE_PROFILE);
if (dwResult == NO_ERROR)
wprintf(L"Connection added to %s\n", nr.lpRemoteName);
But now I get error 85 (Local Resource is in use) .
I think it is mistake in What I must write to this string?
Re: WNetAddConnection2 returns error 1200
Quote:
Originally Posted by
NastyaG
What I must write to this string?
First you must read the article WNetAddConnection2 top to bottom. Please read it carefully. I'm sure you'll find interesting things there about when RESOURCE_ANY is applicable, and when it's not.
Second, you must make sure there is no K: dos device in yor system when you add the connection. You better do that programmatically right before the call.
Re: WNetAddConnection2 returns error 1200
Quote:
Originally Posted by
Igor Vartanov
First you must
read the article
WNetAddConnection2 top to bottom. Please read it carefully. I'm sure you'll find interesting things there about when RESOURCE_ANY is applicable, and when it's not.
Second, you must make sure there is no K: dos device in yor system when you add the connection. You better do that programmatically right before the call.
Thank's for comment. I read this text but my code doesn't work.
I changed it:
Quote:
DWORD dwResult;
NETRESOURCE nr;
nr.dwType = RESOURCETYPE_DISK;
nr.lpLocalName = L"K:";
nr.lpRemoteName = L"\\\\ASYA\\folder";
nr.lpProvider = NULL;
dwResult = WNetAddConnection2(&nr,L"",L"",CONNECT_UPDATE_PROFILE);
But I get error 85.
I don't know what to write in nr.lpLocalName . Can you explain me,please ?
Before I did a shared network folder, that located in C:\folder.
Maybe this info help to solve my problem(
Re: WNetAddConnection2 returns error 1200
Error 85 is resource in use. From command prompt, what does
show?
If the local resource trying to be used (eg k: ) is shown then you'll need to delete it first (or use another local resource if this resource is being used for another purpose).
From a program, if WNetAddConnection2() fails with error 85 then use WNetCancelConnection2() to delete the resource. See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
Re: WNetAddConnection2 returns error 1200
Quote:
Originally Posted by
NastyaG
I don't know what to write in nr.lpLocalName . Can you explain me,please ?
It depends on what you're trying to achieve. Local name may be dos device name, like what you specify in your snippet, in case you want the network share be mapped to local drive. Or it may be some folder name, like what you did before. Or it may be empty string or NULL, in case you just need to log in to remote system and start using its network paths.
Re: WNetAddConnection2 returns error 1200
Like Ignor asked in post #8, what exactly are you trying to achieve? Depending upon this answer depends what you provide for .lpLocalName.
Try doing what you want to achieve using the NET USE command from a command prompt. When that works then get WNetAddConnection2() to work. It's often easier to try with the NET USE command as you can create new connections, delete existing ones, see connections etc easily.
1 Attachment(s)
Re: WNetAddConnection2 returns error 1200
A working sample goes: Attachment 34469
Code:
J:\Temp\126>make
"Making... "
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
126.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:126.exe
/subsystem:console
/out:netmap.exe
126.obj
J:\Temp\126>netmap K: \\iserver\exchange igor
Add: 0
J:\Temp\126>netmap K: /del
K: was deleted successfully.
J:\Temp\126>make unicode
"Making... /DUNICODE /D"_UNICODE""
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
126.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:126.exe
/subsystem:console
/out:netmap.exe
126.obj
J:\Temp\126>netmap K: \\iserver\exchange igor
Add: 0
J:\Temp\126>netmap K: /del
K: was deleted successfully.
When missing username, error 487 happens.
When remote resource is already connected with different credentials, error 1219 happens.
When remote resource is already connected, error 85 happens.
When remote resource is not found, error 53 happens.
And specifically to the subject of the thread:
Code:
J:\Temp\126>netmap !@#$ \\iserver\exchange1 igor
Add: 1200
J:\Temp\126>net helpmsg 1200
The specified device name is invalid.
So, 1200 means local path is something not a path.