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 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
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
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
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
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
|
reply
views
Thread by Tomek |
last post: by
|
5 posts
views
Thread by Wilfried Mestdagh |
last post: by
|
7 posts
views
Thread by Joey Sabey |
last post: by
|
14 posts
views
Thread by ApexData |
last post: by
|
9 posts
views
Thread by de |
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
| | | | | | | | | | |