problem with arrays in WinApi
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: problem with arrays in WinApi

  1. #1
    Join Date
    Mar 2014
    Posts
    13

    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?

    Thanks in advance; may the Compiler be with You.
    Last edited by Fides Facit Fortis; March 5th, 2014 at 01:06 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: problem with arrays in WinApi

    Quote Originally Posted by Fides Facit Fortis View Post
    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.
    It is an error because it is not ANSI C++ code.

    The code you were attempting is the Microsoft extension of C++ geared toward .NET development. C++/CLI or Managed C++ is what you were using, and WinAPI is solely "traditional" ANSI C/C++.

    So you need to get another book, one geared towards Windows API programming as the one you have now is not geared to Windows API, but to using Managed C++ and the .NET framework.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 3rd, 2014 at 06:03 PM.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,723

    Re: problem with arrays in WinApi

    For Windows API programming I would suggest
    Programming Windows by Charles Petzold
    http://www.amazon.co.uk/Programming-...ywords=petzold

    and for MFC windows programming,
    Programming Windows with MFC by Jeff Prosise
    http://www.amazon.co.uk/Programming-...fc+programming
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    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...

    Quote Originally Posted by Fides Facit Fortis View Post
    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.

    Finally, for discussions about C++/CLI there's a separate forum section: http://forums.codeguru.com/forumdisp...ed-C-and-C-CLI. In case you're actually going to use C++/CLI, please post further questions there.
    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.

  5. #5
    Join Date
    Mar 2014
    Posts
    13

    Re: problem with arrays in WinApi

    Wow,thx for a quick response.
    The code you were attempting is the Microsoft extension of C++ geared toward .NET development. C++/CLI or Managed C++ is what you were using, and WinAPI is solely "traditional" ANSI C/C++.
    Yeah, this is propably due to the fact that my book describes both .NET and WinApi programming so I sometimes get lost.

    For Windows API programming I would suggest
    Programming Windows by Charles Petzold
    http://www.amazon.co.uk/Programming-...ywords=petzold

    and for MFC windows programming,
    Programming Windows with MFC by Jeff Prosise
    http://www.amazon.co.uk/Programming-...fc+programming
    I'll check this out,thanks.

    Code:
    array<YourClass ^> ^arr;
    Nope,doesnt work to me. Syntax error missing ';' before '<'. I'm propably doing sth wrong

    Code:
    class BarButton
    {
    ...
    };
    
    array <BarButton^> bar; //doesn't work
    array <BarButton^>^bar; //doesn't work as well
    Thx for all the great responses, still need help with my objects array,though.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: problem with arrays in WinApi

    Quote Originally Posted by Fides Facit Fortis View Post
    Code:
    array<YourClass ^> ^arr;
    Nope,doesnt work to me. Syntax error missing ';' before '<'. I'm propably doing sth wrong
    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].

    Code:
    class BarButton
    {
    ...
    };
    
    array <BarButton^> bar; //doesn't work
    array <BarButton^>^bar; //doesn't work as well
    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.

  7. #7
    Join Date
    Mar 2014
    Posts
    13

    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
    Code:
    array <BarButton> ^bar;
    ?
    I'll try it out when I'll have some time because I'm busy today.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: problem with arrays in WinApi

    Quote Originally Posted by Fides Facit Fortis View Post
    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.
    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.
    Victor Nijegorodov

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: problem with arrays in WinApi

    Quote Originally Posted by VictorN View Post
    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.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: problem with arrays in WinApi

    Quote Originally Posted by eri523 View Post
    or, perhaps, some moderator can move this thread over there.
    ok.
    Victor Nijegorodov

  11. #11
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    Re: problem with arrays in WinApi

    Quote Originally Posted by VictorN View Post
    ok.
    Thanks.

    Quote Originally Posted by Fides Facit Fortis View Post
    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?

    Name:  Tabs.jpg
Views: 45
Size:  11.5 KB

    The Windows Forms equivalent of this is the standard tab control.

    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.

    Also, regarding terminology, note that we're way apart from Win API now.

    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.

  12. #12
    Join Date
    Mar 2014
    Posts
    13

    Re: problem with arrays in WinApi

    Thx,you are my Guru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center