C
Craig Kenisston
Hi,
I have a dll that must return a string to a C# .Net application and also
needs to server a Delphi application.
I have defined my delphi function like this :
function GetString(var strPtr : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctionThatReturnsAString()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;
C# is as is :
[DllImport(LibraryName)]
public static extern int GetString(ref string strPtr, int iStrLen);
....
....
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(ref tmpstr, MaxWidth);
result = tmpstr;
}
This works. Just why everybody tells, that the PChar should not have to be
passed using "Var" ??
I've read that like a hundred times in old posts.
If I try to do it this way :}
function GetString(strPtr : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctionThatReturnsAString()
Result := Length(strTmp);
StrPLCopy(strPtr, strTmp, iStrLen);
end;
This works great as long as I call this function from another Delphi
application, but from C#, it returns an gives me an empty string !
C# is as is :
[DllImport(LibraryName)]
public static extern int GetString(string strPtr, int iStrLen);
....
....
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpstr, MaxWidth);
result = tmpstr;
}
C# application gets an empty string with this approach. Delphi application
however get the correct string.
Well, I found already the approach that worked in this case, i.e using Var,
however, I still not knowing why exactly one works on the other does not,
and more annoying going against everybody's advise.
I'm not fan of "accidental programming", so I would really thank anyone who
could shed some light on this !
Thanks in advance,
CK
I have a dll that must return a string to a C# .Net application and also
needs to server a Delphi application.
I have defined my delphi function like this :
function GetString(var strPtr : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctionThatReturnsAString()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;
C# is as is :
[DllImport(LibraryName)]
public static extern int GetString(ref string strPtr, int iStrLen);
....
....
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(ref tmpstr, MaxWidth);
result = tmpstr;
}
This works. Just why everybody tells, that the PChar should not have to be
passed using "Var" ??
I've read that like a hundred times in old posts.
If I try to do it this way :}
function GetString(strPtr : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctionThatReturnsAString()
Result := Length(strTmp);
StrPLCopy(strPtr, strTmp, iStrLen);
end;
This works great as long as I call this function from another Delphi
application, but from C#, it returns an gives me an empty string !
C# is as is :
[DllImport(LibraryName)]
public static extern int GetString(string strPtr, int iStrLen);
....
....
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpstr, MaxWidth);
result = tmpstr;
}
C# application gets an empty string with this approach. Delphi application
however get the correct string.
Well, I found already the approach that worked in this case, i.e using Var,
however, I still not knowing why exactly one works on the other does not,
and more annoying going against everybody's advise.
I'm not fan of "accidental programming", so I would really thank anyone who
could shed some light on this !
Thanks in advance,
CK