473,856 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;

C# is as is :
[DllImport(Libra ryName)]
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(strPt r : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrPLCopy(strPt r, 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(Libra ryName)]
public static extern int GetString(strin g strPtr, int iStrLen);
....
....
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpst r, 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 18800
Hi Craig,

If you search on "Directiona l 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******** ******@tk2msftn gp13.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 := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;

C# is as is :
[DllImport(Libra ryName)]
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(strPt r : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrPLCopy(strPt r, 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(Libra ryName)]
public static extern int GetString(strin g strPtr, int iStrLen);
...
...
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpst r, 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 "Directiona l 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******** ******@tk2msftn gp13.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 := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;

C# is as is :
[DllImport(Libra ryName)]
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(strPt r : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrPLCopy(strPt r, 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(Libra ryName)]
public static extern int GetString(strin g strPtr, int iStrLen);
...
...
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpst r, 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******** ******@tk2msftn gp13.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 := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;

C# is as is :
[DllImport(Libra ryName)]
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(strPt r : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrPLCopy(strPt r, 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(Strin gBuilder sb, int iStrLen)

void test()
{
int len = ....;
StringBuilder sb = new StringBuilder( len );
GetString( sb, len );
Console.WriteLi ne( 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(Libra ryName)]
public static extern int GetString(strin g strPtr, int iStrLen);
...
...
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpst r, 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******** ******@tk2msftn gp13.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 := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrLCopy( strPtr, PChar(strTmp), iStrLen);
end;

C# is as is :
[DllImport(Libra ryName)]
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(strPt r : PChar; iStrLen : Integer ): Integer;
var
strTmp : String;
begin
strTmp := InternalFunctio nThatReturnsASt ring()
Result := Length(strTmp);
StrPLCopy(strPt r, 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(Strin gBuilder sb, int iStrLen)

void test()
{
int len = ....;
StringBuilder sb = new StringBuilder( len );
GetString( sb, len );
Console.WriteLi ne( 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(Libra ryName)]
public static extern int GetString(strin g strPtr, int iStrLen);
...
...
{
string tmpstr = new string(' ',MaxWidth);
int actualLen = GetString(tmpst r, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2271
by: David Lozzi | last post by:
Here is the proc: CREATE PROCEDURE . @CID as int, @Netname as nvarchar(25), @Return as int OUTPUT AS IF EXISTS (SELECT DISTINCT netname FROM computers WHERE CompanyID = @CID AND UPPER(netname) = UPPER(@Netname))
4
3044
by: David Lozzi | last post by:
Howdy, I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then save it back to my SQL server using a SQLCommand, all HTML characters are changed to HTML code, i.e. &gt;strong&lt; instead of <strong> and so on. Is this a problem with the editor or something else? Does .Net convert it? I had to disable page validate request because of the tags, is there more? Thanks,
0
5247
by: Tomek | last post by:
Welcome. I`m trying to prepere some project that shows how to use Corba technology i heterogenic systems.I`d like to create CORBA server with Borland Delphi (or C#.NET) and few client applications taht comunicate with serwer: J2ME client for WindowsCE 5.0, c# or Delphi from WindowsXP, some Web application using Java and maybe something for Linux system. For few days i`m looking for some examples in internet but most of them using only...
5
3043
by: Wilfried Mestdagh | last post by:
Hi, I have a DLL using stdcall and a function of it is returning a null terminated string. How do I declare this in C# (VS2005) ? I try things like: public static extern string Version();
7
4632
by: Joey Sabey | last post by:
Hey, I've got a bit of a strange problem caused by a situation I'm in. I'm writing a DLL for a friend of mine in C++, and he plans to use it in delphi. The DLL needs to return strings, and we doubt that a string would return well to a delphi program, so the only thing I could think of was returning a char*, which obviously would be very dangerous as the array would be being declared as a local variable and would only be returning it's...
14
3893
by: ApexData | last post by:
I am considering building some distributable commercial applications. For about a year now, I have been using Access2000. This was my first venture into object oriented database development. Having a background in Pascal and some C++, I would have preferred those languages, but VBA made do. The SQL was fine. I believe that Security issues on the backend, and data integrity/ corruption complaints over the network may be a stumbling...
9
1753
by: de | last post by:
between c and c++ I have understood is preferibile c++, but always? and regarding C# what changes? Moreover which are the main differences between the rad Turbo C++, Borland Studio6 C++, Visual Study .NET, Visual C++? and with all they is possible to make applications that turn with win XP, Linux and winodows Vista? Moreover,which are differences between delphi and C++ ?
4
4106
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Dear List, We have a large application written in Delphi. I am writing a mobile app written in .NET (C#) which reads and writes into the Delphi applications Oracle database via a .NET Web Service. Much of the data centred functionality on the application is quite involved, and to re-write all this functionality in the .NET web service would be very time consuming. Our idea is to perhaps develop a DLL written in Delphi, which copies...
1
10719
by: Jure Bogataj | last post by:
Hello! Does anybody knows how to handle this issue: I have an Delphi DLL with following two function declaration: function DeallocateString(lpszString : PChar) : DWORD; stdcall; function MyFunc1(lpszInput : PChar; var lpszOutput : PChar) : DWORD; stdcall;
0
9758
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7929
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5757
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.