472,111 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

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


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
Nov 16 '05 #1
4 18555
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

"Craig Kenisston" <cr************@hotmail.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...

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

Nov 16 '05 #2
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

"Craig Kenisston" <cr************@hotmail.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...

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

Nov 16 '05 #3
Hi, inline

"Craig Kenisston" <cr************@hotmail.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...

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

Nov 16 '05 #4
Hi, inline

"Craig Kenisston" <cr************@hotmail.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...

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

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David Lozzi | last post: by
4 posts views Thread by David Lozzi | last post: by
7 posts views Thread by Joey Sabey | last post: by
4 posts views Thread by =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post: by
1 post views Thread by Jure Bogataj | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.