C#DLLImport将“ const char *”转换为字符串

时间:2019-03-12 14:51:12

标签: c# marshalling dllimport

我需要在C#中实现此DLLImport

const char* PegaSolicitacao(const char* CNPJ,
                            const char* CPF,
                            const char* CRM,
                            const char* UF_CRM,
                            const char* DT_EMISSAO );

可以在此链接上找到Dll https://farmaciapopular-portal-homologacao.saude.gov.br/farmaciapopular-portal/gbas/GBASMSB_2-Client.rar

.RAR \ GBASMSB_2-Client \ Ofd SDK 0.2 Windows.zip内部-> gbasmsb_library.dll

我得到退货的唯一方法是使用以下代码:

        [DllImport(@"gbasmsb_library.dll")]
    public static extern char PegaSolicitacao(string CNPJ,
                                              string CPF,
                                              string CRM,
                                              string UF_CRM,
                                              string DT_Emissao);

            var Teste = PegaSolicitacao("31617905000139",
                                    "99999999484",
                                    "30828",
                                    "SP",
                                    DateTime.Today.ToString("d"));

但是返回值应该是字符串而不是char。 当我尝试在DLLImport中返回一个字符串时,系统中断,如果我尝试返回char [],则会出现异常,告诉我有关封送处理的信息。

我是C#的新手,从未与MarshalAs合作,但是在论坛上,我尝试了一些选择,例如:

                [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);

以及其他一些变体,但我找不到正确的选项。

0 个答案:

没有答案