Board index » delphi » Returning ShortStrings from C

Returning ShortStrings from C


2004-07-10 08:10:45 PM
delphi109
Hi,
I currently need to write a plug-in for a Delphi application, and I need
to write that plug-in in C using Visual Studio .NET 2003 (unfortunately
not Borland C++ Builder, that would make things easier I guess). The
Delphi prototype is
function PluginName:ShortString; stdcall;
and I have written the following C method
#pragma pack(push,1)
typedef struct {
unsigned char length;
char array[255];
} ShortString;
#pragma pack(pop)
void WINAPI TPlugin::PluginName(ShortString* destination) {
static ShortString name={0,"PluginName"};
if (!name.length) name.length=strlen(name.array);
memcpy(destination,&name,1+name.length);
}
As Delphi ShortStrings are returned on the stack pointed to by an
implicit parameter (which I am making explicit in the C method), this
basically works very well. But under certain circumstances and after
calling the above method several times, I get an exception in the main
program which uses this plug-in. Interestingly, the exception does not
occur when I return an empty string above, i.e. doing
void WINAPI TPlugin::PluginName(ShortString* destination) {
static ShortString name={0,""};
memcpy(destination,&name,1+name.length);
}
So I guess I am doing some kind of stack corruption in my code, but I do
not see where. Can anyone give me a hint?
--
Sebastian Schuberth
 
 

Re:Returning ShortStrings from C

Quote
shortstrings in delphi have a leading length
byte.
I know, that is what the "unsigned char length" field is for ;-)
Quote
to make things simply try using charactor
pointer type strings with nulls
and in delphi just have the return as
Pchar.
I can not because I am just writing the plug-in. I have no influence on
what kind of return value the main programs expects.
--
Sebastian Schuberth
 

Re:Returning ShortStrings from C

shortstrings in delphi have a leading length
byte.
to make things simply try using charactor
pointer type strings with nulls
and in delphi just have the return as
Pchar.
make sure the pointer that is being return
is from a string or charactor array that stays
alive.
Sebastian Schuberth writes:
Quote
Hi,

I currently need to write a plug-in for a Delphi application, and I need
to write that plug-in in C using Visual Studio .NET 2003 (unfortunately
not Borland C++ Builder, that would make things easier I guess). The
Delphi prototype is

function PluginName:ShortString; stdcall;

and I have written the following C method

#pragma pack(push,1)

typedef struct {
unsigned char length;
char array[255];
} ShortString;

#pragma pack(pop)

void WINAPI TPlugin::PluginName(ShortString* destination) {
static ShortString name={0,"PluginName"};
if (!name.length) name.length=strlen(name.array);
memcpy(destination,&name,1+name.length);
}

As Delphi ShortStrings are returned on the stack pointed to by an
implicit parameter (which I am making explicit in the C method), this
basically works very well. But under certain circumstances and after
calling the above method several times, I get an exception in the main
program which uses this plug-in. Interestingly, the exception does not
occur when I return an empty string above, i.e. doing

void WINAPI TPlugin::PluginName(ShortString* destination) {
static ShortString name={0,""};
memcpy(destination,&name,1+name.length);
}

So I guess I am doing some kind of stack corruption in my code, but I do
not see where. Can anyone give me a hint?