Start New Discussion within our Software Development Community

I am facing a small problem. Something i might have mis understood.
If u see the code without using a function if i print the message then i am getting proper output. But after i create Error function and print message from main function it is creating some problem.
I am not sure is to why this is the case.

Help would be appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace Test
{
    class Program
    {
        unsafe static void Main(string[] args)
        {
            char* errMsg = (char*)Marshal.AllocHGlobal(255);
            //ShrinkError(errMsg);
            errMsg = (char*)Marshal.StringToHGlobalAnsi("This is just testing ... Again Testing");
            string msg = Marshal.PtrToStringAnsi((IntPtr)errMsg);
            Console.Write(msg);
            Marshal.FreeHGlobal((IntPtr)errMsg);
        }

        unsafe public static void Error(char* msg)
        {
           msg = (char*)Marshal.StringToHGlobalAnsi("This is just testing ... Again Testing\n");
        }
    }
}

If u run my code u will get "This is just testing ... Again Testing" as output.
Now after commenting the line "errMsg = (char*)Marshal.StringToHGlobalAnsi("This is just testing ... Again Testing");" and uncomment "//ShrinkError(errMsg);" the expected output should be same but getting different output.

If u run my code u will get "This is just testing ... Again Testing" as output.
Now after commenting the line "errMsg = (char*)Marshal.StringToHGlobalAnsi("This is just testing ... Again Testing");" and uncomment "//ShrinkError(errMsg);" the expected output should be same but getting different output.

ShrinkError method is Error method here.

This article has been dead for over six months. Start a new discussion instead.