Login to facebook with Indy - facebook

I would like to login to my facebook account with Indy. The version is 9.00.10 and I use OpenSSL with TIDHTTP and assigned a cookie manager to it. Everything works fine (I can send a POST request a GET, etc.)
I sniffed the actual login to facebook and I have the following information:
UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)
There are several POST parameters:
lsd = I have no idea what that is.
email = The actual facebook username/email.
pass = The password (unencrypted) --> I was shocked to see in clear text.
default_persistent = (0 or 1) for "keep me logged in"
timezone = Timezone code.
lgnrnd = I have no idea what that is.
lgnjs = I have no idea what that is.
locale = GEOIP location (e.x. en_US)
The post is made on https://www.facebook.com/login.php?login_attempt=1. However when I try to login it returns that I have entered an incorrect eMail. I'm sure I used the right eMail and Password.
Here is my code:
procedure TForm1.Button1Click(Sender: TObject);
var
TEST : STRING;
lParamList: TStringList;
i : Integer;
begin
lParamList := TStringList.Create;
lparamlist.Add('lsd=AVoBzJ5G');
lparamlist.Add('email=myeMail%40mysite.com');
lparamlist.Add('pass=mypass');
lparamlist.Add('default_persistent=0');
lparamlist.Add('timezone=240');
lparamlist.Add('lgnrnd=210302_FeQV');
lparamlist.Add('lgnjs=1367035381');
lparamlist.Add('locale=en_US');
IDHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
Test := IdHTTP1.Get('https://www.facebook.com'); // To get the first cookies.
for i := 0 to IDHTTP1.CookieManager.CookieCollection.Count - 1 do begin
ShowMessage(IDHTTP1.CookieManager.CookieCollection.Items[i].CookieText); // Show me the cookies.
end;
TEST := IDHTTP1.Post('https://www.facebook.com/login.php?login_attempt=1', lParamList);
StrToFile ('text.html', test);
ShellExecute (0, 'open', 'text.html', '', '', SW_SHOW);
end;
I used the parameters that I got from LiveHTTPHeaders.
How would I successfully login to facebook with Indy?
EDIT: Tried this with XE2 and Indy 10 but I get the 'incorrect email' error:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdCookieManager, IdIOHandler,
IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdBaseComponent,
IdComponent, DateUtils, ShellAPI, IdTCPConnection, IdTCPClient, IdHTTP, Vcl.StdCtrls;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
IdCookieManager1: TIdCookieManager;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetBetween (Str: String; StrStart : String; StrEnd : String) : String;
var
iPos : Integer;
BackUp : String;
begin
result := '';
iPos := Pos (StrStart, Str);
if iPos <> 0 then begin
Delete (Str, 1, iPos + Length (StrStart) - 1);
iPos := Pos (StrEnd, Str);
if iPos <> 0 then begin
result := Copy(Str,1, iPos - 1);
end;
end;
end;
function StrToFile(szFilePath:string; dwPosition:DWORD; szInput:string):Boolean;
var
hFile: DWORD;
dwSize: DWORD;
dwWritten: DWORD;
begin
Result := FALSE;
hFile := CreateFileW(PWideChar(szFilePath), GENERIC_WRITE, 0, nil, CREATE_ALWAYS, 0, 0);
if hFile <> INVALID_HANDLE_VALUE then
begin
dwSize := Length(szInput) * 2;
if dwSize > 0 then
begin
SetFilePointer(hFile, dwPosition, nil, FILE_BEGIN);
WriteFile(hFile, szInput[1], dwSize, dwWritten, nil);
if dwWritten = dwSize then
Result := TRUE;
end;
CloseHandle(hFile);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Response : String;
lparamList : TStringList;
begin
IDHTTP1.Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
try
Response := IDHTTP1.Get('https://www.facebook.com/');
except
end;
lParamList := TStringList.Create;
lParamList.Add('lsd='+GetBetween (Response, 'name="lsd" value="', '"'));
lParamList.Add('eMail=myEmail#mySite.com');
lParamList.Add('pass=myPassword');
lParamList.Add('default_persistent'+GetBetween (Response, 'name="default_persistent" value="', '"'));
lParamList.Add('timezone=240');
lParamList.Add('lgnrnd='+GetBetween (Response, 'name="lgnrnd" value="', '"'));
lParamList.Add('lgnjs='+inttostr(DateTimeToUnix(Now)));
lParamList.Add('locale=en_US');
IDHTTP1.Request.Referer := 'https://www.facebook.com/';
try
Response := IDHTTP1.Post('https://www.facebook.com/login.php?login_attempt=1', lparamList);
except
end;
StrToFile ('test.html', 0, Response);
ShellExecute (0, 'open', 'test.html', '', '', SW_SHOW);
end;
end.

