Hello,
I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006.
The DLL works fine when called from a win32 app. The problem I am encountering is that the string being returned to the caller is in Chinese (No, I'm not kidding). (I need it to be in English).
Background: The DLL uses 3 pchar parms, 2 in and one out. In debug mode the ws is passing the 2 string parms in ok (I can see them) but the "string" being returned is all unprintable characters.
I am using the default Cassini server built in to D2006. I "hardcoded" hello world as my return string to rule out anything internal to the DLL, and it still returns 5 chinese characters to my calling web browser. FYI I tried changing the response Encoding in the web.config from utf-8 to both ISO-8859-1 (or whatever) and "Windows-1252" to no avail.
Note the HelloWorld and Add functions both return legible data. I am sure it has something to do with widestrings vs regular strings but I've tried everything I can think of and its still Chinese. I have read every article an tip that Google can find. I am using PChar in the DLL because I dont want to mess with ShareMem.
This is my first time using .Net and I am stuck, so any help would be Greatly Appreciated!
Thanks!
Sample code follows.
The DLL:
=============== =============== =============== ==
library testDLL;
uses
SysUtils,
Classes;
function GetData(Name, Email, Answer : PChar) : Integer; stdCall;
var
S: string;
begin
// Note for purposes of this demo I am not doing anything with the input parms
s := 'hello' + 'world';
StrPCopy(Answer ,S );
Result := Length(S);
end;
exports GetData;
begin
end.
=============== =============== =============== =======
The webService code:
=============== ====
unit WebService1;
interface
{$UNSAFECODE ON} // Note I needed this so I can use PChar
uses
System.Collecti ons, System.Componen tModel,
System.Data, System.Diagnost ics, System.Web,
System.Web.Serv ices;
function GetData(Name, Email, Answer: PChar) : Integer; StdCall; External 'testDLL.dll'; unsafe;
type
TWebService1 = class(System.We b.Services.WebS ervice)
{$REGION 'Designer Managed Code'}
strict private
components: IContainer;
procedure InitializeCompo nent;
{$ENDREGION}
strict protected
procedure Dispose(disposi ng: boolean); override;
private
{ Private Declarations }
public
constructor Create;
// Sample Web Service Method
[WebMethod]
function HelloWorld: string;
[WebMethod]
function Add(A, B: Integer): Integer;
[WebMethod]
function GetKey(Name, Email : String) : String;
end;
var
c : integer;
implementation
{$REGION 'Designer Managed Code'}
procedure TWebService1.In itializeCompone nt;
begin
end;
{$ENDREGION}
constructor TWebService1.Cr eate;
begin
inherited;
InitializeCompo nent;
end;
procedure TWebService1.Di spose(disposing : boolean);
begin
if disposing and (components <> nil) then
components.Disp ose;
inherited Dispose(disposi ng);
end;
// Sample Web Service Method
// The following method is provided to allow for testing a new web service.
function TWebService1.He lloWorld: string;
begin
Result := 'Hello World';
end;
function TWebService1.Ad d(A, B: Integer): Integer;
begin
Result := A + B;
end;
function TWebService1.Ge tKey(Name, Email: String): String; unsafe;
var N, E, X : PChar;
R : Array[0..250] of char;
S : String;
i, ctr : Integer;
begin
N := @Name;
E := @Email;
ctr := GetData(@Name, @Email, @R);
i := 0;
While R[i] <> #0 do begin // had to do it this way because
S := S+R[i]; // no typecasting or conversion routines worked.
i := i + 1;
end;
Result := S;
end;
end.
I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006.
The DLL works fine when called from a win32 app. The problem I am encountering is that the string being returned to the caller is in Chinese (No, I'm not kidding). (I need it to be in English).
Background: The DLL uses 3 pchar parms, 2 in and one out. In debug mode the ws is passing the 2 string parms in ok (I can see them) but the "string" being returned is all unprintable characters.
I am using the default Cassini server built in to D2006. I "hardcoded" hello world as my return string to rule out anything internal to the DLL, and it still returns 5 chinese characters to my calling web browser. FYI I tried changing the response Encoding in the web.config from utf-8 to both ISO-8859-1 (or whatever) and "Windows-1252" to no avail.
Note the HelloWorld and Add functions both return legible data. I am sure it has something to do with widestrings vs regular strings but I've tried everything I can think of and its still Chinese. I have read every article an tip that Google can find. I am using PChar in the DLL because I dont want to mess with ShareMem.
This is my first time using .Net and I am stuck, so any help would be Greatly Appreciated!
Thanks!
Sample code follows.
The DLL:
=============== =============== =============== ==
library testDLL;
uses
SysUtils,
Classes;
function GetData(Name, Email, Answer : PChar) : Integer; stdCall;
var
S: string;
begin
// Note for purposes of this demo I am not doing anything with the input parms
s := 'hello' + 'world';
StrPCopy(Answer ,S );
Result := Length(S);
end;
exports GetData;
begin
end.
=============== =============== =============== =======
The webService code:
=============== ====
unit WebService1;
interface
{$UNSAFECODE ON} // Note I needed this so I can use PChar
uses
System.Collecti ons, System.Componen tModel,
System.Data, System.Diagnost ics, System.Web,
System.Web.Serv ices;
function GetData(Name, Email, Answer: PChar) : Integer; StdCall; External 'testDLL.dll'; unsafe;
type
TWebService1 = class(System.We b.Services.WebS ervice)
{$REGION 'Designer Managed Code'}
strict private
components: IContainer;
procedure InitializeCompo nent;
{$ENDREGION}
strict protected
procedure Dispose(disposi ng: boolean); override;
private
{ Private Declarations }
public
constructor Create;
// Sample Web Service Method
[WebMethod]
function HelloWorld: string;
[WebMethod]
function Add(A, B: Integer): Integer;
[WebMethod]
function GetKey(Name, Email : String) : String;
end;
var
c : integer;
implementation
{$REGION 'Designer Managed Code'}
procedure TWebService1.In itializeCompone nt;
begin
end;
{$ENDREGION}
constructor TWebService1.Cr eate;
begin
inherited;
InitializeCompo nent;
end;
procedure TWebService1.Di spose(disposing : boolean);
begin
if disposing and (components <> nil) then
components.Disp ose;
inherited Dispose(disposi ng);
end;
// Sample Web Service Method
// The following method is provided to allow for testing a new web service.
function TWebService1.He lloWorld: string;
begin
Result := 'Hello World';
end;
function TWebService1.Ad d(A, B: Integer): Integer;
begin
Result := A + B;
end;
function TWebService1.Ge tKey(Name, Email: String): String; unsafe;
var N, E, X : PChar;
R : Array[0..250] of char;
S : String;
i, ctr : Integer;
begin
N := @Name;
E := @Email;
ctr := GetData(@Name, @Email, @R);
i := 0;
While R[i] <> #0 do begin // had to do it this way because
S := S+R[i]; // no typecasting or conversion routines worked.
i := i + 1;
end;
Result := S;
end;
end.