title: Creating a basic C++ .dll for p/invoke in C#
body: <p>I am a C# programmer, and unfortunately due to both age and experience, I did not have the luxury of getting the chance to go through the C++ era of programming in my learning - so much of it is mysterious and new to me. Not really here to debate the importance of learning such or not, but I am in need of some help with what should be a trivial matter.</p>
  
  <h2>PROBLEM</h2>
  
  <p>I need assistance in packaging my C++ code into a .dll. I do not have experience with C++ and am having a great deal of difficulty making a working .dll that I can p/invoke from (<em>Visual Studio 2010</em>). Please keep reading for more details and for the code I am trying to package.</p>
  
  <h2>DETAILS</h2>
  
  <p>I have some code that needs to run in an unmanaged environment. Under normal circumstances, a simple p/invoke is suitable for this task. Wire up some <code>[ImportDll]</code> and you're good to go. Worst case scenario I can sometimes use Marshalling. However for reasons that I have yet to really discover, this does not seem possible with my present task. </p>
  
  <p>I am trying to read some data from an <code>owner drawn list box</code> that was made in an old unmanaged C++ application, and get the text from it. I have seen several examples of how to do this - but they are in older VB6 code. </p>
  
  <p>I have discovered the C++ equivalent, but this too is unsuitable for me. Even with p/invoke, there seems to be a lot of trouble with the moving of memory and how it needs to operate. The process is pretty simplistic.</p>
  
  <blockquote>
    <p>I need to create a simple C++ .dll that runs the method I need in unmanaged code, and populates a variable with the result. </p>
  </blockquote>
  
  <p>As dumb as this sounds, <strong>I do not know how to do this</strong>. I have tried just running through the simple C++ Wizards in Visual Studio (using Visual Studio 2010) and I'm just not having luck. I was hoping someone could help me walk through this, and perhaps explain what is going on so I can learn from it.</p>
  
  <p>Here is the code I need to place into a <code>.dll</code>. </p>
  
  <p>The intention is for <code>hListWnd</code> to be passed as an <code>IntPtr</code> in C#, <code>index</code> will be a simple <code>Int32</code>, and <code>outputResult</code> will go to whatever is neccessary to make this work. In other p/invoke situations, I have seen this done with <code>System.Text.StringBuilder</code>, but I am willing to work with <strong>anything</strong> to make this work right. </p>
  
  <pre><code>void GetListItemData( HWND hListWnd, long index, char *outputResult )
  {
      int result;
      DWORD processID;
      HANDLE hProcess;
      char *itemData;
      char sDataRead[5];
      DWORD bytes;
      DWORD lListItemHold, lListItemDataHold;
      *outputResult=0;
  
      if( hListWnd )
      {
          GetWindowThreadProcessId( hListWnd, &amp;processID );
  
          hProcess=OpenProcess( 0x10|0xf0000|PROCESS_VM_READ, 0, processID );
  
          if( hProcess )
          {
              lListItemHold=(DWORD)SendMessage( hListWnd, LB_GETITEMDATA, index-1, 0 );
              lListItemHold=lListItemHold+24;
  
              result=ReadProcessMemory( hProcess, (void *)lListItemHold, &amp;sDataRead, 4, &amp;bytes );
              if( !result )
              {
                  RaiseWinErr();
              }
  
              memcpy( &amp;lListItemDataHold, &amp;sDataRead, 4 );
              lListItemDataHold=lListItemDataHold+6;
  
              ReadProcessMemory( hProcess, (void *)lListItemDataHold, outputResult, 16, &amp;bytes );
  
              CloseHandle( hProcess );
          }
      }
  }
  </code></pre>
  
  <p>If anyone can help me, please, I am begging here. This has become a very frustrating problem for me. I have tried to reproduce this in C# using <code>RtlMoveMemory</code> and <code>ReadProcessMemory</code> p/invoke, but to no avail. I have been hunting for days and days, and continue to come up with dead results. I know that some of the code does not make a lot of sense (I've had people ask me why it is requiring the <code>+6</code> and what the <code>+24</code> is, etc), and the absolute truth is that I don't <strong>entirely</strong> know. What I do know is that the <strong>owner drawn list box</strong> employs some graphics on it, and the purpose of these offsets is to examine the data beyond where the graphic area is drawn, because the actual structure of the <code>List Item</code> is unknown. </p>
  
  <p>Another method I have tried is retrieving my item as an <code>IAccessible</code> using the <code>Accessibility.dll</code> library. This too proved fruitless. I have also tried <code>ManagedWinApi</code> from pinvoke.net and the libraries there did not prove any success.</p>
  
  <p>I know this code works. It just doesn't work in C#. My theory is that using a <code>[DllImport]</code> to invoke it in C# will give me the desired result. </p>
  
  <p>Thanks again for any help anyone provides.</p>