Click to See Complete Forum and Search --> : MultiByteToWideChar() & NetShareCheck()
mocrotronics
July 14th, 2005, 01:22 PM
Hi there,
I would like to check some share info of remote machine's using the NetShareCheck function. It's all working fine as long as I declare a wmain function and call the NetShareCheck with the console parameters (argv[x]).
I would like to use this function within a normal main function and without using the console parameters. I've tried using the MultiByteToWideChar() function for conversion of normal char's to UNICODE. unfortunatly it doesn't work..
///////////////////////////////
LPCSTR szServer = "192.168.2.152";
LPWSTR lpWideCharStr;
MultiByteToWideChar(CP_ACP, 0, szServer, sizeof(szServer), lpWideCharStr, sizeof(lpWideCharStr));
res = NetShareCheck(lpWideCharStr, argv[2],&devType);
//////////////////////////////
any one suggestions or some sample code?
Thanks a lot!
Smasher/Devourer
July 14th, 2005, 02:05 PM
There are several problems with the way you're calling MultiByteToWideChar().
1. You cannot use sizeof(szServer) to get the length of the string. Because of the way you've declared szServer -- as a pointer to a constant string literal -- sizeof() will return the size of the pointer itself, which presumably is 4 bytes. Your next thought might be to use strlen(szServer), but in my testing I found that the function seems to want the terminating null included in the character count, so you'd have to use strlen(szServer) + 1. An easier approach would be to use -1 for this parameter, which tells MultiByteToWideChar() that it should assume your string is null-terminated and figure out the length itself.
2. You have declared a pointer called lpWideCharStr without allocating any memory to it. Thus, trying to write to that pointer will almost certainly produce some kind of access violation. Declare an array of wchar_t instead.
3. If you declare an array of wchar_t, then sizeof(lpWideCharStr) will give the length of the buffer in bytes. But that's not what you want -- you need the length of the buffer in characters. So you need to divide sizeof(lpWideCharStr) by sizeof(wchar_t).
Here's an example program that shows a conversion such as the one you're attempting and checks to see whether it was successful.
#include <windows.h>
int main()
{
// original server address
char szServer[] = "192.168.2.152";
// this buffer holds the expected result so we can check whether the conversion works
wchar_t wszNewServer1[] = L"192.168.2.152";
// this buffer will hold the result of MultiByteToWideChar()
wchar_t wszNewServer2[80];
// convert szServer to wszNewServer2
MultiByteToWideChar(CP_ACP, 0, szServer, -1, wszNewServer2, sizeof(wszNewServer2) / sizeof(wchar_t));
// compare wszNewServer1 to wszNewServer2 to see if we got the expected results
if (wcscmp(wszNewServer1, wszNewServer2) == 0)
MessageBox(NULL, "Strings are equal!", "Results", MB_OK);
else
MessageBox(NULL, "Strings are not equal.", "Results", MB_OK);
return 0;
}
mocrotronics
July 14th, 2005, 03:09 PM
Thanks a lot Smasher/Devourer!! Conversion seems to be working, but i'm still having problems calling the NetShareCheck function using these wide char formatted strings.
Any suggestion on that?
thanks in advance!
NoHero
July 14th, 2005, 03:21 PM
Thanks a lot Smasher/Devourer!! Conversion seems to be working, but i'm still having problems calling the NetShareCheck function using these wide char formatted strings.
Any suggestion on that?
thanks in advance!
You mean this code?
res = NetShareCheck(lpWideCharStr, argv[2],&devType);
Well... Where do you define and/or fillup devType? Is argv[2] a wide character string or not? Notice argv[2] also has to be a wchar_t* and not char*. Use wmain to archeive this:
#define _UNICODE
#define UNICODE
int wmain ( int argc, wchar_t* argv[] )
{
// use argv
}
Smasher/Devourer
July 14th, 2005, 03:22 PM
Yes, if you're using main() rather than wmain(), then argv[2] must be converted to a Unicode string prior to passing it to NetShareCheck(). You can do the conversion on that string the same way you converted szServer to lpWideCharStr.
mocrotronics
July 14th, 2005, 03:43 PM
int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
NET_API_STATUS res;
DWORD devType = 0;
// this buffer holds the expected result so we can check whether the conversion works
wchar_t wszNewServer1[] = L"192.168.2.152";
wchar_t wszDevice[] = L"C:";
res = NetShareCheck(&wszNewServer1,&wszDevice,&devType);
}
I'm using the main function where i've now declared two wide chars. I've tried several methods to use these as functionparameters for NetShareCHeck. I can't get it working.
NoHero
July 14th, 2005, 03:52 PM
Check this FAQ (http://www.codeguru.com/forum/showthread.php?t=318721) to see what's going wrong
NetShareCheck (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/stgmgmt/fs/netsharecheck.asp) on MSDN, with an example.
mocrotronics
July 14th, 2005, 04:18 PM
Check this FAQ (http://www.codeguru.com/forum/showthread.php?t=318721) to see what's going wrong
NetShareCheck (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/stgmgmt/fs/netsharecheck.asp) on MSDN, with an example.
I've had it working in the way it's described on the msdn (within a wmain function using argv[]). Instead, I would like to use this function within a main function with declared w_char vars (not using the main arguments) or converted conventional strings using the mentioned function MultiByteToWideChar.
The question is how to get this working.
NoHero
July 15th, 2005, 04:30 AM
I've had it working in the way it's described on the msdn (within a wmain function using argv[]). Instead, I would like to use this function within a main function with declared w_char vars (not using the main arguments) or converted conventional strings using the mentioned function MultiByteToWideChar.
The question is how to get this working.
Then check the first FAQ to retrieve extended error information. Or use the \\ donation instead of IP address:
wchar_t szAddr[] = "\\\\.\\";
mocrotronics
July 15th, 2005, 09:09 AM
I don't think i'm passing the arguments in the right format within the main function. Even after the conversion to wide chars. Well, i think i just declare a wmain function, cause this seems to be working fine.
Thanks a lot guys!!
NoHero
July 15th, 2005, 09:14 AM
I don't think i'm passing the arguments in the right format within the main function. Even after the conversion to wide chars. Well, i think i just declare a wmain function, cause this seems to be working fine.
Thanks a lot guys!!
Yes and define _UNICODE & UNICODE before including any headers. And start using tchar functionality. This helps you to quickly change between ANSI & Unicode.
:wave:
inbugable
July 26th, 2007, 10:39 PM
nipple
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.