I don't understand Win32 datatypes

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1347 Views - Last Post: 17 July 2012 - 12:14 AM Rate Topic: ***** 1 Votes

#1 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

I don't understand Win32 datatypes

Posted 16 July 2012 - 08:46 PM

I am creating a console application using the win32 API. i am having a lot of trouble getting anything to compile. what is LPSTR? how do i use it. what datatype's SHOULD i be using? the only reason I'm creating a console app is because to create a G.U.I. app i MUST use Microsoft's stupid datatype's which i obviously don't understand. here is what i have so far.

main.cpp
//Headers
#include <Windows.h>
#include <iostream>
#include <tchar.h>
#include "Classes.h"
//NameSpaces
using namespace std;
//Functions
int main()
{
	Class ComputerProgramming(L"Computer Programming");
	cout<<ComputerProgramming.getLevel();
	return 0;
}


[size=5]Classes.h[size]
#ifndef Classes_h
#define Classes_h
enum Subject
{
	Math,
	Science,
	History,
	English,
	Latin,
	Fench, 
	ComputerScience
};


class Class
{
	public:
		Class();
		Class(LPSTR &);
		void setName(LPSTR &);
		void setSubject(Subject);
		
		LPCSTR getName();
		float getExperience();
		unsigned int getLevel();
		Subject getSubject();
	
	private:
		LPCSTR name;
		int level;
		float experience;
		Subject subject;
};

Class::Class()
{
	name = "";
	level = 0;
	experience = 0.0;
}

Class::Class(LPSTR &incName)
{
	name = incName;
	level = 0;
	experience = 0.0;
}

void setName(LPSTR &incName)
{
	name = *incName;
}

void getName()
{
	return name;
}

void setSubject(Subject incSubject)
{
	subject = incSubject;
}

float getExperience()
{
	return experience;
}

unsigned int getLevel()
{
	return level;
}

Subject getSubject()
{
	return subject;
}

#endif //Classes_h


Is This A Good Question/Topic? 0
  • +

Replies To: I don't understand Win32 datatypes

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 08:50 PM

You can always grep the header files to see the definitions.

But to answer your question, a LPSTR is a basically char *. An LPCSTR is a const char *.

The problem is that on your line 11:
Class ComputerProgramming(L"Computer Programming");



L"Computer Programming" is a const wchar_t * or otherwise known as a LPCWSTR.

Why do you feel that you are compelled to use the Microsoft Win32 data types. You seem to only be using cout in your code which is in the C++ standard and not Microsoft specific.

This post has been edited by Skydiver: 16 July 2012 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 08:58 PM

does this mean i can use char* instead of LPSTR?

and if i can use cahr* instead of LPSTR, how do i use char* as a return type?

View PostSkydiver, on 16 July 2012 - 08:50 PM, said:

Why do you feel that you are compelled to use the Microsoft Win32 data types. You seem to only be using cout in your code which is in the C++ standard and not Microsoft specific.

i have to use win32 datatypes because the win32 api requires it. i can't use std datatypes like string. and COUT is a part of the std lib is it not?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:04 PM

And a little background on the types...
L = Hungarian notation for long
P = Hungarian notation for pointer
STR = C style null terminated string (as opposed to a Pascal string)

So you get LPSTR. The long used to be significant because Windows used to be 16 bit, and so the default pointer in the compiler was only 16 bit, but there was a need to ensure that all pointers were 32 bit, and so the long modifier.

Nowadays, there is no need for the L. You can technically use PSTR, PCSTR, PWSTR, and PCWSTR if the 'L' messes with your sensibilities.

You can probably figure out that the C stood for "const". The WSTR stands for "Wide character string".
Was This Post Helpful? 0
  • +
  • -

#5 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:08 PM

i really appreciate your help. but what REALLY pisses me off is that microsoft requires us to use their datayptes! what. the. ~?!W?@?!?f

sorry. I'm just tired of not understanding this crap. ive never struggled this much with anything else in programming ever.

This post has been edited by GunnerInc: 16 July 2012 - 09:12 PM
Reason for edit:: Removed curse words

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:08 PM

View PostxXTyMeador73Xx, on 16 July 2012 - 08:58 PM, said:

does this mean i can use char* instead of LPSTR?

and if i can use cahr* instead of LPSTR, how do i use char* as a return type?

View PostSkydiver, on 16 July 2012 - 08:50 PM, said:

Why do you feel that you are compelled to use the Microsoft Win32 data types. You seem to only be using cout in your code which is in the C++ standard and not Microsoft specific.

i have to use win32 datatypes because the win32 api requires it. i can't use std datatypes like string. and COUT is a part of the std lib is it not?


Yes, you can use char* instead of LPSTR. You can use wchar_t* instead of LPWSTR.

You can return a char * as a return type by declaring it just like any other type:
char * GetPointerToFirstMatchingChar(char * sz, char ch)
{
    while(*sz)
    {
        if (*sz == ch)
            return sz;
        sz++;
    }
    return nullptr;
}


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:15 PM

The Windows API evolved before there was the C++ standard. Since MS's goal was to have some kind standard API for companies and individuals outside Microsoft to have some standard way to use the Windows APIs it had to forge ahead and define it's own types.

If you were going to use GIMP or MPIR, would you begrudge using their datatypes (mpz_t) to use their API's?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:24 PM

Quote

sorry. I'm just tired of not understanding this crap. ive never struggled this much with anything else in programming ever.

