473,775 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delphi PChar and C#

Hello!

Does anybody knows how to handle this issue:

I have an Delphi DLL with following two function declaration:

function DeallocateStrin g(lpszString : PChar) : DWORD; stdcall;
function MyFunc1(lpszInp ut : 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). DeallocateStrin g 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("myli b.dll", EntryPoint = "DeallocateStri ngA", CharSet =
CharSet.Ansi, ExactSpelling = true, CallingConventi on =
CallingConventi on.Winapi)]
public static extern uint DeallocateStrin g(string lpszString);
[DllImport("myli b.dll", EntryPoint = "MyFunc1A", CharSet = CharSet.Ansi,
ExactSpelling = true, CallingConventi on = CallingConventi on.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.MyFu nc1("mystring", ref myvalue);
MessageBox.Show (myvalue); // This is OK
DLLWrapper.Deal locateString(my value); // 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 10702
Jure Bogataj wrote:
Hello!

Does anybody knows how to handle this issue:

I have an Delphi DLL with following two function declaration:

function DeallocateStrin g(lpszString : PChar) : DWORD; stdcall;
function MyFunc1(lpszInp ut : 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("name OfDLL", 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
2679
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 --> I assume VB: String The Delphi Declaration looks as follows: function MP_GetMessageList( HDL : Integer; var ErrCount : Integer;
7
3727
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 : String;
4
18788
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 : PChar; iStrLen : Integer ): Integer; var strTmp : String; begin
4
3246
by: erdosa | last post by:
Hi I want use dll (Delphi 7) in c# example delphi procedure GetNev(s:PChar);export; begin
1
4472
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. thanks here is my dll: #include "stdafx.h" #include "djencode.h"
0
1363
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 listing of one of the UDF's and the associated create function. The UDF is written using Delphi 6. The UDF doesn't fail at the same point each time you rerun the process. If anybody has had a similar situation, we'd be more than interested in...
1
2042
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 FullName:pchar; export; // my code public static extern string FullName();
5
3867
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 extern "C" char *Func1(int isDsb, char *firstString, char
0
1740
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 works fine when called from a win32 app. The problem I am encountering is that the string being returned to the caller is in Chinese (No, I'm not kidding). (I need it to be in English). Background: The DLL uses 3 pchar parms, 2 in and one out. In...
0
9454
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
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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
9915
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...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4014
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.