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)

LVL 1
fischermxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dagan HooverDeveloperCommented:
Try a PByteArray instead of PChar. See if you get more.
0
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:

var
sFile : String;
pFile : PChar;
begin

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

OpenThisFile(pFile);

How do I use a byte array here?
0
ChristianWimmerCommented:
In D2009 a PChar is really a PWideChar which is not the same as the C function declaration. You can use PAnsiChar instead.
0
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.
0
ChristianWimmerCommented:
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:
OpenThisFile(PAnsiChar(AnsiString(MyString)));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.