locked
string.h - strange new ways of declaring strings in 2008 Studio C++ forms

    Question

  • I am an old fashioned "C" programmer. I am having trouble with understanding how to do simple string manipulations with the new tools in Studio 2008 C++ forms.

     

    When I use Studio 2005 C++, I can use the following declaration:

     

    String *something;

     

    When I use Studio 2008 C++, The above declaration errors out.

     

    The following does work for some strange reason in Studio 2008 C++:

     

    String ^something;

     

    What does the "^" mean now?? It used to be a bitwise or????????

     

    I also can not seem to use my old <string.h> functions anymore, like strcmp, strcat, strncmp etc. etc.

     

    How do I get these old functions to work using the String type shown above, or, is there a much better way that I am missing the boat on???

     

     

    Thanks,

     

    Gordon

     

     

    Wednesday, October 01, 2008 1:50 PM

Answers

  • Hans,

     

    I got it to work by placing the variable declarations in form1.h up close to the top of my auto generated code, just before the initialization function. It was that simple.

     

    Breath In, Breath Out, Move on!

     

    Gordon

     

    Friday, October 03, 2008 8:22 PM

All replies

  • You are writing managed code in the C++/CLI syntax.  It is an entirely different language, designed to produce code that runs on the .NET framework.  The "hat" means "reference", somewhat similar to an unmanaged pointer.

    The old way of writing C/C++ code, including <string.h>, is still very much supported.  Just start off your project with the right template in the Win32 node, not the CLR node.
    Wednesday, October 01, 2008 3:44 PM
  • Hans,

     

    You are back again. Thanks.......

     

    I use forms, so don't I have to use CLR ???????? If I use CLR, is there any way to use string.h ??????

     

    Thanks,

     

    Gordon

     

    Wednesday, October 01, 2008 5:56 PM
  • Forms require C++/CLI.  Although you technically could use string.h, you shouldn't.  You'll hit the wall over and over again, converting between managed and unmanaged strings.  Understanding the "hat" is pretty essential if you want to make Windows Forms work for you.  I'd recommend either C# (no hats) or a book.
    Wednesday, October 01, 2008 6:24 PM

  • Quote>I also can not seem to use my old <string.h> functions anymore,
    Quote>like strcmp, strcat, strncmp etc. etc.

     

    Be specific. What *exactly* happens when you try?

     

    Quote>How do I get these old functions to work using the String type shown above

     

    Spend some time with the compiler help. For starters, look for the topic
    "How to: Convert Between Various String Types"

     

    Quote>What does the "^" mean now??

     

    It's part of the C++/CLI language. From the spec:

     

    handle - A handle is called an "object reference" in the CLI specification.
    For any CLI class type T, the declaration T^ h declares a handle h to type T,
    where the object to which h is capable of pointing resides on the CLI heap.
    A handle tracks, is rebindable, and can point to a whole object only.
    (See also type, reference, tracking.)

     

    type, value class, boxed - A boxed value class is an instance of a value class
    on the CLI heap. For a value class V, a boxed value class is always of the form V^.

     

    unboxing - An explicit conversion from type System:: Object^ to any value class type,
    from type System::ValueType^ to any value class type, from V^ (the boxed form of a value
    class type) to V (the value class type), or from any interface class type handle to any value
    class type that implements that interface class. (See also "boxing".)

     

    - Wayne

    Wednesday, October 01, 2008 8:08 PM
  • This line of code produces the build message below:

     

    if (strcmp(strAnswer1,"test")!=0)

     

    Here is the build output messages:

     

    1>------ Build started: Project: gordon, Configuration: Debug Win32 ------

    1>Compiling...

    1>gordon.cpp

    1>c:\users\t71-2107\gordon\gordon\Form1.h(462) : error C3861: 'strcmp': identifier not found

    1>Build log was saved at "file://c:\Users\t71-2107\gordon\gordon\Debug\BuildLog.htm"

    1>gordon - 1 error(s), 0 warning(s)

     

     

    How do I get string.h or whatever it is now called included so my code can use theses functions with my forms project??

     

    I tried:

     

     #include <string>

     

     

    This include did not help in the main.cpp program.

     

     

    Thanks,

     

    Gordon

    Wednesday, October 01, 2008 8:52 PM
  • I hesitate to tell you because you might actually use it, and that would be a mistake.  The C++/CLI version of that statement would be:

      String^ strAnswer;
      ...
      strAnswer = textBox1->Text;  // For example
      if (strAnswer != "test")

    But you probably won't rest until you've got your strcmp() back.  The best place to put the #include <string.h> directive is in your stdafx.h file.
    Wednesday, October 01, 2008 9:10 PM

  • The error message says that strcmp is used at line 462 of form1.h but you say
    "This include did not help in the main.cpp program".

     

    The include statement has to be in the same source code module as the
    strcmp call.

     

    If you had the include in form1.h and the strcmp in main.cpp after an include
    of form1.h that would be OK. But you appear to be trying the reverse.

     

    - Wayne

    Wednesday, October 01, 2008 9:13 PM
  • Hans,

     

    I appreciate your guidance!! If you knew how freaking old I was, and where my roots are in technology, it would probably scare you to death! I was born in 1949 and have worked as a design engineer most of my life. I design digital video/audio recorders and GPS trackers and do a lot of "C",assembly programming at the embedded level. Every now and then I do some Windows stuff, but rarely.

     

    Currently, I have some embedded devices that need to talk through old fashioned RS-232C ports on a Windows PC. The nasty part of this is that they interface to WIFI and wireless radios that use RTS/CTS handshaking. The existing design this company uses, that was written by somebody other than myself, did not use RTS/CTS properly(at all), and packets can be dropped on a random basis. My task is to implement RTS/CTS hardware handshaking so that the communications becomes much more robust.

     

    With all this said, I need to be able to grab characters, one by one, and parse them on the fly, decoding commands and responses that are very custom and proprietery of this design. I can't wait for delimiters or EOL's that may never occur. Obviously, I need to be able to properly respond to RTS/CTS on both ends of the RS-232C chain, without getting completely hung up in a Mexican stand off under adverse signal conditions.

     

    Now that you understand my task, I would like to say that I truly appreciate your assistance, and will return the favor some day, any way I can................

     

    I am not adverse to using new and proper disciplines of C++ and CLI. I only can relate to what I have used for years in pure "C". I looked at the serialPort class methods list and have been a little puzzled on how to absorb a single character/byte into a C+++ String and still be able to look at the character on a byte by byte comparrison basis. You have to realize that I still have the "array of char" mentality from "C". When I use "readExisting". I seem to get a single character stuffed in a String, but have not managed to get the String::Compare to fire correctly. That of course makes me tend to want to use my old string.h functions that I understand from days of old. I really have no problems writing this app in pure "C", but I can't make the pretty GUI in "C" at my poor C++ skill level. That is why forms and CLI looks so appealing right now. I can make the GUI look top notch, but I just have to master the simple tasks that I was so handy with in regular old "C".

     

    Your thoughts???????

     

    Thanks,

     

    R. Gordon price

    VP of R&D

    Keytroller, LLC.

    Tampa Florida

    (813) 877-4500 ext 226

    http://keytroller.com

     

    Thursday, October 02, 2008 1:33 PM
  • HANS,

     

    I have attached a piece of code that clearly has a problem in that I am declaring variables inside an event that gets recalled with every charcter event. The end result is that the variables ^strAnswer1 and ^strByte are probably re-initialized and cleared with each event. Here is a case where I need to know where I can globally and/or statically define these variables so that the string concatenation sticks and works.

     

     

    private: System::Void serialPort1_DataReceived(System:Surprisebject^ sender, System::IO:Stick out tongueorts:Tongue TiederialDataReceivedEventArgs^ e)

    {

    String ^strAnswer1, ^strByte; // this is defined in the wrong place!!!! Where should it go????

     

    strByte = serialPort1->ReadExisting();

    listBox4->Items->Add(strByte);

    strAnswer1 = strAnswer1 + strByte;

    listBox4->Items->Add(strAnswer1);

     

    if (strAnswer1 != "test")

    {

    listBox4->Items->Add("NOT TEST");

    }

     

    if (strAnswer1 == "test")

    {

    listBox4->Items->Add("TEST");

    }

    }

     

    Thursday, October 02, 2008 2:17 PM
  • Move it out of the function definition so it becomes a member of the class.
    Thursday, October 02, 2008 2:46 PM
  • Hans,

     

    Where is this class code located??? Form1.h????? Remember, I am lost here..

     

    Gordon

     

    Thursday, October 02, 2008 2:55 PM
  • There's a limit to what is practical to explain in a forum post.  We passed it.  I suspect you haven't had a lot of experience working with C++ classes.  There are many excellent books and training courses to help you with that, I strongly recommend you consider them.
    Thursday, October 02, 2008 4:05 PM
  • Hans,

     

    I got it to work by placing the variable declarations in form1.h up close to the top of my auto generated code, just before the initialization function. It was that simple.

     

    Breath In, Breath Out, Move on!

     

    Gordon

     

    Friday, October 03, 2008 8:22 PM