Calling a function from Delphi 2009 to a C++ Dll

So, I have to use a previously compiled DLL made in C++.

There's a function defined as :
extern "C" SHORT DLLExport WINAPI OpenThisFile(PBYTE cpFileName)

I used to have the next code in Delphi 5,6,7,2005,006 and 2007 which used to work:
OpenThisFile: function(cpFileName: PChar); SmallInt stdcall

But now, in Delphi 2009 that seems broken, it seems from the error messages I'm getting that the C++ function just reads the first character of the filename I'm passing.

How do I do that now in Delphi 2009? (and preferable be backward compatible with Delphi 2007 and older)

Who is Participating?
ChristianWimmerConnect With a Mentor Commented:
Usually if an ANSI char function only reads the first char, the problem is a NULL character at the second place right behind the first char.
Maybe you're using type String in D2009 through your app which is a UnicodeString.
So try to convert the String to ANSI at first:

Open in new window

Try a PByteArray instead of PChar. See if you get more.
fischermxAuthor Commented:
I know this is a Delphi question, but how do I copy my data from the string to the bytearray?
I have currently have:

sFile : String;
pFile : PChar;

sFile := 'test.txt';
pFile := @sFile[1];


How do I use a byte array here?
In D2009 a PChar is really a PWideChar which is not the same as the C function declaration. You can use PAnsiChar instead.
fischermxAuthor Commented:
I already tried PAnsiChar and it behave exactly the same. I changed the parameter and the declaration in Delphi from PChar to PAnsiChar, and the C++ function still see only the first character.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.