473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 10616
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.