13 Replies - 624 Views - Last Post: 27 August 2012 - 04:00 PM Rate Topic: -----

#1 dearranged  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 12

Need help working with WIN32 datatype

Posted 26 August 2012 - 03:24 AM

Hi everyone, new here and new to C++ programming in general. I am trying to write a program but I need to use a WIN32 function which expects a LPWSTR as a parameter. Until now I have only worked with char arrays and the STL string class.

I have a "string literal" and a variable which also holds a string. To try and make things easier they are both LPWSTR. I want to stick them together, something like:
LPWSTR something = L"string literal" + variable;

I can't figure out how to do it, I have been looking for solutions for ages but so far my search has proved fruitless.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help working with WIN32 datatype

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,106
  • Joined: 05-May 12

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 03:37 AM

Try using std::wstring instead of std::string. This will let you append strings together like the std::string. And then you can get an LPWSTR by using the std::wstring.cstr().
Was This Post Helpful? 1
  • +
  • -

#3 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 03:40 AM

Unfortunately, working with Microsoft's string pointer types (such as LPCWSTR) can be a bit daunting for new programmers.

Your best bet in this case, if you absolutely must use that Win32 function, is to treat your strings as an STL string and convert them into an LPCWSTR with the following function when you need to call the Win32 function.

LPCWSTR strToWStr(const std::string& string)
{
	std::wstring result(string.length(), L'');
	std::copy(string.begin(), string.end(), result.begin());
	return result.c_str();
}

This post has been edited by Mina-no-Hime: 26 August 2012 - 03:43 AM

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,106
  • Joined: 05-May 12

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 03:53 AM

Ummm. Won't the result string that within the function get destroyed as it leaves the scope? So you'll be returning a pointer to a string that has been freed.

Additionally, the std::copy() operation won't do the correct multibyte to Unicode conversion if the std::string contains a multibyte string rather than a vanilla ASCII string.

This post has been edited by Skydiver: 26 August 2012 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#5 dearranged  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 12

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 04:02 AM

Thank you both for your replies.

View PostSkydiver, on 26 August 2012 - 06:37 PM, said:

Try using std::wstring instead of std::string. This will let you append strings together like the std::string. And then you can get an LPWSTR by using the std::wstring.cstr().


I tried with std::wstring before, but I will have another go at it with the .cstr() and I'll let you know how it goes.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,106
  • Joined: 05-May 12

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 04:07 AM

Sorry... c_str(), like what Mina-no-Hime was showing in the code snippet.
Was This Post Helpful? 0
  • +
  • -

#7 dearranged  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 12

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 07:42 AM

I tried the std::wstring way, and used Mina-no-Hime's code but it doesn't seem to work with LPWSTR. I tried to use LPCWSTR but that type is not accepted for the parameter. I get complaints about the two type being incompatible.

View PostMina-no-Hime, on 26 August 2012 - 06:40 PM, said:

Unfortunately, working with Microsoft's string pointer types (such as LPCWSTR) can be a bit daunting for new programmers.

Yeah, it is. I've been reading some books and doing some exercises trying to learn, thinking I am doing quite well until I try to do something "real".
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 09:19 AM

Here's one way to do it. Casted back to unsigned char for print-ability with standard out stream.

MultiByteToWideChar reference page

#include <Windows.h>
#include <iostream>
#include <string>
using namespace std;

//courtesy of msdn forums
std::wstring s2ws(const std::string& s)
{
 int len;
 int slength = (int)s.length() + 1;
 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
 wchar_t* buf = new wchar_t[len];
 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
 std::wstring r(buf);
 delete[] buf;
 return r;
}

