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.Collections, System.ComponentModel,
System.Data, System.Diagnostics, System.Web,
System.Web.Services;
function GetData(Name, Email, Answer: PChar) : Integer; StdCall; External 'testDLL.dll'; unsafe;
type
TWebService1 = class(System.Web.Services.WebService)
{$REGION 'Designer Managed Code'}
strict private
components: IContainer;
procedure InitializeComponent;
{$ENDREGION}
strict protected
procedure Dispose(disposing: 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.InitializeComponent;
begin
end;
{$ENDREGION}
constructor TWebService1.Create;
begin
inherited;
InitializeComponent;
end;
procedure TWebService1.Dispose(disposing: boolean);
begin
if disposing and (components <> nil) then
components.Dispose;
inherited Dispose(disposing);
end;
// Sample Web Service Method
// The following method is provided to allow for testing a new web service.
function TWebService1.HelloWorld: string;
begin
Result := 'Hello World';
end;
function TWebService1.Add(A, B: Integer): Integer;
begin
Result := A + B;
end;
function TWebService1.GetKey(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.