If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

problem with arrays in WinApi

Hello.
I'm new to winapi but I've read a book about it already. The problem is that my Visual Studio 2012 considers some of the stuff mentioned in my book as an error. For example:

Code:

Array^ arr = Array::CreateInstance(System::Int32::typeid, 10);

doesnt work.It asks me to put a semicolon between 'Array^' and 'arr',which also results in an error ofc.Also it says stuff like "Array must be a class or a namespace when followed by '::' ",CreateInstance is not a member of global namespace" and a ton of other issues.What's more:

Code:

array<int>^ arr = { 0, 1, 2 };

doesn't work as well. it says that " array is not a template ".Furthermore it seems to be completly unfamiliar with the gcnew operator:"error operator gcnew is unidentified".It makes me tired already

Additional question:
I want to make an array of objects of my own class.How can i make a definition of such an array without initializing it's components?

Re: problem with arrays in WinApi

... or, in case you actually do want to go the C++/CLI route, you'd need to create one of the CLR project types (preferably CLR Console Application or Windows Forms Application) instead of a Win32 one.

Also...

Originally Posted by Fides Facit Fortis

Code:

Array^ arr = Array::CreateInstance(System::Int32::typeid, 10);

What kind of book are you trying to learn C++/CLI from? In case you'd get that to compile, it actually would create an array of ten integers, but that would then be extremely inconvenient to use. The usual, and practically much more useful way to create such an array is this:

Code:

array<int> ^arr = gcnew array<int>(10);

Additional question:
I want to make an array of objects of my own class.How can i make a definition of such an array without initializing it's components?

You can simply declare the array without actually creating it:

Code:

array<YourClass ^> ^arr;

(The first one of the two ^s would or would not be required, depending on whether your class is a reference class or a value class.)

However, in case your class is a reference class, you could as well create the array using gcnew right away. Its items would then be initialized to nullptr, thereby not refering to any concrete instance of your class. You can later fill them with handles refering to instances of your class.

Re: problem with arrays in WinApi

It does work. ... provided, however, that it's in a module compiled with CLR support (which usually means it's part of one of the CLR type of projects) and YourClass is a managed class (denoted by either of the C++/CLI-specific keywords ref or value preceeding the class in the declaration), since a tracking handle (denoted by the ^) can't refer to a native object [EDIT: ... and it's at function or class member scope, see below].

Assuming this is at global or namespace scope, they don't work for two reasons: BarButton is not a managed class (see above) and global (as well as non-class-member static) variables of managed types (in this case the managed array) are not allowed (error C3145). The first one of the two array declarations also doesn't work because the managed array is a reference type, and that requires declaring variables of that type as tracking handles. The ^ I declared to be optional for a potential value type would be the one after BarButton in this case, not the one before bar.

Also, if you want a bar with buttons on it in a C++/CLI application, why not simply utilize Windows Forms and the ToolStrip and ToolStripButton classes? Writing something even remotely similar would be a vast lot of work you probably can't even imagine, and most certainly wouldn't want to take.

Last edited by Eri523; March 4th, 2014 at 07:24 PM.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: problem with arrays in WinApi

Also, if you want a bar with buttons on it in a C++/CLI application, why not simply utilize Windows Forms and the ToolStrip and ToolStripButton classes?

You see, the problem is that the bar I'm trying to implement needs to have custom properties.I want it to have similiar functionality to overlap buttons in browsers. Each one is responsible for one site.
But I also want them to close when I right-click them.All the rest has to rearange then so there are no empty spaces between them.I also want to be able to create a new overlap button each time I press a '+' button, like in Firefox, for example. Also I want to limit their number by up to ten. I know it's going to be hard but I've got some experience in programming and scripting, just not so much in WinApi . I'll try hard until I'll achieve desired effect.

It does work. ... provided, however, that it's in a module compiled with CLR support (which usually means it's part of one of the CLR type of projects) and YourClass is a managed class (denoted by either of the C++/CLI-specific keywords ref or value preceeding the class in the declaration), since a tracking handle (denoted by the ^) can't refer to a native object
...
global (as well as non-class-member static) variables of managed types (in this case the managed array) are not allowed (error C3145)

So, all I have to do is to switch my project type to an empty CLR project,use ref keyword before class and make my array local?Then I can simply use

Re: problem with arrays in WinApi

Originally Posted by VictorN

Then please stop posting to this thread.
And if you will need some help with your new managed C++/CLI project - start a new thread in the Managed C++ and C++/CLI forum.

Or, perhaps, some moderator can move this thread over there. Now it seems quite clear that it's C++/CLI what the OP is bound to. I'd really like to reply to post #7 in the C++/CLI section, but, though not impossible, it is much more tideous when doing it "remotely".

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: problem with arrays in WinApi

Originally Posted by VictorN

ok.

Thanks.

Originally Posted by Fides Facit Fortis

You see, the problem is that the bar I'm trying to implement needs to have custom properties.I want it to have similiar functionality to overlap buttons in browsers. Each one is responsible for one site.

I'm not quite sure what you're meaning by that description. Perhaps something like these?

But I also want them to close when I right-click them.All the rest has to rearange then so there are no empty spaces between them.I also want to be able to create a new overlap button each time I press a '+' button, like in Firefox, for example. Also I want to limit their number by up to ten. I know it's going to be hard but I've got some experience in programming and scripting, just not so much in WinApi . I'll try hard until I'll achieve desired effect.

Except for the auto-arranging, none of what you're describing here is standard behaviour of the Windows Forms tab control. Yet I'd bet it's still way simpler to implement your custom behaviour based on the standard control, compared to implementing everything yourself from scratch.

If for some reason you don't want to use a tab control (e.g. because I simply misunderstood your description above): The tool strip features auto-arranging as well. And if that's still not what you want, you can go low-level and implement auto-arranging on any form you may make look like practically anything you like, using a flow layout panel.

So, all I have to do is to switch my project type to an empty CLR project,use ref keyword before class and make my array local?Then I can simply use

Code:

array <BarButton> ^bar;

?
I'll try it out when I'll have some time because I'm busy today.

Basically yes, except:

I wouldn't really recommend the empty project type, rather a Windows Forms project.

You'd most probably want the array holding your buttons to persist longer than the runtime of a single function, so it likely would better be a class member than a local variable.

GUI elements typically are reference objects, so the array declaration would rather need to be like this:

Code:

array<BarButton ^> ^bar;

And if you'd use any of the standard infrastructure I suggested above, you woudn't need that array any more anyway, since all the GUI items I suggested feature their own control collection for that purpose.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.