I feel your pain. I get a headache any time I see a Windows API program. This crap was supposed to make using the Windows API the same no matter what version of Windows you were using from Win16 to Win32. For example a DWORD is guaranteed to be a 32 bit unsigned integer. You may want to book mark this page if you will be dealing with the Windows API: MSDN Common Data Types This is a list of most of the data types used by the Windows API.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:28 PM

View PostSkydiver, on 16 July 2012 - 09:15 PM, said:

The Windows API evolved before there was the C++ standard. Since MS's goal was to have some kind standard API for companies and individuals outside Microsoft to have some standard way to use the Windows APIs it had to forge ahead and define it's own types.

If you were going to use GIMP or MPIR, would you begrudge using their datatypes (mpz_t) to use their API's?

YES, i would. and jim, how do you write GUI applications without the windows api?
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4067
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:31 PM

Easy, I don't do Windows!

But there are Frameworks that encapsulate the low level Windows API, such as wxWidgets, QT, and several others.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:31 PM

You could use some other graphics framework that hides the Win32 API.
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

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

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:36 PM

Use a GUI Library,

Like Qt... which in my opinion is the best one...

However it is limiting in that you can't do everything that

perhaps you would like to do...

Best Wishes

Snoopy.

Edit Ahh shucks ninja'd !!

This post has been edited by snoopy11: 16 July 2012 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#13 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:42 PM

View Postjimblumberg, on 16 July 2012 - 09:31 PM, said:

Easy, I don't do Windows!

But there are Frameworks that encapsulate the low level Windows API, such as wxWidgets, QT, and several others.

Jim

my goal in learning the windows API was to learn the low level aspects. this was also my goal in learning C++. after learning the windows API, then direct x, i plan to learn assembly. My goal in every program i write is efficiency. so using wxWidgets or QT would defeat the purpose of me writing GUI applications period! thanks for the input though! now, as for my next issue, why am i getting the compile time error(s)

#ifndef Classes_h
#define Classes_h
enum Subject
{
	Math,
	Science,
	History,
	English,
	Latin,
	Fench, 
	ComputerScience
};


class Class
{
	public:
		Class();
		Class(char*);
		~Class();
		void setName(char*);
		void setSubject(Subject);
		
		char* getName();
		float getExperience();
		unsigned int getLevel();
		Subject getSubject();
	
	protected:
		char* name;
		int level;
		float experience;
		Subject subject;
};

Class::Class()
{
	name = "";
	level = 0;
	experience = 0.0;
}

Class::Class(char* incName)
{
	name = incName;
	level = 0;
	experience = 0.0;
}

void setName(char* incName)
{
	name = incName;
}

char* getName()
{
	return name;
}

void setSubject(Subject incSubject)
{
	subject = incSubject;
}

float getExperience()
{
	return experience;
}

unsigned int getLevel()
{
	return level;
}

Subject getSubject()
{
	return subject;
}

#endif //Classes_h
[size=5]errors[/size]
1>------ Build started: Project: Classes Class Tester, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\stark\documents\visual studio 2010\projects\classes class tester\classes class tester\classes.h(52): error C2065: 'name' : undeclared identifier
1>c:\users\stark\documents\visual studio 2010\projects\classes class tester\classes class tester\classes.h(57): error C2065: 'name' : undeclared identifier
1>c:\users\stark\documents\visual studio 2010\projects\classes class tester\classes class tester\classes.h(62): error C2065: 'subject' : undeclared identifier
1>c:\users\stark\documents\visual studio 2010\projects\classes class tester\classes class tester\classes.h(67): error C2065: 'experience' : undeclared identifier
1>c:\users\stark\documents\visual studio 2010\projects\classes class tester\classes class tester\classes.h(72): error C2065: 'level' : undeclared identifier
1>c:\users\stark\documents\visual studio 2010\projects\classes class tester\classes class tester\classes.h(77): error C2065: 'subject' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3570
  • View blog
  • Posts: 11,099
  • Joined: 05-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 09:51 PM

You forgot the Class:: scoping qualifier in front of your class function implementations.

As a side note: unless you plan on declaring those functions as inline, you should really move the implementation into a .cpp file.

BTW, the way I deal with Windows data types, is I just use native C++ types in my Model and Controller code. Only when in my View code do I convert to a Windows type if necessary. Often most of the basic C++ types have direct equivalents. I think that my only concession to the Windows data types is that instead of using std::string to store ANSI strings, I always use std::wstring to store UNICODE string.

This post has been edited by Skydiver: 16 July 2012 - 09:57 PM

Was This Post Helpful? 1
  • +
  • -

#15 xXTyMeador73Xx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-May 12

Re: I don't understand Win32 datatypes

Posted 16 July 2012 - 10:11 PM

View PostSkydiver, on 16 July 2012 - 09:51 PM, said:

You forgot the Class:: scoping qualifier in front of your class function implementations.

As a side note: unless you plan on declaring those functions as inline, you should really move the implementation into a .cpp file.

BTW, the way I deal with Windows data types, is I just use native C++ types in my Model and Controller code. Only when in my View code do I convert to a Windows type if necessary. Often most of the basic C++ types have direct equivalents. I think that my only concession to the Windows data types is that instead of using std::string to store ANSI strings, I always use std::wstring to store UNICODE string.

so what your saying is that its better practice to declare the class in a .h file and the actual code for the class into a .cpp file (with the same name as the .h file perhaps?) i have seen this done, but when i first learned OOP C++ i always did it all in one file.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2