Hi to all ,just a small question.
When i read in the book for Win32 API tur. in c++,
this: "
cbSize
The size of the structure. "
I asked my self ,what sturcture?
I tought we useing class.

Dani AI

Generated

Good points from and ; here are focused, practical notes that go beyond the basic example.

Think of cbSize as a version/length tag that tells the API how many bytes of the structure were supplied. That allows the OS to evolve structures safely: if cbSize is smaller, the API knows not to touch newer fields. Because of that role, exact byte count and binary layout matter more than the source-language keyword (struct vs class).

Common pitfalls and best practices: always initialize the structure memory (zero it) before use, and make sure cbSize matches the exact Windows structure type declared in the headers — not the size of any C++ wrapper or derived type. Do not change packing for Windows headers, and be mindful of 32-bit vs 64-bit differences (pointer sizes affect sizeof). When writing C++ wrappers, keep the native C struct as a plain member and populate its cbSize from inside the wrapper rather than exposing a wrapped object directly to Win32 calls.

Troubleshooting tips: if a Win32 call fails or silently ignores fields, check GetLastError() for ERROR_INVALID_PARAMETER and verify sizeof of the exact header-declared struct against the docs. Some Win32 structs (for example, NOTIFYICONDATA or WNDCLASSEX) intentionally changed size across OS versions; their cbSize is how the API detects which version the caller expects.

A few clarifications tied to the thread: gave the usage pattern; ’s reminder about language choice is apt — Win32 expects C-style layouts. In C++ a plain data-only struct and class with identical data members have the same layout, but adding constructors, virtual functions or inheritance can change it and break binary compatibility with the API.

Recommended Answers

All 7 Replies

After pack of moments,my most logical conclude.
The class is extended(embodiment) structure ,so the writer is knew about that and He have a free to write this.
Am i right?

cbSize is a member of a structure that you set to the structure size before calling win32 api functions with it. For example:

struct foo
{
    unsigned int cbSize;
    // blabla
};

struct foo MyStruct;
MyStruct.cbSize = sizeof(struct foo);

>>But why they put a option with struct when they have a class?Where is advance?

win32 api is mostly written in C, not C++, so that they can be called by almost any computer language. The same functions are called by VB, assembly, C#, pascal, etc. This would not be possible if Microsoft had written the api functions in C++.

>But why they put a option with struct when they have a class?
Why not? How do you know a class was even an option?

Oh yes ,i mixed with a c++ clas.
right?

>>i mixed with a c++ clas. right?
Yup.

Thank you again for reslove my confuseing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.