title: C# DLLImport convert "const char*" to string
body: <p>I need to implement this DLLImport in C#</p>
  
  <pre><code>const char* PegaSolicitacao(const char* CNPJ,
                              const char* CPF,
                              const char* CRM,
                              const char* UF_CRM,
                              const char* DT_EMISSAO );
  </code></pre>
  
  <p>The Dll could be found at this link
  <a href="https://farmaciapopular-portal-homologacao.saude.gov.br/farmaciapopular-portal/gbas/GBASMSB_2-Client.rar" rel="nofollow noreferrer">https://farmaciapopular-portal-homologacao.saude.gov.br/farmaciapopular-portal/gbas/GBASMSB_2-Client.rar</a></p>
  
  <p>Inside the .RAR \GBASMSB_2-Client\Ofd SDK 0.2 Windows.zip <strong>-> gbasmsb_library.dll</strong></p>
  
  <p>The only way that i got a return was with this code:</p>
  
  <pre><code>        [DllImport(@"gbasmsb_library.dll")]
      public static extern char PegaSolicitacao(string CNPJ,
                                                string CPF,
                                                string CRM,
                                                string UF_CRM,
                                                string DT_Emissao);
  </code></pre>
  
  <hr>
  
  <pre><code>            var Teste = PegaSolicitacao("31617905000139",
                                      "99999999484",
                                      "30828",
                                      "SP",
                                      DateTime.Today.ToString("d"));
  </code></pre>
  
  <p>But the return is suposed to be a string not a char.
  When I tried return a string in the DLLImport the system breaks, if I try to return a char[] I got a exception telling me about Marshaling.</p>
  
  <p>Im, new at C# and never worked with MarshalAs, but looking at the Forum I tried some option like:</p>
  
  <pre><code>                [DllImport(@"gbasmsb_library.dll", CharSet = CharSet.Ansi)]
      [return: MarshalAs(UnmanagedType.LPTStr)]
      public static extern char[] PegaSolicitacao([MarshalAs(UnmanagedType.LPArray)]char[] CNPJ,
                                                  [MarshalAs(UnmanagedType.LPArray)]char[] CPF,
                                                  [MarshalAs(UnmanagedType.LPArray)]char[] CRM,
                                                  [MarshalAs(UnmanagedType.LPArray)]char[] UF_CRM,
                                                  [MarshalAs(UnmanagedType.LPArray)]char[] DT_Emissao);
  </code></pre>
  
  <p>and some other variants too, but I cant find the right option.</p>