If the hoForceEncodeParams flag is enabled in the TIdHTTP.HTTPOptions property (which it is by default), then you need to fill the posted TStringList with un-encoded values. TIdHTTP.Post() will then encode the values for you when transmitting them.
Assuming the hoForceEncodeParams flag is enabled, lparamlist.Add('email=myeMail%40mysite.com'); would be transmitted as email=myeMail%2540mysite.com because the % character gets encoded as %25. Facebook would decode that as email=myeMail%40mysite.com and reject it as an invalid email.
You can either:
disable the hoForceEncodeParams flag so the TStringList values get transmitted as-is. You would then be responsible for encoding them manually.
leave the hoForceEncodeParams flag enabled and change lparamlist.Add('email=myeMail%40mysite.com'); to lparamlist.Add('email=myeMail#mysite.com'); instead. TIdHTTP.Post() in Indy 9 will then transmit it as email=myeMail#mysite.com because Indy 9 does not encode the # character. That may or may not work, depending on how lenient Facebook is.
If you upgrade to Indy 10, TIdHTTP.Post() will encode the # character as %40 as expected when the hoForceEncodeParams flag is enabled.

For anyone interested, the OP's code works just fine with the mobile versions of Facebook, so just replace www.facebook.com with touch/m.facebook.com.
Now if the OP would also be kind enough to share how he got the full version to work (at least the Cookies not enabled part), I'm sure we'll all be grateful.

Related

How to post data with a ContentType of 'multipart/form-data' in Delphi REST?

I am trying to send a request to a REST API using multipart/form-data as the content type.
I always get "HTTP/1.1 500 Internal Error" as a response.
I tried sending requests to methods that require application/x-www-form-urlencoded and had success, though.
How can I achieve getting a success response from my API using multipart/form-data?
Here is my code:
procedure TForm10.Button1Click(Sender: TObject);
var
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
strImageJSON : string;
Input: TIdMultipartFormDataStream;
begin
Input := TIdMultipartFormDataStream.Create;
Input.Clear;
Input.AddFormField('Email', 'tugba.xx#allianz.com.tr');
Input.AddFormField('Password', 'xxxx');
RESTClient1 := TRESTClient.Create('http://192.168.1.172:81/');
RESTRequest1 := TRESTRequest.Create(nil);
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'api/Mobile/MobileLoginControl';
RESTRequest1.AddBody(Input,TRESTContentType.ctMULTIPART_FORM_DATA);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Execute;
strImageJSON := RESTRequest1.Response.Content;
end;
Embarcadero's REST component has its own built in multipart/form-data capabilities via the TRESTRequest.AddParameter() method:
procedure TForm10.Button1Click(Sender: TObject);
var
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
strImageJSON : string;
begin
RESTClient1 := TRESTClient.Create('http://192.168.1.172:81/');
try
RESTRequest1 := TRESTRequest.Create(nil);
try
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'api/Mobile/MobileLoginControl';
RESTRequest1.AddParameter('Email', 'tugba.xx#allianz.com.tr', TRESTRequestParameterKind.pkREQUESTBODY);
RESTRequest1.AddParameter('Password', 'xxxx', TRESTRequestParameterKind.pkREQUESTBODY);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Execute;
strImageJSON := RESTRequest1.Response.Content;
finally
RESTRequest1.Free;
end;
finally
RESTClient1.Free;
end;
end;
You don't need to use use Indy's TIdMultiPartFormDataStream, especially when you are not using Indy's TIdHTTP.

