Re: Using function with PChar data type
- From: "Andrius B." <andriusbl@xxxxxxx>
- Date: Sat, 23 May 2009 21:56:02 +0300
Hi,
Thanks for your trying to help.
The problem is, that this DLL with that function was made not by me, but
another programmer; I needed this function in order to improve my app
written in VB.NET, bay adding some new function. So, the author of DLL just
sent me the brief description of the function, and that's all.
After this weekend I'll try to connect with him and ask additional questions
about specifications for using it in VB.
If I find out smth I will write here.
Thanks again.
Andrius
"Mike" <unknown@xxxxxxxxxx> wrote in message
news:OMaDj902JHA.3544@xxxxxxxxxxxxxxxxxxxxxxx
Mike wrote:
Forgot to point out, maybe it will matter by the compiler directive
{$H-}
may make a big difference. Try {$H+} as well. If I recall, it makes a
short STRING into a HUGE string. A short again, is the zero index byte
length format but its limite to 255 characters. The {$H+} makes all
those string type into C like pointer strings.
--
Andrius,
You might have figured it out by now, if you did, pass on the info :-)
I tried to figure out why I could not get basic delphi pascal DLL to
marshal correctly. But what I did was to make sure it worked as a C dll.
---------------- cut/paste to mydll.c ------------
// file: mydll.c
// compiled like so: cl mydll.c /LD /DLL
#include <stdio.h>
#include <windows.h>
__declspec( dllexport ) int __stdcall GetStatus(char *status)
{
strcpy(status,"success from mydll.dll");
return 1;
}
------------------- cut here ------------------------
This worked calling it from C applet, VB.NET, even a delphi applet
For VB.NET, the import just like I gave you, worked fine:
' import mydll.c
Declare Auto Function cdll_GetStatus Lib "mydll.dll"
(ByVal status As IntPtr) As Integer
' wrapper
Function GetStatus(ByRef status As String) As Integer
Dim h As IntPtr = Marshal.AllocHGlobal(255)
Dim res As Integer = cdll_GetStatus(h)
status = Marshal.PtrToStringAnsi(h)
Marshal.FreeHGlobal(h)
Return res
End Function
Well, a Delphi DLL should work the same:
// File : testpasdll.pas
{$H-,O-,A-}
Library testpasdll;
uses
sysutils,windows;
function GetStatus(var status : pchar): integer; stdcall;
begin
StrPlCopy(status,'success from pascal dll',255);
result := 1;
end;
//////////////////////////////////// EXPORT LIST
Exports
GetStatus index 1;
end.
Its been so long that I have worked with the finer details of delphi, but
I am sure it is related to getting to the mixed string stuff in delphi
similar to how VB6 had BSTR string and fixed strings mixing.
Bu I just have not figured why the above does not marshal correctly. It is
functionally the same as the c version. But I will say, that generally,
it recommended when passing strings with have a size with it, similar to
other WIN32 functions that has string by reference pointer with a size to
copy to the pointer and the pointer is allocated in the caller side with
the function simply filling in.
But if you don't have control of the DLL, then you have to prototype it
the why it is with var status : pchar.
I'll be interested to see what you find out to get it right.
--
.
- Follow-Ups:
- Re: Using function with PChar data type
- From: Mike
- Re: Using function with PChar data type
- References:
- Using function with PChar data type
- From: Andrius B.
- Re: Using function with PChar data type
- From: Mike
- Re: Using function with PChar data type
- From: Andrius B.
- Re: Using function with PChar data type
- From: Mike
- Re: Using function with PChar data type
- From: Mike
- Re: Using function with PChar data type
- From: Mike
- Using function with PChar data type
- Prev by Date: Re: use variable as textbox name?
- Next by Date: Re: use variable as textbox name?
- Previous by thread: Re: Using function with PChar data type
- Next by thread: Re: Using function with PChar data type
- Index(es):
Relevant Pages
|