int main(){
	string temp("Huzzah!");
	wstring result = s2ws(temp);	
	for(int i = 0; i< result.length(); i++) cout << (unsigned char)result[i];
	cout << endl;
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 773
  • View blog
  • Posts: 2,287
  • Joined: 20-March 10

Re: Need help working with WIN32 datatype

Posted 26 August 2012 - 10:23 AM

View Postdearranged, on 26 August 2012 - 02:42 PM, said:

I tried the std::wstring way, and used Mina-no-Hime's code but it doesn't seem to work with LPWSTR. I tried to use LPCWSTR but that type is not accepted for the parameter. I get complaints about the two type being incompatible.

View PostMina-no-Hime, on 26 August 2012 - 06:40 PM, said:

Unfortunately, working with Microsoft's string pointer types (such as LPCWSTR) can be a bit daunting for new programmers.

Yeah, it is. I've been reading some books and doing some exercises trying to learn, thinking I am doing quite well until I try to do something "real".



I am not 100% sure but I think you mean how to add a variable to a unicode widestring
something like
int variable = 24;
string mystring = "The result is ";

then add variable and mystring together

if so there are a few ways here is one way

#include <iostream>
#include <Windows.h>
#include <sstream>

int main()

{
	int variable = 12;
	std::wstringstream sss;
	wchar_t *mainbody = L"string literal + ";
	sss<< mainbody;
	sss<< variable;
	wchar_t something[100] = L"";
	wcscat(something, sss.str().c_str());
    LPWSTR result = something;
	std::wcout << result << std::endl;
	std::cin.ignore();
	std::cin.get();
	return 0;
}


Was This Post Helpful? 2
  • +
  • -

#10 dearranged  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 12

Re: Need help working with WIN32 datatype

Posted 27 August 2012 - 01:33 PM

Thank you everyone for all of your help. It works now. I don't suppose anyone could recommend a book focusing on getting started with the Windows API.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,106
  • Joined: 05-May 12

Re: Need help working with WIN32 datatype

Posted 27 August 2012 - 02:49 PM

Programming Windows (5th Edition) by Charles Petzold. http://www.amazon.co...t/dp/157231995X

Don't let the old publication date fool you. It's withstood the test of time and is the key reference used by everybody else who has written other Windows programming books. (Unfortunately, the 6th edition that is coming out in November will be in C# and focused Windows 8 rather than the Win32 API.)
Was This Post Helpful? 1
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,106
  • Joined: 05-May 12

Re: Need help working with WIN32 datatype

Posted 27 August 2012 - 02:58 PM

If you want something more modern, Jeffrey Richter's Windows via C/C++: http://www.amazon.co...r/dp/0735663777

I've not read this particular book, but having read his other books and highly recommend them. Glancing at the ToC, it looks like you'll have a very solid understanding of how the Win32 APIs work (and why they work that way), and be able to use them effectively to write unmanaged Windows programs.

He covers Vista and the new sets of APIs introduced then than persist through Windows 7 and 8. He also covers running as a normal user instead of the old Windows assumption of always running as administrator.
Was This Post Helpful? 1
  • +
  • -

#13 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 773
  • View blog
  • Posts: 2,287
  • Joined: 20-March 10

Re: Need help working with WIN32 datatype

Posted 27 August 2012 - 02:59 PM

View Postdearranged, on 27 August 2012 - 08:33 PM, said:

Thank you everyone for all of your help. It works now. I don't suppose anyone could recommend a book focusing on getting started with the Windows API.



Yes I can,

Petzold's Programming Windows

It's like the book on win32...

It shows you nearly everything apart from networking..

It kinda skims that right at the end...

but there is hope in the shape of..

Making Unix and Windows NT Talk

It covers both Unix and the Windows enviroments.

Regards

Snoopy.

edit

Arggh Ninja'd by Skydiver...

This post has been edited by snoopy11: 27 August 2012 - 03:00 PM

Was This Post Helpful? 2
  • +
  • -

#14 dearranged  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 26-August 12

Re: Need help working with WIN32 datatype

Posted 27 August 2012 - 04:00 PM

Thanks again guys. I'll look into those books, hope to give them a good read soon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1