Calling DLL functions, strings, pointers

G

Guest

Hi,

If I make a DLL in Delphi (5..7, so not NET) to manipulate strings, then I
assume I have to do something like this:

[DllImport("test.dll")]
public static extern TestProc(String C);

and in the Delphi DLL:

procedure TestProc(C: PChar); stdcall;

I think so because I have made such P/Invoke things for Win32 API whitch
also accept a pointer to null terminated data and are fine working.

This also mean I can modify the data of C safely in my DLL ?

All advice is very welcome, I'm still a newbe for dotnet :)
 
N

Nicholas Paldino [.NET/C# MVP]

Wilfried,

What does PChar map to? This will affect the declaration on the .NET
side. If PChar is a char string, then you would do this:

[DllImport("test.dll", CharSet=CharSet.Ansi)]
public static extern void TestProc(String C);

If it is a unicode string then you would do this:

[DllImport("test.dll", CharSet=CharSet.Unicode)]
public static extern void TestProc(String C);

Now, if you modified the contents of C in the string, you would have to
declare your function using a StringBuilder, like so:


[DllImport("test.dll", CharSet=CharSet.Unicode)]
public static extern void TestProc(StringBuilder C);

Of course, you would have to allocate space in the string builder before
you pass it to the function (otherwise, you could get a nasty access
violation).

Hope this helps.
 
G

Guest

Hi Nicholas,
What does PChar map to? This will affect the declaration on the .NET
side. If PChar is a char string, then you would do this:

Thanks for your reply. Yes, PChar is pointer to null terminated string. So I
guess StringBuilder is what I need if I make changes into the string,
otherwise String.

rgds, Wilfried
http://www.mestdagh.biz

Nicholas Paldino said:
Wilfried,

What does PChar map to? This will affect the declaration on the .NET
side. If PChar is a char string, then you would do this:

[DllImport("test.dll", CharSet=CharSet.Ansi)]
public static extern void TestProc(String C);

If it is a unicode string then you would do this:

[DllImport("test.dll", CharSet=CharSet.Unicode)]
public static extern void TestProc(String C);

Now, if you modified the contents of C in the string, you would have to
declare your function using a StringBuilder, like so:


[DllImport("test.dll", CharSet=CharSet.Unicode)]
public static extern void TestProc(StringBuilder C);

Of course, you would have to allocate space in the string builder before
you pass it to the function (otherwise, you could get a nasty access
violation).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Wilfried Mestdagh said:
Hi,

If I make a DLL in Delphi (5..7, so not NET) to manipulate strings, then I
assume I have to do something like this:

[DllImport("test.dll")]
public static extern TestProc(String C);

and in the Delphi DLL:

procedure TestProc(C: PChar); stdcall;

I think so because I have made such P/Invoke things for Win32 API whitch
also accept a pointer to null terminated data and are fine working.

This also mean I can modify the data of C safely in my DLL ?

All advice is very welcome, I'm still a newbe for dotnet :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top