dsRESTConnection component won't allow me change the .username and .password after the first connect

In my Delphi 10.3.1 application server (which is a "DataSnap REST Application"), I have put some logging as follows:
procedure TWebModule1.DSAuthenticationManager1UserAuthenticate(
Sender: TObject; const Protocol, Context, User, Password: string;
var valid: Boolean; UserRoles: TStrings);
begin
valid := (User > '') and (Password > ''); // pass-through
if Valid then
begin
MyWebUtils.LogMessage('Authentication, ' + User + ', ' + Password +
', ' + protocol + ', ' + context);
TDSSessionManager.GetThreadSession.PutData('USERNAME', User);
end;
end;
In my client (on Android), which uses a dsRestConnection, I have a procedure to test the connection, which I call, for example in FormShow of the main form.
function TClientModuleRest.TestConnect(var ErrorMsg: String): Boolean;
var
returned: String;
begin
Result := True;
try
DSRestConnection.Username := 'Admin';
DSRestConnection.Password := 'Test';
DSRestConnection.TestConnection([toNoLoginPrompt]);
except
on e: exception do
begin
Result := False;
ErrorMsg := e.Message;
end
end;
end;
This works well.
The problem occurs when I call the first Server method.
function TClientModuleRest.ValidUser(username, password: String;
var Response: String): Integer;
var
Server: TServerMethodsRClient;
Valid: Boolean;
IniFile: TStringList;
FileName: TFileName;
begin
Result := 0;
DSRestConnection.UserName := username;
DSRestConnection.Password := password;
try
Server := TServerMethodsRClient.Create(DSRestConnection);
try
Valid := Server.ValidUser(username, password, Response);
if not Valid then
raise Exception.Create(response);
Result := 1;
finally
Server.Free;
end;
except
on e: exception do
begin
Response := e.Message;
Result := -1; // an error occured
end;
end;
end;
So the problem is that the setting of the .username and .password doesn't 'take'. In other words, my conclusion is that the .username and `.password can only be set before the first connect, and not thereafter.
The server log file shows that even though the ValidUser function was called with the username and password values that were passed to it, the authentication is receiving the username and password from the first (test) connect.
Is this by design? Or am I missing something? Thanks.

Why does indy return unreadable data but chrome loads it fine?

