Tech Support > Microsoft Windows > Development Resources > NetShareAdd/Unicode? issues
NetShareAdd/Unicode? issues
Posted by Conor on May 5th, 2004


I have an issue where I am getting back errors from the NetShareAdd
function. The function is returning ERROR_INVALID_NAME and is setting
my "parm_err" variable to 8. From reading up on this it seams to be an
issue that it requires unicode. I have changed the entry point to
"wWinMainCRTStartup" and changed my main to "wWinMain". This builds
fine but produces the same problem as detailed above. My code is
attached below.

My Project details are: VC6 Win32 project with above changes for
unicode. The NetShareAdd code is almost identical to MSDN example.

What am I doing wrong?



#define UNICODE

#include <windows.h>
#include <lm.h>

#include <string>

using namespace std;

int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
NET_API_STATUS res;
SHARE_INFO_2 p;
DWORD parm_err = 0;
string errString;

p.shi2_netname = "MYSHARE";
p.shi2_type = STYPE_DISKTREE;
p.shi2_remark = "";
p.shi2_permissions = 0;
p.shi2_max_uses = 4;
p.shi2_current_uses = 0;
p.shi2_path = "C:\\";
p.shi2_passwd = NULL; // no password

res=NetShareAdd(NULL, 2, (LPBYTE) &p, &parm_err);

if(res==0) {
// Share passed
} else {
if(res==ERROR_ACCESS_DENIED){
errString = "ERROR_ACCESS_DENIED";
} else if(res==ERROR_INVALID_LEVEL) {
errString = "ERROR_INVALID_LEVEL";
} else if(res==ERROR_INVALID_NAME) {
errString = "ERROR_INVALID_NAME";
} else if(res==ERROR_INVALID_PARAMETER) {
errString = "ERROR_INVALID_PARAMETER";
} else if(res==NERR_DuplicateShare) {
errString = "NERR_DuplicateShare";
} else if(res==NERR_RedirectedPath) {
errString = "NERR_RedirectedPath";
} else if(res==NERR_UnknownDevDir) {
errString = "NERR_UnknownDevDir";
}
}

return 0;
};

Posted by Peter Mourfield on May 5th, 2004


try replacing these lines:

p.shi2_netname = L"MYSHARE";
p.shi2_path = L"C:\\";

In the SHARE_INFO_2 struct both of these elements are defined as LPWSTR
which means they need to be wide strings. the L macro does this for you.

Pete Mourfield


"Conor" <conor_gallagher@hotmail.com> wrote in message
news:2f8abc12.0405050658.39d83476@posting.google.c om...


Posted by Boris Dynin on May 5th, 2004


"Peter Mourfield" <pete@mourfield.com> wrote in message
news:109i6f01v5ibdd8@corp.supernews.com...
Platform SDK.
In file ...\vc98\include\lmshare.h (VC6 header):

typedef struct _SHARE_INFO_2 {
LPTSTR shi2_netname;
DWORD shi2_type;
LPTSTR shi2_remark;
DWORD shi2_permissions;
DWORD shi2_max_uses;
DWORD shi2_current_uses;
LPTSTR shi2_path;
LPTSTR shi2_passwd;
} SHARE_INFO_2, *PSHARE_INFO_2, *LPSHARE_INFO_2;

but in file ...\Microsoft SDK\include\lmshare.h (SDK header):

typedef struct _SHARE_INFO_2 {
LMSTR shi2_netname;
DWORD shi2_type;
LMSTR shi2_remark;
DWORD shi2_permissions;
DWORD shi2_max_uses;
DWORD shi2_current_uses;
LMSTR shi2_path;
LMSTR shi2_passwd;
} SHARE_INFO_2, *PSHARE_INFO_2, *LPSHARE_INFO_2;

where LMSTR is declared as (file ...\Microsoft SDK\include\lmcons.h):

//
// Only the UNICODE version of the LM APIs are available on NT.
// Non-UNICODE version on other platforms
//
#if defined( _WIN32_WINNT ) || defined( WINNT ) || defined( __midl ) \
|| defined( FORCE_UNICODE )
#define LMSTR LPWSTR
#define LMCSTR LPCWSTR
#else
#define LMSTR LPSTR
#define LMCSTR LPCSTR
#endif

So, if VC6 headers are used, 'shi2_netname' can be either char* or WCHAR*.
But, if SDK headers are used, 'shi2_netname' is always WCHAR* (on NT, W2k,
etc.).

However, in all cases (even when using VC6 headers) UNICODE value should be
supplied (on NT, W2k, etc.). For example, with VC6 headers and non-UNICODE
build configuration:

SHARE_INFO_2 p;
p.shi2_netname = (char*) L"MYSHARE";
// etc.

Boris



Posted by Conor on May 7th, 2004


Cheers for the feed back guys,

I thought alright it may have been an issue with the char* vars
alright. Originally when I was writing it I was using the TEXT("")
macro but it was giving me the error that it couldn't convert it to
char*. TEXT("") as far as I'm aware is pretty much a macro that
incorporates the L macro so I would encounter the same problem.

What I basically did, using the info Boris gave me, was created my own
typedef:

typedef struct _MY_SHARE_INFO_2 {
LPTSTR shi2_netname;
DWORD shi2_type;
LPTSTR shi2_remark;
DWORD shi2_permissions;
DWORD shi2_max_uses;
DWORD shi2_current_uses;
LPTSTR shi2_path;
LPTSTR shi2_passwd;
} MY_SHARE_INFO_2, *PMYSHARE_INFO_2, *LPMYSHARE_INFO_2;


And then used this instead.

Cheers guys.


Similar Posts