How to pass TCHAR* returned by one function to another function in(Managed) C++
<p>I have a function called ReversedIPAddressString which takes IPAddress as a TCHAR* and then returns reveresed IPAddress as TCHAR*. I am able to get reversed IP fine but when I passed this TCHAR* pointer(reversedIP) to some other function(let's say dns.query(TCHAR*)),IP value always junk. I am wondering what I am missing?</p>

<p>I am pasting my code here for your reference...</p>

<h3>Caller Method:</h3>

<pre><code>bool DNSService::DoesPtrRecordExixts(System::String^ ipAddress)
{
    IntPtr ipAddressPtr = Marshal::StringToHGlobalAuto(ipAddress);
    TCHAR* ipAddressString = (TCHAR*)ipAddressPtr.ToPointer();
    bool bRecordExists = 0;

    WSAInitializer initializer;
    Locale locale;

    // Initialize the identity object with the provided credentials
    SecurityAuthIdentity identity(userString,passwordString,domainString);
    // Initialize the context
    DnsContext context;
    // Setup the identity object
    context.acquire(identity);

    DnsRecordQueryT&lt;DNS_PTR_DATA&gt; dns(DNS_TYPE_PTR, serverString);
    try
    {
        bRecordExists = dns.query(ReversedIPAddressString(ipAddressString)) &gt; 0;
    }
    catch(SOL::Exception&amp; ex)
    {
        // Free up the pointers to the resources given to this method
        Marshal::FreeHGlobal(ipAddressPtr);

        if(ex.getErrorCode() == DNS_ERROR_RCODE_NAME_ERROR)
            return bRecordExists;
        else
            throw SOL::Exception(ex.getErrorMessage());
    }

    // Free up the pointers to the resources given to this method
    Marshal::FreeHGlobal(ipAddressPtr);

    return bRecordExists;
}
</code></pre>

<h3>Called Method:</h3>

<pre><code>TCHAR* DNSService::ReversedIPAddressString(TCHAR* ipAddressString)
{
    TCHAR* sep = _T(".");
    TCHAR ipArray[4][4];
    TCHAR reversedIP[30];
    int i = 0;

    TCHAR* token = strtok(ipAddressString, sep);
    while(token != NULL)
    {
        _stprintf(ipArray[i], _T("%s"), token);
        token = strtok((TCHAR*)NULL, sep);
        i++;
    }
    _stprintf(reversedIP, _T("%s.%s.%s.%s.%s"), ipArray[3], ipArray[2], ipArray[1], ipArray[0],_T("IN-ADDR.ARPA"));

    return reversedIP;
}
</code></pre>

<h3>DNS.Query method declaration:</h3>

<pre><code>int query(__in const TCHAR* hostDomain, __in DWORD options=DNS_QUERY_STANDARD)
</code></pre>

<p>Hope to get help from you. </p>

<p>Thanks in advance!</p>

<p>Ramani   </p>