I'm trying to do a GET request on one of my game server's RESTAPI. When I load it in chrome it load with the correct json response but when I try to do it with indy it just gives me a bunch of unreadable text.
URL for the request: http://129.232.180.125:28019/deaths.json
Chrome Response:
Indy Reponse:
Y;9[ÿÍZË7¼ç3öìDQ%ÿJÃÚØ,Öw òï§IµzvXÓö{Ab¿ÿöÏÝ»÷Ä!¾»ûûîýBó»»÷ßOwïï><¼îþ}§ På¢&È÷ÇÇÓË
gP¤#¹(¬Dv&©ÙEµ¸®D"`»#ëJ¥E\¦£??Þ?Ù#%¤3¨µ òrÿaCHZÝÀÕ
¯[%ö1cÓÔB)QV¡÷àõ=¢oÅt¥þóR§Ó§§Ï®Ô¶â*ëEu<wKÜôiýUØ^VµcÓ#vêâ»ÈR©hPædÛ=û±ô~¡;±8¢¢ÒtÇ/§û
Si¿·Õ
Zô1-W¥²¾KdP¡tÇXýc--[ÛàÔ-Go¼aI4]ñôãÏǯÏ_& h¶#5Gá0Ú#!`u.éöæà¿i³ÇhFÜ]Q´µjÁ
d¼Ê%½|ÔRØ\­¡Ókbê;Â$`¹^ìX»Ó`ϢѤù» i2S$Ýûöõ¯ÉÑ2 D(,öæÛý§ûÙ¯j\-AOÙ^&Gä£WK¥2\ÙW[%a»a\Ò' U½4ßß+¯>bV«ÏêUçq¡õÏIÜ
,1×çiJ¾¸¸^Ðh}_æéê{²Ú°pp¼²KÖ÷ÞNÄIß¼D÷W+K3ËûÔz:}ÚÞ5
SB^"ÕKªGè¸+¾ß^~$Õâ³6Tõ]2Õßr9¸%eàYYUrsJÏ`Êä³ë'£t8#®AB¨KϹëOAe9V ×Ís-£¬Ó鬺ùÜ\)µµ©±¢xÜj« ½ê(JRâÜ>\<M´#6Xd°ý~®i ÄCõBe;&©l³"¦}.uUè5(ÝE®V;BìðùôtúöðqÆÅ8~cÜ¡}{\h9Í©¡0²]Cèl¯s%qeÅô{Ø]zOöÞ|W-ÆöÎ%媫¦#çÎл[jѶÊÞÒÙq¤¸qZÛ/îÕ=KTÓ³ÿÒ=Go#Îì6*Z¯\W7¡ê!rM´'£¸ÇR­»[G%ëp"?+²q?!ÔU{n,QâsðÚ9÷Þòì³H¡#f<Î.ÛÂѲ­Ät$u\9KoËGpE9b+ NsQÏ>ÉIiÒQs8ôòVÖMó.Þ÷ex ½½îx!RNÇZH?×b# Ó$v¥_SfçF«Q®n`|à`«T­®Ð¦©¸×5*<?qC´/ø©¿ÑJ¬ªï´´+ýÑHjiK( ;¤øÓA#mýH߬5v ¡µR
´RI{ÊPAn,&æF ,&Õ/ônÓÚ¨­oÄÀùYó}DUÿêà}C$e õ^LYæªYßÍX«Z2»ËeOæVqWvrW1ÚP«¿_kÆ-¢¢2Ar«ö©]x¢|4Ñ¿¯Y[¹">´¸(lÉÆÓÌå/BÙ!ìPGr£`¶Ê
[!ªóèsfÄt+­.v.5_jÎê'Ô8Õ
Ö1ÒøMÜÑõâ[®ÇÄ)ãZYàÜ;Q!Ëa2Û^Zìçsá¹Ø^´QZbKnk9&ÄKºý¼ý®Úú11pÀ¦Eç°¿9r¾«ÿ®yýÆà"ø×
 (ù ?Ko¼Þ!Τ¸ò¿^/ºo¼½åzãß*A9aãÔ°¥Ù8umÁàËuni(
'-®®DÕêüý¸o¢z­äݲé/lßþËÁi-ö:IhÙ8£i?|Û×RÐØï/ÐÀ,I
°Ù¥xXIRê0¹\x9séU=ªajä ^Æ£À±b¼$ÞP6<fF!µ¡Àßù!ûí7t¨¬XRÀJï\VãØsþð»b¨´7B` FýÀJÕ´F
6Ë
èög»&·ã«¶ÛýAuR®ÝåL¬.YÌÄÿ£0æÀ%dÅ)²dhs´k¯.Ûg%ÅÂ.sâ¬Úé
zµ½ ;¸Z}»ÕUH«;ébÿ£j£%³S÷ú~X´#ýgý:¹½ 0w#ºØHâ³·,`!«G(]íä,Q¡NØm¢Î°,ÔY`Ùi;Ò¦×b¤×?BûMd¡Çdª <ï"QÕýWÏ®µ±× "6'3ý¹¦wþGSCt7Ùäk Ne¾ªÑ¿Ò(múS Ô$DifA{ª*ö¥(-gg¶a³~v(dê©=Om+á½üí±6®yf°1²ÐNi(y`B»ò¦¹ÃàuS!kµ¡¡i÷6¦ hÖ½;r"¢R»yÆ&OªoE·VU´¹)_Q9Ç¥2lÑÀâîUhñ~¼4óß4"\µnøæèî½5Ç00fúiPàýLØ~¯é§?_HÙ&Í÷;Ynnw¯8àoü¬¼^+2
My Code:
mmo1.Lines.Text := idhtp1.Get('http://129.232.180.125:28019/deaths.json');
If the server has Content-Encoding: gzip in the headers, so you must decompress the data.
Here is a full & working example (the check for gzipped content is omitted):
program SO51126032;
{$APPTYPE CONSOLE}
{$R *.res}
uses
IdHttp,
IdZLib,
Classes,
System.SysUtils;
function GetServerData(Url : String) : String;
var
Http : TIdHttp;
Strm : TMemoryStream;
OutStrm : TStringStream;
begin
Http := TIdHttp.Create(nil);
try
Http.HandleRedirects := True;
Http.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
Http.Request.AcceptEncoding := 'gzip';
Strm := TMemoryStream.Create;
try
OutStrm := TStringStream.Create(TEncoding.UTF8);
try
Http.Get(URL, Strm);
Strm.Position := 0;
if TextIsSame(Http.Response.ContentEncoding, 'gzip') then
DecompressStream(Strm, OutStrm)
else
OutStrm.CopyFrom(Strm, 0);
end;
Result := OutStrm.DataString;
finally
OutStrm.Free;
end;
finally
Strm.Free;
end;
finally
Http.Free;
end;
end;
begin
try
Writeln(GetServerData('http://129.232.180.125:28019/deaths.json'));
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
* RECOMMENDED WAY *
Indy can do the hard work for you if you assign a TIdCompressorZlib to TIdHTTP, it will automatically detect gzipped content and decompress on the fly:
uses
IdHttp,
IdCompressorZLib,
System.SysUtils;
function GetServerData(const URL : String) : String;
var
Http : TIdHttp;
begin
Http := TIdHttp.Create(nil);
try
Http.HandleRedirects := True;
Http.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
Http.Compressor := TIdCompressorZLib.Create(Http);
Result := Http.Get(URL);
finally
Http.Free;
end;
end;

Sending emails from the USA

My program uses Indy 10 TIdSMTP and TIdMessage to send error reports via email. The program is used mainly in Germany, but some clients are in the USA.
The tool works flawlessly in Germany, but the USA clients are not able to send anything at all. The output shows that the connection to the German email server (smtp.web.de) is made, but after that, an immediate disconnect follows. The exception "Socket error #10054 connection reset by peer" is thrown.
Here's some code:
procedure SendMail(const PI_sSenderAddress: string; const PI_arrReceiverAddresses, PI_arrCCAddresses, PI_arrBCCAddresses: array of string; const PI_sSubject, PI_sMailText: string; const PI_arrAttachments: array of string; const PI_sHost: string = ''; const PI_sUsername: string = ''; const PI_sPassword: string = ''; const PI_bUseTLS: boolean = False; const PI_nPort: integer = 25; const PI_nTimeoutSeconds: integer = 5 );
var
smtp: TIdSMTP;
Mail: TIdMessage;
i: integer;
arrAttachments: array of TIdAttachmentFile;
io: TIdSSLIOHandlerSocketOpenSSL;
begin
io := nil;
smtp := TIdSMTP.Create(Application);
try
smtp.Password := PI_sPassword;
smtp.Username := PI_sUsername;
smtp.Port := PI_nPort;
smtp.Host := PI_sHost;
if PI_bUseTLS then begin
io := TIdSSLIOHandlerSocketOpenSSL.Create(Application);
smtp.IOHandler := io;
end;
smtp.UseTLS := utUseRequireTLS;
if PI_sUserName <> '' then begin
smtp.AuthType := satDefault;
end else begin
smtp.AuthType := satNone;
end;
smtp.HeloName := Split(PI_sUsername, 1, '#');
mail := TIdMessage.Create( Application );
try
mail.Clear;
mail.From.Address := PI_sSenderAddress;
mail.From.Text := PI_sSenderAddress;
mail.Subject := PI_sSubject;
mail.Recipients.EMailAddresses := ArrayToStr( PI_arrReceiverAddresses, ',' ); //!! only one
mail.CCList.EMailAddresses := ArrayToStr( PI_arrCCAddresses, ',' );
mail.BccList.EMailAddresses := ArrayToStr( PI_arrBCCAddresses, ',' );
mail.ReceiptRecipient.Text := '';
mail.Body.Add( PI_sMailText );
mail.Date := now;
if Length(PI_arrAttachments) > 0 then begin
SetLength(arrAttachments, Length(PI_arrAttachments));
for i := 0 to High(PI_arrAttachments) do begin
arrAttachments[i] := TIdAttachmentFile.Create(mail.MessageParts, PI_arrAttachments[i]);
end;
end;
try
smtp.ConnectTimeout := PI_nTimeoutSeconds * 1000;
smtp.Connect;
if smtp.Connected then begin
smtp.Send(mail);
end;
finally
smtp.disconnect;
if Length(arrAttachments) > 0 then begin
for i := 0 to High(arrAttachments) do begin
arrAttachments[i].Free;
end;
end;
end;
finally
mail.Free;
end;
finally
smtp.free;
if Assigned(io) then io.Free;
end;
end;
I call it like this:
SendMail(
'myaccount#web.de',
['receiver#web.de'],
[],
[],
'Subject',
'Text',
[],
'smtp.web.de',
'myaccount#web.de',
'mypassword',
True,
587
);
Can someone help?
EDIT: I changed to a googlemail account, but getting the same error. Currently, i call it like this:
I call it like this:
SendMail(
'myaccount#gmail.com',
['receiver#web.de'],
[],
[],
'Subject',
'Text',
[],
'smtp.gmail.com',
'myaccount#gmail.com',
'mypassword',
True,
587
);

Save INI file in UTF-8 rather than ANSI in Inno Setup

I'm starting to use Inno Setup, and I have some problems with my INI file encoding.
I want to save user input in the INI file, and this input can contain accents.
I use Inno Setup Unicode, my setupScript.iss is UTF-8 encoded, and here is my code (a part) :
[INI]
Filename: "{app}\www\conf\config.ini"; Section: "Settings"; Key: "ca.plafondAnnuel"; String: "{code:GetUser|Plafond}"
Filename: "{app}\www\conf\config.ini"; Section: "Settings"; Key: "app.siren"; String: "{code:GetUser|Siren}"
Filename: "{app}\www\conf\config.ini"; Section: "Settings"; Key: "app.adresse"; String: "{code:GetUser|Adresse}"
[Code]
var
UserPage: TInputQueryWizardPage;
ExamplePage : TInputOptionWizardPage;
ImmatriculationPage : TInputOptionWizardPage;
FakeElemIndex: Integer;
FakeElem: TCustomEdit;
AdresseTextarea: TNewMemo;
procedure InitializeWizard;
begin
UserPage := CreateInputQueryPage(wpWelcome,
'Configuration de l''application', '',
'Configurez ici votre application. Une fois installée, vous pourrez modifier ces valeurs.');
UserPage.Add('Siren :', False);
UserPage.Add('Plafond annuel (utilisé par les auto-entreprises, mettre 0 si vous ne souhaitez pas plafonner votre chiffre d''affaire.):', False);
FakeElemIndex := UserPage.Add('Votre adresse complète (telle qu''elle s''affichera sur les devis et factures, avec nom complet):', False);
FakeElem := UserPage.Edits[FakeElemIndex];
AdresseTextarea := TNewMemo.Create(WizardForm);
AdresseTextarea.Parent := FakeElem.Parent;
AdresseTextarea.SetBounds(FakeElem.Left, FakeElem.Top, FakeElem.Width, ScaleY(50));
// Hide the original single-line edit
FakeElem.Visible := False;
end;
function GetUser(Param: String): String;
begin
if Param = 'Adresse' then
Result := AdresseTextarea.Text
else if Param = 'Siren' then
Result := UserPage.Values[0]
else if Param = 'Plafond' then
Result := UserPage.Values[1];
end;
The value returned by getUser|Adresse in the [INI] part is not UTF-8 encoded: I open the INI file with Notepad++ and I see the file is UTF-8 encoded. But the value adresse is ANSI encoded (If I change the encoding of the file to ANSI, this value is readable)
Someone can help me understand how can I save this user input in UTF-8 ?
Thanks a lot !
The INI functions of Inno Setup ([INI] section and SetIni* functions) use internally the Windows API function WritePrivateProfileString.
This function does not support UTF-8 at all. All it supports is the ANSI encoding and UTF-16.
See How to read/write Chinese/Japanese characters from/to INI files?
So it's even questionable whether the target application will be able to read UTF-8-encoded INI file, if it relies on the Windows API function to read it.
Anyway, if you need the UTF-8, you would have to format the entries to INI format yourself and use SaveStringsToUTF8File function to write it.
The last option is to hack it by using the system call WritePrivateProfileString to write seemingly ANSI-encoded string, which will be in fact UTF-8-encoded.
For that you need to convert the string to UTF-8 in your code. You can use WideCharToMultiByte for that.
function WideCharToMultiByte(CodePage: UINT; dwFlags: DWORD;
lpWideCharStr: string; cchWideChar: Integer; lpMultiByteStr: AnsiString;
cchMultiByte: Integer; lpDefaultCharFake: Integer;
lpUsedDefaultCharFake: Integer): Integer;
external 'WideCharToMultiByte#kernel32.dll stdcall';
const
CP_UTF8 = 65001;
function GetStringAsUtf8(S: string): AnsiString;
var
Len: Integer;
begin
Len := WideCharToMultiByte(CP_UTF8, 0, S, Length(S), Result, 0, 0, 0);
SetLength(Result, Len);
WideCharToMultiByte(CP_UTF8, 0, S, Length(S), Result, Len, 0, 0);
end;
function WritePrivateProfileString(
lpAppName, lpKeyName, lpString, lpFileName: AnsiString): Integer;
external 'WritePrivateProfileStringA#kernel32.dll stdcall';
procedure CurStepChanged(CurStep: TSetupStep);
var
IniFileName: string;
begin
if CurStep = ssInstall then
begin
Log('Writting INI file');
if not ForceDirectories(ExpandConstant('{app}\www\conf')) then
begin
MsgBox('Error creating directory for INI file', mbError, MB_OK);
end
else
begin
IniFileName := ExpandConstant('{app}\www\conf\config.ini');
if (WritePrivateProfileString(
'Settings', 'ca.plafondAnnuel', GetStringAsUtf8(GetUser('Plafond')),
IniFileName) = 0) or
(WritePrivateProfileString(
'Settings', 'app.siren', GetStringAsUtf8(GetUser('Siren')),
IniFileName) = 0) or
(WritePrivateProfileString(
'Settings', 'app.adresse', GetStringAsUtf8(GetUser('Adresse')),
IniFileName) = 0) then
begin
MsgBox('Error writting the INI file', mbError, MB_OK);
end;
end;
end;
end;