G
Gabriel Bassani Ribeiro
Guest
Exellent night!
In a pascal example code, I make the following excerpt:
unit dllTeste;
interface
uses Windows;
Type
AbastPAF2 = record
value: boolean;
total_dinheiro: currency;
total_litros: double;
PU: currency;
tempo: integer;
codbico: string[2];
numbico: integer;
numtanque: integer;
voltanque: integer;
codcombustivel: integer;
seriecbc: integer;
tipocbc: char;
datetime: TDatetime;
st_full: string[123];
registro: integer;
encerranteI: double;
encerranteF: double;
integridade: boolean;
checksum: boolean;
tag1: string[16];
tag2: string[16];
end;
{$IFNDEF EMPLOYER}
Function InicializaSocket2(ip: ansistring;porta:integer): boolean; stdcall;
Function LeAbastecimentoPAF2(NumReg:integer): AbastPAF2; stdcall;
{$ENDIF}
implementation
{$IFNDEF EMPLOYER}
Function InicializaSocket; external 'EMPLOYER.DLL' name 'InicializaSocket';
Function LeAbastecimentoPAF2; external 'EMPLOYER.DLL' name 'LeAbastecimentoPAF2';
{$ENDIF}
end.
Then I can use the DLL in code. However in C # I can only replicate the "InitializeSocket". My goal is just to make it work, then I improve the code:
using System;
using System.Runtime.InteropServices;
namespace WindowsFormsApp
{
public static class TesteExtracao
{
[DllImport("EMPLOYER.dll", EntryPoint = "InicializaSocket")]
public static extern Boolean OpenSocket(string ip);
[DllImport("EMPLOYER.dll", EntryPoint = "LeAbastecimentoPAF2")]
public static extern AbastPAF2 ReadSupply(int numReg);
}
[StructLayout(LayoutKind.Sequential)]
public struct AbastPAF2
{
public bool value;
public decimal total_dinheiro;
public double total_litros;
public decimal PU;
public int tempo;
public string codbico;
public int numbico;
public int numtanque;
public int voltanque;
public int codcombustivel;
public int seriecbc;
public char tipocbc;
public DateTime datetime;
public string st_full;
public int registro;
public double encerranteI;
public double encerranteF;
public bool integridade;
public bool checksum;
public string tag1;
public string tag2;
}
}
And on the call:
if (NativeMethods.InicializaSocket2(IP, 2001))
{
btnClose.Enabled = true;
btnConnect.Enabled = false;
Models.Type.AbastPAF2 ab = NativeMethods.ReadSupply(1480);
var Checked = ab.checksum;
var integridade = ab.integridade;
var value = ab.value;
var EditTotaisDin = ab.total_dinheiro;
var EditCanal = ab.codbico;
var EditPPL = ab.PU;
var EditData = ab.datetime;
//var EditHora = ab.datetime;
var EditTempo = ab.tempo;
var EditEnc = ab.encerranteI / ab.encerranteF;
var EditRegistro = ab.registro;
var EditTotaisLT = ab.total_litros;
var EditString = ab.st_full;
rchTxtBxLog.AppendText($@"--------------------------------
Ident.: {ab.tag1}
Total: {ab.total_dinheiro}
String: {ab.st_full}
Volume: {ab.total_litros}
P.Unit : {ab.PU}
Tempo: {ab.tempo}
Bico: {ab.codbico}
Data: {ab.datetime}
Hora: {ab.datetime}
Reg: {ab.registro}
Enc.L. : {ab.encerranteI / ab.encerranteF}
--------------------------------");
}
else
rchTxtBxLog.AppendText($"Erro na conexão com a concentradora no IP {_Connection.IP}. Tentando novamente...\r\n");
I can return the connection, but when it arrives in the read (where it has a custom type) it returns the following message:
"System.Runtime.InteropServices.MarshalDirectiveException: 'Signature of type of method is not compatible with PInvoke.'"
Can someone help me?
Continue reading...
In a pascal example code, I make the following excerpt:
unit dllTeste;
interface
uses Windows;
Type
AbastPAF2 = record
value: boolean;
total_dinheiro: currency;
total_litros: double;
PU: currency;
tempo: integer;
codbico: string[2];
numbico: integer;
numtanque: integer;
voltanque: integer;
codcombustivel: integer;
seriecbc: integer;
tipocbc: char;
datetime: TDatetime;
st_full: string[123];
registro: integer;
encerranteI: double;
encerranteF: double;
integridade: boolean;
checksum: boolean;
tag1: string[16];
tag2: string[16];
end;
{$IFNDEF EMPLOYER}
Function InicializaSocket2(ip: ansistring;porta:integer): boolean; stdcall;
Function LeAbastecimentoPAF2(NumReg:integer): AbastPAF2; stdcall;
{$ENDIF}
implementation
{$IFNDEF EMPLOYER}
Function InicializaSocket; external 'EMPLOYER.DLL' name 'InicializaSocket';
Function LeAbastecimentoPAF2; external 'EMPLOYER.DLL' name 'LeAbastecimentoPAF2';
{$ENDIF}
end.
Then I can use the DLL in code. However in C # I can only replicate the "InitializeSocket". My goal is just to make it work, then I improve the code:
using System;
using System.Runtime.InteropServices;
namespace WindowsFormsApp
{
public static class TesteExtracao
{
[DllImport("EMPLOYER.dll", EntryPoint = "InicializaSocket")]
public static extern Boolean OpenSocket(string ip);
[DllImport("EMPLOYER.dll", EntryPoint = "LeAbastecimentoPAF2")]
public static extern AbastPAF2 ReadSupply(int numReg);
}
[StructLayout(LayoutKind.Sequential)]
public struct AbastPAF2
{
public bool value;
public decimal total_dinheiro;
public double total_litros;
public decimal PU;
public int tempo;
public string codbico;
public int numbico;
public int numtanque;
public int voltanque;
public int codcombustivel;
public int seriecbc;
public char tipocbc;
public DateTime datetime;
public string st_full;
public int registro;
public double encerranteI;
public double encerranteF;
public bool integridade;
public bool checksum;
public string tag1;
public string tag2;
}
}
And on the call:
if (NativeMethods.InicializaSocket2(IP, 2001))
{
btnClose.Enabled = true;
btnConnect.Enabled = false;
Models.Type.AbastPAF2 ab = NativeMethods.ReadSupply(1480);
var Checked = ab.checksum;
var integridade = ab.integridade;
var value = ab.value;
var EditTotaisDin = ab.total_dinheiro;
var EditCanal = ab.codbico;
var EditPPL = ab.PU;
var EditData = ab.datetime;
//var EditHora = ab.datetime;
var EditTempo = ab.tempo;
var EditEnc = ab.encerranteI / ab.encerranteF;
var EditRegistro = ab.registro;
var EditTotaisLT = ab.total_litros;
var EditString = ab.st_full;
rchTxtBxLog.AppendText($@"--------------------------------
Ident.: {ab.tag1}
Total: {ab.total_dinheiro}
String: {ab.st_full}
Volume: {ab.total_litros}
P.Unit : {ab.PU}
Tempo: {ab.tempo}
Bico: {ab.codbico}
Data: {ab.datetime}
Hora: {ab.datetime}
Reg: {ab.registro}
Enc.L. : {ab.encerranteI / ab.encerranteF}
--------------------------------");
}
else
rchTxtBxLog.AppendText($"Erro na conexão com a concentradora no IP {_Connection.IP}. Tentando novamente...\r\n");
I can return the connection, but when it arrives in the read (where it has a custom type) it returns the following message:
"System.Runtime.InteropServices.MarshalDirectiveException: 'Signature of type of method is not compatible with PInvoke.'"
Can someone help me?
Continue reading...