title: Pointers in C# to Retrieve Reference From DllImport Function
body: <p>I am referencing a DLL in my C# project as follows:</p>
  
  <pre><code>[DllImport("FeeCalculation.dll", CallingConvention = CallingConvention.StdCall,
             CharSet = CharSet.Ansi)]
  
          public static extern void FeeCalculation(string cin, string cout, string flimit,
              string frate, string fwindow, string fincrement, string fbird, 
              string fparameter, string fvalidation, string fcoupon);
  </code></pre>
  
  <p>The FeeCalculation function is exported as follows in the DLL:</p>
  
  <pre><code>extern "C" __declspec(dllexport) void __stdcall FeeCalculation(char *cin, 
  char *cout, char *flimit, char *frate,
  char *fwindow, char *fincrement, char *fbird,
  char *fparameter, char *fvalidation, char *fcoupon);
  </code></pre>
  
  <p>The DLL function returns a reference to it's internal structures in the form of char * so if you were to reference this DLL in C++, you would do the following to do the calculation and get the returned structures:</p>
  
  <pre><code>FeeCalculation(buff, (char *)&amp;fans, (char *)fl, (char *)ft, (char *)fw, (char *)fi, (char *)fe, (char *)&amp;fm, (char *)val, (char *)cpn);
  </code></pre>
  
  <p>Now, how do I retrieve those values that are returned by reference using C#? Meaning, how do I do the same thing in C# to get the returned structures to get my returned calculation? I know I need to create an unsafe method, but I am unclear on how to deal with the memory addresses in C# like you would in C++.</p>
  
  <p><strong>Edit: Below states to use IntPtr but how do you place into identical structure so the fields of the structure can be referenced?</strong></p>
  
  <p>Edit: Here is the returned structure that I am interested in (cout):</p>
  
  <pre><code>struct feeAnswer {
  
  
      unsigned int    fee;
  
      unsigned int    tax1;
  
      unsigned int    tax2;
  
      unsigned int    tax3;
  
      unsigned int    tax4;
  
      unsigned int    surcharge1;
  
      unsigned int    surcharge2;
  
      unsigned int    validationFee;
  
      unsigned int    couponFee1;
  
      unsigned int    couponFee2;
  
      unsigned int    couponFee3;
  
      unsigned int    couponFee4;
  
      unsigned short int dstay;       //Day Stay
  
      unsigned short int mstay;       //Minute Stay
  
  };
  </code></pre>
  
  <p>Here is the (cin) that I would pass along with other structures (they are zero byte at the moment, I want to get this to work first then I will implement the rest):</p>
  
  <pre><code>struct feeRequest {
  
      unsigned char   day;
  
      unsigned char   month;
  
      unsigned int    year;   //2000 ~ 2099
  
  
  
      unsigned char   hour;
  
      unsigned char   minute;
  
      unsigned char   rate;
  
      unsigned char   validation;
  
  
  
      unsigned char   coupon1;
  
      unsigned char   coupon2;
  
      unsigned char   coupon3;
  
      unsigned char   coupon4;
  
  };
  </code></pre>