Returning a PChar/String from a Delphi dll to .Net application.

  • Thread starter Craig Kenisston
  • Start date
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
 
D

Doug Forster

Hi Craig,

If you search on "Directional Attributes" (with the quotes) you will find
a very clear explanation in the MSDN help.

Cheers

Doug Forster
 
B

BMermuys

Hi, inline

Craig Kenisston said:
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;

The second case is preferred but you must use a StringBuilder as parameter
in c#: A stringbuilder is always [In,Out].

[DllImport(...)]
public static extern int GetString(StringBuilder sb, int iStrLen)

void test()
{
int len = ....;
StringBuilder sb = new StringBuilder( len );
GetString( sb, len );
Console.WriteLine( sb.ToString() );
}


hth,
greetings

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top