How to copy string at specified memory location?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chintan Shah
    New Member
    • Jan 2011
    • 6

    #1

    How to copy string at specified memory location?

    I have a string and i want to copy at specified memory location.
    Suppose i have a pre-allocated memory represented by

    char* logFile (unsafe code)

    I have a generated string which contains the name of log file.
    I want to copy that string on location represented by char*.

    Please help me on this.
    =============== =============== =============== =============== =

    Also i have to copy specified number of characters from a given location
    to specified location.

    The code in delphi looks something like:

    StrLCopy(msg, PChar(gsRootErr orMsg), MAX_ERR_MESSAGE );
    where msg is character pointer:msg: PChar
    and gsRootErrorMsg is string.


    Help would be really appreciated.
    Thanks.
  • Samuel Jones
    New Member
    • Jan 2011
    • 48

    #2
    I can try to help, but you will need to help me understand this StrLCopy thing.

    This is what i can guess it does.
    StrLCopy(dest, source, number)

    StrLCopy copies the first 'number' of characters from the string specified as 'source', to the 'dest' string

    Am i right? If so, i can help, otherwise a little more explanation please.

    Comment

    • Chintan Shah
      New Member
      • Jan 2011
      • 6

      #3
      No

      Destination and source are memory locations.
      So StrLCopy will copy the max number of characters from the memory address of source to memory address of destination.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I did some digging on this... I'm not sure how to get the memory address of a string in C# since it seems you can't get the address of a managed type.

        That said, you can use a char array... Have a look at the following:

        Code:
                    string str = "blah";
                    char[] b = new char[str.Length];
                    str.CopyTo(0, b, 0, str.Length);
        
                    foreach (char c in b)
                        Console.Write(c);
                    Console.WriteLine();
        
                    unsafe
                    {
                        fixed (char* p = b)
                        {
                            Console.WriteLine((int)p);
                            *p = 'f';
                        }
                    }
        
                    foreach (char c in b)
                        Console.Write(c);
                    Console.WriteLine();
        Have a read here: http://www.codeproject.com/KB/cs/csunsafeintro01.aspx

        Also, here's some discussion on a C# equivalent of memset: http://stackoverflow.com/questions/1...of-memset-in-c

        Comment

        Working...