none
how to use TlsSetValue with C# RRS feed

  • Question

  • hi I am trying to convert some legacy vc++ code to c#; i found a place in legacy code where

    TlsSetValue was used and I wonder if I need to use the same function as

    [DllImport("kernel32.dll")]
    static extern bool TlsSetValue(uint dwTlsIndex, IntPtr lpTlsValue);

    but then how do I implement the second parameter: as ref? as out? 

    and what would be the type of the second parameter?

    thanks in advance

    Wednesday, May 15, 2013 8:04 PM

Answers

  • When you convert that struct to a C# class, you can store the thread local data you're trying to use in a ThreadLocal<ThatClass>, and bypass the entire logic used here.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by kobosh Wednesday, May 15, 2013 11:38 PM
    Wednesday, May 15, 2013 10:38 PM
    Moderator

All replies

  • You should use ThreadLocal<T> instead: http://msdn.microsoft.com/en-us/library/dd642243.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, May 15, 2013 8:12 PM
    Moderator
  • this is the c++ code  

    BOOL CProvThread::InitInstance()
    {
      ASSERT( m_tld.id != -1 ); 
    
      BOOL rc = FALSE;
    
      char title[_MAX_PATH+1];//added 1
    
      memset (title, 0, sizeof(title));
    
      if(m_tld.id >= MAX_NUMBER_STATIC_PROV_THREADS)
        strcpy(title, m_newThreadData.title);
      else
      {
        strcpy(title, g_threadData[m_tld.id].title);
      }
    
      if ( TlsSetValue( g_tlsIndex, (LPVOID)&m_tld ) )
      {
        rc = TRUE;
        SystemLog( "%s thread starting, TID %ld", title, GetCurrentThreadId() );
      }
      else
        ErrorMessage( "TlsSetValue failed for %s thread", title );
      //AfxMessageBox(title);
    	return rc;
    }

    m_tld  is struct     with thread id, logdirectory, and log file information

    how  ThreadLocal<t> would fit into this!

    Wednesday, May 15, 2013 10:33 PM
  • When you convert that struct to a C# class, you can store the thread local data you're trying to use in a ThreadLocal<ThatClass>, and bypass the entire logic used here.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by kobosh Wednesday, May 15, 2013 11:38 PM
    Wednesday, May 15, 2013 10:38 PM
    Moderator
  • Hi reed 

    i want to go dllimport way

    [DllImport("kernel32.dll")]
    static extern bool TlsSetValue(uint dwTlsIndex, IntPtr lpTlsValue);

    the first parameter is  int the second is struct

    how do this part

    if(TlsSetValue(.........))

    i tried 

    if(TlsSetValue(100,mystruct)) 

    but this gives error

    Thursday, May 16, 2013 2:01 PM
  • I would strongly recommend avoiding this - 

    If you want to do this, you'll have to do something to put the struct into a memory location where it's persistent.  You can do this via Marshal.StructureToPtr (and then retrieve the data via Marshal.PtrToStructure).  Note that these copy the struct, and require you to manage the memory, which really takes away a lot of the advantages of C#...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 16, 2013 3:54 PM
    Moderator
  • you are right;

    i did not understand c++ was allocating memory to store the struct

    now you say threadlocal   will take care of that 

    but wait: at one time i heard that GC can interfere with thread processing because gc is random and when it    goes collecting  it might block a thread!

    what is your answer?

    Friday, May 17, 2013 2:01 PM
  • but wait: at one time i heard that GC can interfere with thread processing because gc is random and when it    goes collecting  it might block a thread!

    what is your answer?

    This won't be a problem.  If the GC blocks the thread (which is very temporary, especially in .NET 4.5), the thread local won't be "used" while the thread is blocked.  If it relocates memory, that too won't be an issue - it'll act like any other managed object.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, May 17, 2013 4:07 PM
    Moderator