-- Por Wesley Y
DLL para validar CPF e CNPJ - Parte I
Vamos criar uma DLL para validar CPF e CNPJ, então vamos criar a DLL.
Para maiores informações sobre DLL veja os links abaixo..
http://www.devmedia.com.br/articles/viewcomp.asp?comp=15450
http://www.devmedia.com.br/articles/viewcomp.asp?comp=15451
http://www.devmedia.com.br/articles/viewcomp.asp?comp=15452
Para criarmos a DLL.
Menu File / New / Other / DLL Wizard.
Agora iremos implementar nossa DLL. Salve a mesma com o nome prj_DLL_CNPJ_CPF, este será o nome da nossa DLL.
library prj_DLL_CNPJ_CPF;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils, Dialogs,
Classes;
{$R *.res}
// Valida dígito verificador de CNPJ
function TestaCGC(Dado : string) : boolean;stdCall;
var D1 : array[1..12] of byte;
I, DF1,
DF2, DF3,
DF4, DF5,
DF6, Resto1,
Resto2, PrimeiroDigito,
SegundoDigito : integer;
begin
Result := true;
if Length(Dado) = 14 then
begin
for I := 1 to 12 do
if Dado[I] in ['0'..'9'] then
D1[I] := StrToInt(Dado[I])
else
Result := false;
if Result then
begin
DF1 := 0;
DF2 := 0;
DF3 := 0;
DF4 := 0;
DF5 := 0;
DF6 := 0;
Resto1 := 0;
Resto2 := 0;
PrimeiroDigito := 0;
SegundoDigito := 0;
DF1 := 5*D1[1] + 4*D1[2] + 3*D1[3] + 2*D1[4] + 9*D1[5] + 8*D1[6] +
7*D1[7] + 6*D1[8] + 5*D1[9] + 4*D1[10] + 3*D1[11] + 2*D1[12];
DF2 := DF1 div 11;
DF3 := DF2 * 11;
Resto1 := DF1 - DF3;
if (Resto1 = 0) or (Resto1 = 1) then
PrimeiroDigito := 0
else
PrimeiroDigito := 11 - Resto1;
DF4 := 6*D1[1] + 5*D1[2] + 4*D1[3] + 3*D1[4] + 2*D1[5] + 9*D1[6] +
8*D1[7] + 7*D1[8] + 6*D1[9] + 5*D1[10] + 4*D1[11] + 3*D1[12] +
2*PrimeiroDigito;
DF5 := DF4 div 11;
DF6 := DF5 * 11;
Resto2 := DF4 - DF6;
if (Resto2 = 0) or (Resto2 = 1) then
SegundoDigito := 0
else
SegundoDigito := 11 - Resto2;
if (PrimeiroDigito <> StrToInt(Dado[13])) or
(SegundoDigito <> StrToInt(Dado[14])) then
Result := false;
end;
end
else
if Length(Dado) <> 0 then
Result := false;
end;
// Valida dígito verificador de CPF
function TestaCPF(Dado : string) : boolean;stdCall;
var D1 : array[1..9] of byte;
I, DF1,
DF2, DF3,
DF4, DF5,
DF6, Resto1,
Resto2, PrimeiroDigito,
SegundoDigito : integer;
begin
Result := true;
if Length(Dado) = 11 then
begin
for I := 1 to 9 do
if Dado[I] in ['0'..'9'] then
D1[I] := StrToInt(Dado[I])
else
Result := false;
if Result then
begin
DF1 := 0;
DF2 := 0;
DF3 := 0;
DF4 := 0;
DF5 := 0;
DF6 := 0;
Resto1 := 0;
Resto2 := 0;
PrimeiroDigito := 0;
SegundoDigito := 0;
DF1 := 10*D1[1] + 9*D1[2] + 8*D1[3] + 7*D1[4] + 6*D1[5] + 5*D1[6] +
4*D1[7] + 3*D1[8] + 2*D1[9];
DF2 := DF1 div 11;
DF3 := DF2 * 11;
Resto1 := DF1 - DF3;
if (Resto1 = 0) or (Resto1 = 1) then
PrimeiroDigito := 0
else
PrimeiroDigito := 11 - Resto1;
DF4 := 11*D1[1] + 10*D1[2] + 9*D1[3] + 8*D1[4] + 7*D1[5] + 6*D1[6] +
5*D1[7] + 4*D1[8] + 3*D1[9] + 2*PrimeiroDigito;
DF5 := DF4 div 11;
DF6 := DF5 * 11;
Resto2 := DF4 - DF6;
if (Resto2 = 0) or (Resto2 = 1) then
SegundoDigito := 0
else
SegundoDigito := 11 - Resto2;
if (PrimeiroDigito <> StrToInt(Dado[10])) or
(SegundoDigito <> StrToInt(Dado[11])) then
Result := false;
end;
end
else
if Length(Dado) <> 0 then
Result := false;
end;
// Validar a CPF e CNPJ, esta é a função que iremos executar na aplicação
function TestaCpfCgc(Dado: string): String; stdCall;
var
i: integer;
begin
for i:= 1 to length(Dado) do begin
if not (Dado[i] in ['0'..'9']) then delete(Dado,i,1);
end;
if ((length(Dado) <> 11) and (length(Dado) <> 14))then
MessageDlg('ATENÇÃO: O número informado NÃO representa' + #13 +
'um CPF ou CGC válido pelas regras da Receita Federal',mtWarning,[mbOK],0);
if length(Dado) = 14 then begin
if TestaCGC(Dado) then begin
insert('-',Dado,13);
insert('/',Dado,9);
insert('.',Dado,6);
insert('.',Dado,3);
end
else MessageDlg('O número informado NÃO representa um CGC' + #13 +
'válido pelas regras da Receita Federal', mtWarning, [mbOK], 0);
end;
if length(Dado) = 11 then begin
if TestaCPF(Dado) then begin
insert('-',Dado,10);
insert('.',Dado,7);
insert('.',Dado,4);
end
else MessageDlg('O número informado NÃO representa um CPF' + #13 +
'válido pelas regras da Receita Federal', mtWarning, [mbOK], 0);
end;
Result := Dado;
end;
exports
// Exportamos somenet a Function TestaCPFCGC, pois esta é quem irá validar, as
// demais não precisam ser exportadas pois não serão usadas.
TestaCpfCgc;
begin
end.
//Fim da implementação da DLL
Proximo post veremos como consumir esta DLL.
Take care.
10 comentários:
ӏ'm not sure exactly why but this weblog is loading very slow for me. Is anyone else having this issue or is it a issue on my end? I'll check bacκ lаter and seе if the problem still еxists.
Stop by my site :: mailer.urp.edu.pe
I do accеpt as true with all of the concеpts you've offered to your post. They're really сonvincing and can ԁefіnitely work.
Nonеthеless, the posts arе too brіef for newbies.
Could you plеase prolong them a littlе frоm next time?
Thаnks for the post.
Нere is my homеρage - related website
you're truly a just right webmaster. The website loading velocity is amazing. It seems that you're
ԁoing any diѕtinctive tricκ. Also, The contents are
masterpieсе. yоu have performеd a
fantastic procesѕ in this mattег!
Feel free to visit my wеb blog - http://pagefarm.net
my site - Simply click the up coming internet site
Hello, Neat post. Τherе's an issue together with your website in internet explorer, would check this? IE nonetheless is the marketplace chief and a good component to other folks will omit your wonderful writing because of this problem.
my web-site - www.laudoimagem.com.br
Wonԁerful, whаt a blog it іѕ! Thіs webѕite presents
useful infoгmation to us, keep іt up.
my wеblog :: Find Out More
lеt me know whеrе dο Ι purсhase fοг thеm
Also visit my webpagе :: green Smoke E Cig
I was able to find gooԁ advicе frοm your articles.
My web-sіte: home laser hair
Thiѕ blog ωаs... how do you sаy it?
Relevant!! Fіnally I've found something that helped me. Kudos!
Feel free to visit my website - http://futuretechsystems.com/30760/why-do-you-gain-weight-when-stop-smoking
Anyone who is prepared to get a properly-toned stomach
can use this belt.
Review my blog post :: www.marsvenusatwork.com/
Vapor Couture, released vapor but it does not contain the very same
smoke and negative effects to the body as opposed to the conventional cigarette smoking.
The Official Presenter's Gift Lounge created by On three Productions and held backstage at the 2013 Film Independent Spirit Awards beachside in Santa Monica. V2cigs has brought a main lots of final results, and as nicely as really quite impressive testimonials in other customers. What is more, Naturally i accepted a comprehensive charger that do attaches with regard to the A/C adapter inside n automobile, effectively as the common Universal series bus battery charger you in the initial instance varieties. Outstanding customer service!
my weblog ... v2 cigs coupon code 25
Postar um comentário