472,958 Members | 2,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Delphi PChar and C#

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;

This function (MyFunc1) processes lpszInput and stores the result in
lpszOutput using AllocMem function in Delphi to allocate memory (delphi's
own memory function; must be released by calling FreeMem, also using Delphi
code). DeallocateString function's solely purpose is to destroy PChar
(string) create by delphi's AllocMem function (it only calls FreeMem and
that's it!).

I have made a function prototypes in C#:
[DllImport("mylib.dll", EntryPoint = "DeallocateStringA", CharSet =
CharSet.Ansi, ExactSpelling = true, CallingConvention =
CallingConvention.Winapi)]
public static extern uint DeallocateString(string lpszString);
[DllImport("mylib.dll", EntryPoint = "MyFunc1A", CharSet = CharSet.Ansi,
ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern uint MyFunc1(string lpszInputText, ref string
lpszOutputValue);

MyFunc1 is working as expected (it returns the right string into
lpszOutputValue parameter). Here's the test code:
string myvalue = "";
uint res = DLLWrapper.MyFunc1("mystring", ref myvalue);
MessageBox.Show(myvalue); // This is OK
DLLWrapper.DeallocateString(myvalue); // Here I get an exception: "External
component has thrown an exception."

Am I doing this right? How does C# pass string parameters, whereas Delphi
expects it to be PChar. Delphi then tries to free this string, but obviously
it cannot free it for some reason. Must I use something else or declare
function differently. I'm wondering in the dark here, so any help would be
greatly appreciated. Since C# does not support pointer type, is this code at
all possible?

Thanks in advance!
Best regards,
Jure


Jun 27 '08 #1
1 10503
Jure Bogataj wrote:
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;
Problem is that the string buffer is probably also allocated by the
DLL. This means you'll have to keep the pointer around as an IntPtr,
since marshalling will create new strings, and you don't want the DLL
to deallocate a buffer allocated by the marshaller. This means you
can't easily access the string as such, only pass it around to other
functions of the DLL.

If you have control over the DLL, it is better not to let the DLL
allocate or deallocate strings at all, just to let them fill buffers
allocated by the client (or the marhsller, in this time).

The second function is probably best declared as:

[DllImport("nameOfDLL", CharSet = CharSet.Ansi,
EntryPoint = "MyFunc1")]
public static extern uint MyFunc1(string lpszInput,
ref StringBuilder lpszOutput);

Now the marshaller also doesn't know the size of the second buffer, but
I guess you'll have to handle that, by passing a StringBuilder of a
suitable size.

--
Rudy Velthuis http://rvelthuis.de

"A narcissist is someone better looking than you are."
- Gore Vidal
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Walti | last post by:
Hi I'm trying to interface a foreign DLL that is written in Delphi. I know: - Delphi-Integer is a 32-Bit signed Integer --> VB: LONG - Delphi-PChar is a Pointer to Zero-Terminated ASCII String...
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
4
by: Craig Kenisston | last post by:
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...
4
by: erdosa | last post by:
Hi I want use dll (Delphi 7) in c# example delphi procedure GetNev(s:PChar);export; begin
1
by: denis | last post by:
hello I'm using a dll made in c++ in a delphi app, but get some errors I've use stdcall for the parameters match. I need someone have use C dll in delphi tell me if there is missing something....
0
by: Ivan | last post by:
We have just upgraded from UDB DB2 V7.2 to UDB DB2 V8.2 fixpack 12. We're having problems with UDF's that ran successfully under V7.2 but are now failing with SQL04030N sporatically. Below is a...
1
by: =?Utf-8?B?U2hhdW5P?= | last post by:
Hi, I need to call 2 functions from an old Delphi .dll but am struggling to formulate my call to the second one :( I have managed to one of them as follows: // definition from .dll function...
5
by: kelvin.koogan | last post by:
How can I call a function in a Delphi DLL from C++/CLI? The Delphi function is declared as follows: function Func1(IsDsb: Boolean; FirstStr, SecondStr : String): String; I've tried ...
0
by: BornTOCode | last post by:
Hello, I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006. The DLL...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.