Consume DLL C in VB6 or C #

1

I have a DLL (gbasmsb_library.dll) made available by Popular Pharmacy responsible for returning some functions to interact with this environment. Home I think it's a DLL written in C, I need to consume this DLL in VB6 or C # .

In the programmer's Programmer's Guide I have the following information for using the DLL :
1:

  

Syntax:

const char* IdentificaEstacao();
  

Return Value: A string containing the machine's DNA.

2:

  

Syntax:

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

A string that will be used to validate the request by generating the signature of the   machine.

In VB6 I was able to consume the first function (1), the second one returns error:

  

49 Bad DLL calling convention

VB6 Code:

Private Declare Function PegaSolicitacao Lib "c:\temp\gbasmsb_library.dll" (ByRef CNPJ As String, ByRef CPF As String, ByRef CRM As String, ByRef UF_CRM As String, ByRef DT_EMISSAO As String) As String
Private Declare Function IdentificaEstacao Lib "c:\temp\gbasmsb_library.dll" () As String
    Private Sub Form_Load()
      MsgBox (IdentificaEstacao) //1 - retorno OK    

      Dim CNPJ As String, CPF As String, CRM As String, UF_CRM As String, DT_EMISSAO As String

      Dim resp As String
      CNPJ = "98352942000133"
      CPF = "72794534491"
      CRM = "7347"
      UF_CRM = "AM"
      DT_EMISSAO = "01/01/1991"
      resp = PegaSolicitacao(CNPJ, CPF, CRM, UF_CRM, DT_EMISSAO) //2 - ERRO
    end sub

I have already changed the function's signature to byVal, I changed the parameters to array too, without success.

C # code No success in calling any FUNCTION.

[DllImport(@"c:\temp\gbasmsb_library.dll")]
private static extern string PegaSolicitacao(string cnpj, string cpf, string crm, string ufCrm, string dtEmissao);

[DllImport(@"c:\temp\gbasmsb_library.dll")]
private static extern string IdentificaEstacao();

private void button1_Click(object sender, EventArgs e)
{
    var cnpj = "98352942000133";
    var cpf = "72794534491";
    var crm = "7347";
    var ufCrm = "PI";
    var dtEmissao = "01/01/1991";
    //MessageBox.Show(PegaSolicitacao(ref cnpj, ref cnpj, ref cnpj, ref cnpj, ref cnpj));
    //ERRO
    MessageBox.Show(PegaSolicitacao(cnpj, cpf, crm, ufCrm, dtEmissao));
    //ERRO
    IdentificaEstacao();
}
    
asked by anonymous 12.02.2015 / 17:20

1 answer

3

C / C ++ libraries have call conventions (the way in which the method is manipulated by the CPU, its stack, etc.), this implies in otmization.

  

link

Anyway, your solution is here:

  

link

Make sure the arguments are correct and in the same order, as well as the function name, also specify the calling convention directly in the attribute:

    // analise sua DLL, talvez não seja StdCall, pode ser Cdecl ou outras convenções
    [DllImport("Foo.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern void FooMethod();

Maybe some extra points are needed as CharSet (Unicode or Ansi) and ExactSpelling=true

Another important point:

  

link

Strings (char * for string) - > Use System.IntPtr since char * is a pointer, you will need to create the string after calling the method by copying by Marshal .

Signature of Roles

public class Gba
{

    //const char* IdentificaEstacao();
    [return: MarshalAsAttribute(UnmanagedType.AnsiBStr)]
    [DllImport("gba.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
    public static extern string IdentificaEstacao();

    //const char* PegaSolicitacao( const char* CNPJ, const char* CPF, const char* CRM, const char* UF_CRM, const char* DT_EMISSAO );
    [return: MarshalAsAttribute(UnmanagedType.AnsiBStr)]
    [DllImport("gba.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
    public static extern string PegaSolicitacao(string CNPJ, string CPF, string CRM, string UF_CRM, string DT_EMISSAO );

    //const char* PegaConfirmacao( const char* CNPJ, const char* NU_AUTORIZACAO, const char* NU_CUPOM_FISCAL );
    [return: MarshalAsAttribute(UnmanagedType.AnsiBStr)]
    [DllImport("gba.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
    public static extern string PegaConfirmacao(string CNPJ, string NU_AUTORIZACAO, IntPtr NU_CUPOM_FISCAL );
}
    
12.02.2015 / 17:57